I need to make an alarm that 1) makes a sound and "agitates" a servo when it detects light and b) the push of a button adds 30 seconds to a timer, and when it get to 0, does the same stuff.
I'm a little new, but I thought this would be straight forward. I got the photoresistor part just fine, and at the push of a button, it displays a 30 second timer that counts down to 0.
However the alarm, agitate(), does not go off when it reaches zero and I have no idea if I'm missing something simple or am actually doing it wrong. Starts about 5 lines into void loop:
#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd(12,11,5,4,3,2);
Servo servo;
int sensorValue;
int speakerPin = 8;
int switchState = 0;
double timer = 0;
boolean switchClosed = false;
boolean alarmOn = false;
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
servo.attach(9);
pinMode(7, INPUT);
}//end setup
void loop() {
sensorValue = analogRead(A0);
//Serial.println(sensorValue);
switchState = digitalRead(7);
//SETTING BY SWITCH//
if(switchState == HIGH){
switchClosed = true;
timer += 30;
}
if(switchClosed == true){
for(int i = timer; i > 0; i--){
timer--;
delay(1000);
lcd.setCursor(4,0);
lcd.print(timer);
}
alarmOn = true;
switchClosed = false;
}
//PHOTORESISTOR//
if(sensorValue > 100){
alarmOn = true;
}
if(sensorValue < 100){
alarmOn = false;
noTone(speakerPin);
}
if (alarmOn == true){
agitate();
}
}//end loop
void agitate(){
tone(speakerPin, 392);
servo.write(0);
delay(1000);
servo.write(150);
delay(1000);
}