Thursday, December 16, 2010

Project Code

#include <Servo.h>
#define CLOSED 11
#define ALARM 13
int but = 0;
const int analogPin = 0;
const int ledCount = 8;
int ledPins[] = {
  2,3,4,5,6,7,8,9
  };  
Servo myservo;
int val = 0;
int sensorReading = 0;
#define soundalarm 10


//FUNCTIONS


void setup(){
  for (int thisLed = 0; thisLed < ledCount; thisLed++){
    pinMode(ledPins[thisLed], OUTPUT);
  }
  pinMode(CLOSED, INPUT);
  pinMode(ALARM, INPUT);
  pinMode(soundalarm, OUTPUT);
  myservo.attach(12);
  myservo.write(val);
}


void senRead(){
  int sensorReading = analogRead(analogPin);
  if (sensorReading < 550){
    senRead();
  }
  else{
    int ledLevel = map(sensorReading, 500, 1023, 0, ledCount);
    for (int thisLed = 0; thisLed < ledCount; thisLed++){
      if (thisLed < ledLevel){
        digitalWrite(ledPins[thisLed], HIGH);
      }
      else {
        digitalWrite(ledPins[thisLed], LOW);
      }
    }
  }
  if (sensorReading >= 975){
    senRead();
  }
  delay(1000);
}


void openDoor(){
  val = 1500;
  myservo.write(val);
  delay(1000);
}


void alarm(){
  int a = digitalRead(ALARM);
  if (a == HIGH){
    delay(1000);
    alarm2();
  }
  else{
    alarm();
  }
}

void alarm2(){
  int but = digitalRead(CLOSED);
  int a = digitalRead(ALARM);
  if (a == HIGH){
      digitalWrite(soundalarm, HIGH);
      delay(5000);
      digitalWrite(soundalarm, LOW);
      alarm2();
  }
  else{
    if (but == HIGH){
      val = 0;
      myservo.write(val);
      delay(1000);
      clearleds();
    }
    else{
      alarm2();
    }
  }
}
  
void clearleds(){
  for (int thisLed = 0; thisLed < ledCount; thisLed++){
    digitalWrite(ledPins[thisLed], LOW);
  }
}
 
 
 
  //MAIN LOOP


void loop(){
  senRead();
  openDoor();
  alarm();
}

Project 3 - Parts/Product

The whole electronic system

The LED indicators

The contact switch and buzzer

Basic contact switch

The MQ3 all wired up

The alarm

High torque servo

Finished cabinet


3/4 angle view of finished cabinet

Rear view of finished cabinet

Mood Board Pic

Inspirational/Helpful Sites

http://www.danielandrade.net/2010/03/07/building-an-breathalyzer-with-mq-3-and-arduino/

http://nootropicdesign.com/projectlab/2010/09/17/arduino-breathalyzer/

http://www.lulu.com/items/volume_63/1108000/1108699/4/print/ARDUINO_NOTEBOOKv6.pdf

http://www.youtube.com/watch?v=sVbCqQAsUcs