Alright, so I'm attempting to make a timer that will turn on a light after an hour. Currently I have it set to 5 seconds for testing. What is happening is whenever I start the program it won't recognize when the timer reaches 0. Here is my code
/*
Hour Alarm
An hour long alarm that allows breaks
Created by: Jaxon Reid
Last Edited: 19/5/2017
Pins:
Pin 13 = Led
Pin 2 = Button
*/
int secLeft=5;
const int led=13;
const int button=2;
const int buttonIn=4;
int buttonValue=0;
// Declares the button to reset as 2 and the lightbulb on pin 13
//the secLeft means the seconds until the light turns on and button value is for wether or not the button is pushed
void setup() {
Serial.begin(2400);
Serial.print("On ");
pinMode(13,OUTPUT);
pinMode(button,OUTPUT);
pinMode(buttonIn,INPUT);
}
// makes the led pin an input and opens the channel for the debugger
void loop() {
int buttonValue=digitalRead(button);
Serial.println("Checking time");
if (secLeft<=0) {
Serial.println("No time remaining");
digitalWrite(13,HIGH);
Serial.println("Light On");
// If the time remaining is 0 then turn on the light
}
if (secLeft>0) {
Serial.println("Time Remaining");
digitalWrite(led,LOW);
secLeft=(secLeft--);
Serial.println(secLeft);
}
if (buttonValue==1) {
int secLeft=5;
Serial.println("Reset Timer");
Serial.println(secLeft);
}
delay(1000);
}
Whenever I start it up it just starts reading
On 5
Checking time
Time remaining
5
Checking time
Time remaining
4
Checking time
Time remaining
3
Checking time
Time remaining
2
Checking time
Time remaining
1
Checking time
Time remaining
0
Checking time
Time remaining
-1
Checking time
Time remaining
-2
etc... So where did I go wrong is what I'm asking. Thanks!
int secLeft=(secLeft2--);where you are creating a second variable calledsecLeftwithin the scope of theifblock. Ditch theint. This is the second question I have seen with this issue in the last 30 minutes. You probably also have the same issue withsecLeft2int secLeftin the reset section