EDIT: This has been solved already
Hi
I´m having trouble with this kitchen timer sketch of mine, any help is appreciated. (I´m using Arduino Uno r3, 1x8 lcd display in 8-bit mode and 3 pushbuttons connected from A0 / A1 / A2 to ground.)
[i]// For 1x16 display, 8 bit mode
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 9, 8, 7, 6, 5, 4, 3, 2);
unsigned long startTime = 0;
int duration = 0; // how long a key has been kept pressed (ms)
int seconds = 0;
int minutes = 0;
byte reading = 0;
byte keys = 15;
byte flagCntr = 0;
boolean served = false; // indicates that a function has been served
// and we need to wait for the key to be released
volatile boolean flag = false; // indicates that 1/4 second has passed
boolean eggRunning = false;
void setup()
{
pinMode(A0, INPUT_PULLUP); // A0,14
pinMode(A1, INPUT_PULLUP); // A1,15
pinMode(A2, INPUT_PULLUP); // A2,16
pinMode(A3, INPUT_PULLUP); // A3,17
// set up Timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 15625; // compare match register 16MHz/256/15625=4Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
lcd.begin(16, 1); // set up the LCD's number of columns and rows:
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
flag = true;
}
void loop()
{
if(flag) // if 1/4 seconds has passed
{
flagCntr++;
if(flagCntr == 4) // if a second has passed
{
flagCntr = 0;
if(eggRunning) // and egg timer is running
{
subTime(1);
if(seconds == 0 && minutes == 0) // update egg timer and check
// if it has finished
{
eggRunning = false;
lcd.setCursor(0, 0);
lcd.print("Done!");
delay(2000);
}
}
}
}
reading = (PINC & B1111); // check if keys are pressed
delay(50);
if((PINC & B1111) == reading) // if a valid reading
{
if(reading == 15) // if not pressed
{
keys = 15;
served = false;
duration = 0;
}
else
{
if(reading != keys) // if a new key
{
keys = reading;
startTime = millis();
}
else {duration = millis() - startTime;}
}
}
else
{
keys = 15;
duration = 0;
}
if(flag) // if 1/4 seconds has passed
{
if(!served && keys != 15)
{
switch(keys) // find and serve function
{
// case 7: Mode key (A3) (for future extension)
case 9: // Reset key (+ and - keys pressed simultaneously)
if(!eggRunning) // only serve reset, + and - keys if egg timer
// isnt running
{
seconds = 0;
minutes = 0;
served = true;
}
break;
case 11: // + key (A2)
if(!eggRunning)
{
if(duration > 7000)
addTime(125);
else if(duration > 5000)
addTime(25);
else if(duration > 3000)
addTime(5);
else if(duration > 1000 || duration < 250)
addTime(1);
}
break;
case 13: // - key (A1)
if(!eggRunning)
{
if(duration > 7000)
subTime(125);
else if(duration > 5000)
subTime(25);
else if(duration > 3000)
subTime(5);
else if(duration > 1000 || duration < 250)
subTime(1);
}
break;
case 14: // Enter key (A0)
if(seconds != 0 || minutes != 0) // if egg timer is "00:00"
// dont allow it to run
{
eggRunning = !eggRunning; // toggle egg timer start / stop
served = true;
}
break;
default:
served = true; // odd key combination detected,
// wait for keys to be released
}
}
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
if(minutes < 10){lcd.print(" ");}
lcd.print(minutes); // print time
lcd.print(":");
if(seconds < 10){lcd.print(0);} // add leading zero
lcd.print(seconds);
flag = false;
}
}
void addTime(int value)
{
seconds = (seconds + value);
if(seconds > 59)
{
minutes = (minutes + (seconds / 60));
seconds = (seconds % 60);
}
if(minutes > 99){minutes = (minutes % 100);}
}
void subTime(int value)
{
seconds = (seconds - value);
if(seconds < 0)
{
minutes = (minutes + ((seconds - 59)/ 60));
seconds = ((seconds % 60) + 60);
}
if(minutes < 0){minutes = ((minutes % 100) +100);}
}[/i]
Brief description of how it is supposed to work: First there is "0:00" on the display. By pressing + key (pushbutton connected to A2) or - key (connected to A1) you can increase or decrease the time on display. Pressing + and - keys simultaneously will reset the time back to "0:00". When + or - key is being kept pressed down the time on the display will increase (or decrease) on an increasing speed. Pressing Enter key (connected to A0) will start the timer counting down and pressing again will stop it. While the timer is counting down you can´t adjust the time because the + and - keys are ignored. When the timer reaches zero "Done!" is printed on the display.
What is wrong with it: It lets me to adjust the time on the display just fine, but when I press the Enter key the timer doesnt count down. It blocks the + and - keys tough, unless the time is on 0:00 and pressing the Enter key again unblocks the + and - keys, just like it´s supposed to, so the eggRunning flag seems to work at least.(Could be something obvious wrong with it, but I just can´t seem to spot it.)
Also a second question: What do you think of the code ? For example is it overly complicated ?