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!
3 Answers 3
As per comment and Majenko's answer you are current creating local variables called secLeft
and secLeft2
inside the 'if
block. These take precedence over the ones you have defined globally. Removing the in
declarations from inside the if
block will stop doing that and point you at the globally defined variables.
As to the rest of the code. If you have
secLeft=(secLeft2--);
secLeft2=(secLeft);
Then that translates to this sequence of instructions
- Assign
secLeft
with the value ofsecLeft2
- Decrement
secLeft2
- Assign
secLeft2
with the value ofsecLeft
And in the end you do not change anything.
This is because you are using post-decrement
on secLeft2
EG secLeft2--
. This is a feature of C style languages.
I am not sure what you are even trying to do at this point.
-
Got it thankyou I was thinking it would decrement beforehandJayjaxx– Jayjaxx2017年06月17日 22:27:16 +00:00Commented Jun 17, 2017 at 22:27
-
To get
pre-decrement
you have to do--secLeft
. C and C++ have both and they are both useful in their own way.Peter M– Peter M2017年06月17日 22:27:48 +00:00Commented Jun 17, 2017 at 22:27
int secLeft=(secLeft2--);
int secLeft2=(secLeft);
You are making 2 new variables there instead of using the existing ones.
-
alright how do i fix that?Jayjaxx– Jayjaxx2017年06月17日 22:14:28 +00:00Commented Jun 17, 2017 at 22:14
-
By not declaring new variables.Majenko– Majenko2017年06月17日 22:15:09 +00:00Commented Jun 17, 2017 at 22:15
-
I have picked this up yesterday i learned C++ yesterday aswell what do i remove?Jayjaxx– Jayjaxx2017年06月17日 22:16:17 +00:00Commented Jun 17, 2017 at 22:16
-
Remove the
int
from each line. Also, do you really want to reset secLeft2 to what it was before decrementing it? The second line looks a bit daft.Majenko– Majenko2017年06月17日 22:17:41 +00:00Commented Jun 17, 2017 at 22:17 -
Now i just get 5,5,5,5,5 ...Jayjaxx– Jayjaxx2017年06月17日 22:19:32 +00:00Commented Jun 17, 2017 at 22:19
Besides problems mentioned in other answers, your use of button
and buttonIn
variables is messed up.
With button
equal to 2, the pinMode(button,OUTPUT)
statement initializes pin 2 to be an output. Pin 2 is low after reset and pinMode()
doesn't change it.
When you say int buttonValue=digitalRead(button);
the low value at pin 2 will be read into buttonValue
. In the sketch, buttonValue
will never be anything but low.
To fix this problem, get rid of buttonIn
, and in setup()
use pinMode(button,INPUT)
to set button
to be an input, not an output.
int secLeft=(secLeft2--);
where you are creating a second variable calledsecLeft
within the scope of theif
block. 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 withsecLeft2
int secLeft
in the reset section