Continue due to size limit...
void stopwatch_time_counter()
{
static unsigned long elapsed_time = 0;
static unsigned long last_read;
static unsigned char is_ticking = 0;
static unsigned char top_is_pressed = 0;
static unsigned char bottom_is_pressed = 0;
static unsigned char last_seconds = 0;
int button = 0;
while (button != btnSELECT)
{
unsigned long current_time = millis();
if(is_ticking)
{
elapsed_time += current_time - last_read;
}
last_read = current_time;
button = read_LCD_buttons();
if(button == btnUP)
{
if(!top_is_pressed)
{
// reset
top_is_pressed = 1;
is_ticking = 0;
elapsed_time = 0;
timedBeep(shortBeep,1);
//wait_for_button_release(TOP_BUTTON);clear();// tmphax to make this work on org06a01
lcdClear();
lcd.print("Stop Watch Timer");
}
}
else
{
top_is_pressed = 0;
}
if(button == btnDOWN)
{
if(!bottom_is_pressed)
{
// start/stop
bottom_is_pressed = 1;
is_ticking = !is_ticking;
timedBeep(shortBeep,1);
}
}
else
{
bottom_is_pressed = 0;
}
lcd.setCursor(0,1);
lcd.print(elapsed_time/1000/60/60/10%10); // tens of hours
lcd.print(elapsed_time/1000/60/60%10); // hours
lcd.print(":");
lcd.print(elapsed_time/1000/60/10%10%6); // tens of minutes
lcd.print(elapsed_time/1000/60%10); // minutes
lcd.print(":");
lcd.print(elapsed_time/1000%60/10); // tens of seconds
unsigned char seconds = elapsed_time/1000%60%10;
lcd.print(seconds);
lcd.print(".");
lcd.print(elapsed_time/100%10); // tenths of seconds
lcd.print(elapsed_time/10%10); // hundredths of seconds
// beep every second
if(seconds != last_seconds && elapsed_time != 0)
{
//timedBeep(shortBeep,1);
}
last_seconds = seconds;
}
timerCancelled("StopWatch");
}
void setAlarm()
{
int button = 0;
// Pass maxCount to getTimerMinutes
alarmHours = getTimerMinutes("Set Alarm Hour", alarmHours, 23);
// Validate alarm hours > 0 and < 24
if (alarmHours >= 0 && alarmHours < 24)
{
// Pass maxCount to getTimerMinutes
alarmMinutes = getTimerMinutes("Set Minutes", alarmMinutes, 59);
// allow alarm minutes to be 0
if (alarmMinutes < 60)
{
lcdClear();
lcd.setCursor(0,1);
//display alarm time
lcd.print(alarmHours);
lcd.print(":");
if (alarmMinutes < 10)
lcd.print("0");
lcd.print(alarmMinutes);
if (button == btnRIGHT)
{
timedBeep(shortBeep,1);
alarmSet = true;
lcd.setCursor(0,0);
lcd.print("Alarm Set for");
delay(1000);
return;
}
else
{
timerCancelled("Alarm");
return;
}
}
else
{
timerCancelled("Alarm");
}
}
else
{
timerCancelled("Alarm");
}
}
// setDateTime feature
void setDateTime()
{
int button = 0;
//get month
int setMonth = getTimerMinutes("Set Month", lastMonth, 12);
if (setMonth > 0 && setMonth < 13)
{
//get day
// default day and hour settings on set date/time
int setDay = getTimerMinutes("Set Day", lastDay, 31);
if (setDay > 0 && setDay < 32)
{
//get year
int setYear = getTimerMinutes("Set Year", lastYear, 2999);
if (setYear > 2000 && setYear < 3000)
{
//get hour
int thisHour = lastHour;
// default day and hour settings on set date/time
int setHour = getTimerMinutes("Set Hour", thisHour, 23);
if (setHour >= 0 && setHour < 24)
{
//get minutes
int setMinute = getTimerMinutes("Set Minute", lastMinute, 59);
if (setMinute < 60)
{
// RTC.adjust(DateTime(setYear,setMonth,setDay,setHour,setMinute)); // for DS1307
setTime(setHour, setMinute, 0, setDay, setMonth, setYear); // sec, min, hour, date, month, year
lcd.setCursor(0,0);
lcd.print("Saving... ");
delay(1000);
return;
}
else
{
timerCancelled("");
return;
}
}
else
{
timerCancelled("");
}
}
else
{
timerCancelled("");
}
}
else
{
timerCancelled("");
}
}
else
{
timerCancelled("");
}
}
// read the buttons from LCD keypad shield
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 131, 307, 481, 722
// we add approx 50 to those values and check to see if we are close
// No button pressed should be 1023
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void timedCountDown(int secondCount, char countLabel[])
{
long seconds = 0;
long minutes = 0;
lcdClear();
lcd.print(countLabel);
for (int i = secondCount; i >= 0; i--)
{
seconds = i;
minutes = i / 60;
if (minutes > 0)
{
seconds = seconds - (minutes * 60);
}
if (minutes > 0)
{
lcd.setCursor(0,1);
lcd.print(minutes);
lcd.print(" min ");
}
else
{
lcd.setCursor(0,1);
}
if (seconds < 10) lcd.print("0");
lcd.print(seconds);
lcd.print(" sec remaining");
if (seconds > 0) delay(1000);
if (read_LCD_buttons() == btnSELECT) //cancel
{
timerCancelled("Timer");
i = 0;
return;
}
}
lcd.setCursor(6,1);
timedBeep(longBeep,3);
}
// Pass maxCount to getTimerMinutes
int getTimerMinutes(char timerText[], int startNum, int maxCount)
{
int minutes = startNum;
int button = 0;
lcdClear();
lcd.print(timerText);
lcd.setCursor(0,1);
lcd.print(minutes);
while (button != btnSELECT)
{
button = read_LCD_buttons();
Serial.println(button);
if (button == btnLEFT)
{
if ((minutes + 10) <= maxCount)
{
timedBeep(shortBeep,1);
minutes = minutes + 10;
}
else
{
timedBeep(shortBeep,2);
}
}
if (button == btnUP)
{
if (minutes < maxCount)
{
timedBeep(shortBeep,1);
minutes++;
}
else
{
timedBeep(shortBeep,2);
minutes = 0;
}
}
if (button == btnDOWN)
{
if (minutes > 0)
{
timedBeep(shortBeep,1);
minutes--;
}
else
{
timedBeep(shortBeep,2);
minutes = maxCount;
}
}
if (button == btnRIGHT)
{
timedBeep(shortBeep,1);
return minutes;
}
lcd.setCursor(0,1);
lcd.print(minutes);
lcd.print(" ");
}
return 0;
}
void timedBeep(int beepTime, int beepCount)
{
for (int i = 0; i < beepCount; i ++)
{
digitalWrite(beeper, HIGH);
delay(beepTime);
digitalWrite(beeper, LOW);
delay(beepTime);
}
}
void lcdClear(){
lcd.clear();
lcd.begin(16,2);
lcd.setCursor(0,0);
}
void timerCancelled(char message[])
{
lcdClear();
lcd.print(message);
lcd.print(" Cancelled");
timedBeep(shortBeep,3);
}
void setOffAlarm()
{
int button = 0;
int i = 0;
Serial.println(i);
digitalWrite(backLight, HIGH); // turn backlight on
while (button != btnSELECT)
{
button = read_LCD_buttons();
lcdClear();
i++;
if (i > 50)
{
lcdClear();
lcd.print("Alert Alert");
lcd.setCursor(0,1);
lcd.print(" Alert Alert");
i = 0;
timedBeep(shortBeep,3);
}
}
timerCancelled("Alarm");
alarmSet = false;
}