Tuesday, December 3, 2013

Below shows the new and improved Alarm code as described in the previous post. A new variable TRAP =0 is set at the beginning of the code. The alarm loop is now activivated when it receives a HIGH signal or variable TRAP = 1. As a result, when the board receives a single HIGH signal, it sets TRAP =1, indefinitely trapping the arduino in the alarm loop. Pressing the reset button sets TRAP =0, ending the loop and stopping the alarm


const byte POTENTIOMETER = 0;  // Plugged into A0
const byte CONTROL = 13;  // pin #
int reading;
int value;
int MakeSound=0;
int Trap = 0;
int ledArray[] = {1,2,3,4,5};  //LED Array
int rate = 50;
int pause = 1;
void setup() {
  pinMode(CONTROL, OUTPUT);
  pinMode(11,OUTPUT);
 
  beep(50);
  beep(50);
  beep(50);
  delay(1000);
 
  pinMode(8,INPUT);
 
 
  for (int i=0; i<5; i++){  //LED Array
    pinMode(ledArray[i], OUTPUT);
 
}
}
void loop() {
  MakeSound=digitalRead(8);
   if (MakeSound==HIGH || Trap==HIGH)
   {
     Trap=1;
     {
       beep(200);
     }
  for (int i=0; i<5; i++){
    digitalWrite(ledArray[i], HIGH);
    delay(rate);
    digitalWrite(ledArray[i], LOW);
    if (i == 4) delay(pause*rate);
    else delay(rate);
  }
   for (int j=1; j<4; j++){
    int k = 4-j;
    digitalWrite(ledArray[k], HIGH);
    delay(rate);
    digitalWrite(ledArray[k], LOW);
    delay(rate);
  }
}
  reading = analogRead(POTENTIOMETER);
  value = map(reading, 0, 1024, 0, 255);  
  analogWrite(CONTROL, value);
} // end loop

void beep(unsigned char delayms){
  analogWrite(11, 20);      // Almost any value can be used except 0 and 255
                           // experiment to get the best tone
  delay(delayms);          // wait for a delayms ms
  analogWrite(11, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms  
}

No comments:

Post a Comment