Dear Arduino community,
[Arduino Uno Rev 3, Arduino IDE 1.0.5]
I have a sketch (below) that is intended to be a countdown timer. The timer takes advantage of the millis() function to count down a period of time which is assigned to a variable called “interval.” The interval is set using input from a 4-key keypad. The serial monitor shows what the variable is assigned to.
When the sketch is uploaded and I press a key on the keypad, the serial monitor correctly shows the assigned variable, but only temporarily. On the next pass through the sketch, it is re-assigned to a different value, and I can’t figure out why. I need the assigned variable (interval) to keep its value until the timer is up.
For example, the serial monitor will show:
The timer interval is set to 20 seconds. //I don’t know where this value is coming from, but it persists even after powerdown
The timer interval is set to 20 seconds.
The timer interval is set to 60 seconds. //I hit button 3 on the keypad
The timer interval is set to 20 seconds. //but now it goes back to its previous value
The timer interval is set to 20 seconds.
The timer interval is set to 40 seconds. //I hit button 2 on the keypad
The timer interval is set to 20 seconds. //but it goes back to 20 again!
The timer interval is set to 20 seconds.
etc.
If someone can see why the variable “interval” is not keeping its value, please elaborate. Thanks for reading!
/*
This sketch is a countdown timer that takes advantage of the millis() function.
The timer interval is set using a keypad. When the current millis exceeds the
set interval, then an alarm goes off.
Circuit utilizes a 4-key keypad (5 wires--one for each key plus GND) in a breadboard.
Arduino GND is connected to ground wire on keypad and the pins 2, 3, 4, and 5
on the Arduino are connected to the wires on the keypad.
*/
void setup()
{
//start serial connection
Serial.begin(9600);
//configure pins 2,3,4 and 5 as an inputs and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
int ledPin=13;
pinMode(ledPin, OUTPUT);
}
void loop()
{
long interval; //long is used here because it will be compared to millis, another long.
int i=0; //counter variable in for loop
int ledPin=13;
//read the pushbutton value and assign it to a variable
int sensorVal2 = digitalRead(2); //sensorValX is which button (1-4) on the keypad is pressed
int sensorVal3 = digitalRead(3);
int sensorVal4 = digitalRead(4);
int sensorVal1 = digitalRead(5);
long currentMillis = millis();
//Assign the keypad inputs to an interval time for the timer.
if (sensorVal1==LOW) //input pullup inverts the logic. Closed circuit is low and open is high.
{interval=20000; //20 seconds
}
if (sensorVal2==LOW)
{interval=40000; //40 seconds
}
if (sensorVal3==LOW)
{interval=60000; //60 seconds
}
if (sensorVal4==LOW)
{interval=80000; //80 seconds
}
Serial.print("The timer interval is set to "); //serial monitor shows what the timer is set to
Serial.print(interval/1000);
Serial.println(" seconds.");
//Serial.println(currentMillis);
//Check to see if time is up!
if(currentMillis > interval) //If true, trigger the alarm
{
for(i=0; i<4; i++)
{
digitalWrite(ledPin, HIGH);
delay (1000);
digitalWrite(ledPin, LOW);
delay (1000);
}
}
delay(1000); //The delay slows down the serial monitor updates but does not affect the millis() timer
}