Skip to content

Instantly share code, notes, and snippets.

@AnthonyDiGirolamo
Created December 5, 2011 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnthonyDiGirolamo/1434864 to your computer and use it in GitHub Desktop.
Save AnthonyDiGirolamo/1434864 to your computer and use it in GitHub Desktop.
arduino powered kitchen timer with extras
#include <avr/pgmspace.h>
#include <math.h>
#include <LiquidCrystal.h>
#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC
#define LED1 3
#define LED2 5
#define LED3 11
#define LED4 6
#define LED5 10
#define LED6 9
prog_uint8_t leds[] = {3,5,11,6,10,9};
uint8_t led_values[] = {0,0,0,0,0,0};
int8_t light_mode = 0;
int8_t sound_mode = 0;
LiquidCrystal lcd(13, 2, 7, 8, 12, 4);
prog_uint8_t custchar[8][8] = {
{
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000
}, {
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111
}, {
B11111,
B11111,
B11111,
B00000,
B00000,
B11111,
B11111,
B11111
}, {
B00000,
B00000,
B00000,
B00000,
B00000,
B01110,
B01110,
B01110
}, {
B00000,
B00000,
B00000,
B01110,
B01110,
B01110,
B00000,
B00000
}, {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
}, {
B01000,
B00100,
B00010,
B11111,
B00010,
B00100,
B01000,
B00000
}, {
B01100,
B10010,
B10010,
B01100,
B00000,
B00000,
B00000,
B00000
}
};
prog_uint8_t bignums[10][2][3] = {
{
{255, 0, 255},
{255, 1, 255}
},{
{0, 255, 254},
{1, 255, 1}
},{
{2, 2, 255},
{255, 1, 1}
},{
{0, 2, 255},
{1, 1, 255}
},{
{255, 1, 255},
{254, 254, 255}
},{
{255, 2, 2},
{1, 1, 255}
},{
{255, 2, 2},
{255, 1, 255}
},{
{0, 0, 255},
{254, 255, 254}
},{
{255, 2, 255},
{255, 1, 255}
},{
{255, 2, 255},
{254, 254, 255}
}
};
void load_custom_characters() {
lcd.command(64);
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
lcd.write(custchar[i][j]);
lcd.home();
}
void print_big_char(byte digit, byte col, byte row, byte symbol = 0) {
if (digit > 9) return;
for (int i = 0; i < 2; i++) {
lcd.setCursor(col, row + i);
for (int j = 0; j < 3; j++) {
lcd.write(bignums[digit][i][j]);
}
lcd.write(254);
}
if (symbol == 1) {
lcd.setCursor(col + 3, row + 1);
lcd.write(3);
} else if (symbol == 2) {
lcd.setCursor(col + 3, row);
lcd.write(4);
lcd.setCursor(col + 3, row + 1);
lcd.write(4);
}
lcd.setCursor(col + 4, row);
}
void print_big_temp(float number) {
int digit1 = (int) (number / 10 ) % 10;
int digit2 = (int) (number ) % 10;
int digit3 = (int) round(number * 10 ) % 10;
//int digit4 = (int) round(number * 100) % 10;
print_big_char(digit1, 0, 0); // 100's place
print_big_char(digit2, 4, 0, 1); // 10's place, plus decimal
print_big_char(digit3, 8, 0); // 10th's place
//print_big_char(digit4, 12, 0); // 100th's place
}
float temp_c() {
int sensor;
float volts, celsius, farenheit;
sensor = analogRead(3);
volts = sensor * 5.0 / 1024.0; // convert AD units to volts
celsius = volts / 0.02; // convert volts to celsius
return celsius;
}
float temp_to_f(float celsius) {
return (celsius * 9.0 / 5) + 32;
}
int8_t read_encoder() {
// returns change in encoder state (-1,0,1)
static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static uint8_t old_AB = 0;
old_AB <<= 2; //remember previous state
old_AB |= ( ENC_PORT & 0x03 ); //add current state
return ( enc_states[( old_AB & 0x0f )]);
}
int get_rotary_input() {
byte done = 0;
int8_t rotary_changes = 0;
static uint8_t last_counter = 0;
static uint8_t counter = 0; //this variable will be changed by encoder input
int8_t tmpdata;
int r = 0;
while (!done) {
if (any_button_pressed()) {
done = 1;
break;
}
last_counter = counter;
tmpdata = read_encoder(); // Check Encoder
if( tmpdata ) {
counter += tmpdata;
rotary_changes++;
}
if ( rotary_changes >= 4 ) {
if (counter > last_counter)
r = -1;
else
r = 1;
if (abs(counter-last_counter) > 10)
r = r*-1;
last_counter = counter;
rotary_changes = 0;
done = 1;
}
}
return r;
}
int any_button_pressed() {
if (analogRead(2) > 50)
return 1;
else
return 0;
}
int read_button() {
int b,c = 0;
c = analogRead(2); // get the analog value
delay(30);
if (c < 50)
b = 0;
else if (c>150 && c<250)
b = 1;
else if (c>350 && c<450)
b = 2;
else if (c>550 && c<650)
b = 3;
else if (c>750 && c<850)
b = 4;
else
b = 0;
return b;
}
void led_write(int i[]) {
analogWrite(LED1, i[0]*8);
analogWrite(LED2, i[1]*8);
analogWrite(LED3, i[2]*8);
analogWrite(LED4, i[3]*8);
analogWrite(LED5, i[4]*8);
analogWrite(LED6, i[5]*8);
}
void led_on() {
analogWrite(LED1, 255);
analogWrite(LED2, 255);
analogWrite(LED3, 255);
analogWrite(LED4, 255);
analogWrite(LED5, 255);
analogWrite(LED6, 255);
}
void led_off() {
analogWrite(LED1, 0);
analogWrite(LED2, 0);
analogWrite(LED3, 0);
analogWrite(LED4, 0);
analogWrite(LED5, 0);
analogWrite(LED6, 0);
}
void led_toggle() {
for (int i=0; i<6; i++) {
if (led_values[i] != 0)
led_values[i] = 0;
else
led_values[i] = 255;
analogWrite(leds[i], led_values[i]);
}
}
void led_cylon(int cycles = 1) {
int cylon_animation[10][6] = {{2,4,8,0,0,0},
{0,2,4,8,0,0},
{0,0,2,4,8,0},
{0,0,0,2,4,8},
{0,0,0,0,8,4},
{0,0,0,8,4,2},
{0,0,8,4,2,0},
{0,8,4,2,0,0},
{8,4,2,0,0,0},
{4,8,0,0,0,0}};
for (int j=0; j<cycles; j++) {
for (int i=0; i<10; i++) {
led_write(cylon_animation[i]);
delay(50);
}
}
led_off();
}
void led_random() {
analogWrite(leds[random(6)], random(256));
}
void led_pulse(int cycles = 2) {
for (int j=0; j<cycles; j++) {
for (int i=0; i<32; i++) {
analogWrite(LED1, i);
analogWrite(LED2, i);
analogWrite(LED3, i);
analogWrite(LED4, i);
analogWrite(LED5, i);
analogWrite(LED6, i);
delay(30);
}
for (int i=31; i>=0; i--) {
analogWrite(LED1, i);
analogWrite(LED2, i);
analogWrite(LED3, i);
analogWrite(LED4, i);
analogWrite(LED5, i);
analogWrite(LED6, i);
delay(30);
}
}
led_off();
}
char buffer[64];
char string_mode_title[] PROGMEM = "Mode:";
char string_mode_temp[] PROGMEM = "Temperature";
char string_mode_temp_short[] PROGMEM = "Temp ";
char string_mode_timer[] PROGMEM = "Timer";
char string_mode_stopwatch[] PROGMEM = "Stop Watch";
char string_mode_stopwatch1[] PROGMEM = "Stop";
char string_mode_stopwatch2[] PROGMEM = "Watch";
char string_mode_lights[] PROGMEM = "Light Setting";
char string_mode_lights1[] PROGMEM = "Mode: ";
char string_light_mode0[] PROGMEM = "off";
char string_light_mode1[] PROGMEM = "on";
char string_light_mode2[] PROGMEM = "cylon";
char string_light_mode3[] PROGMEM = "pulse";
char string_mode_sound[] PROGMEM = "Sound Setting";
char string_mode_sound1[] PROGMEM = " Song: ";
char string_rotarty_set1[] PROGMEM = " Turn the Dial ";
char string_rotarty_set2[] PROGMEM = "Press any Button";
char string_font_size_title[] PROGMEM = "Font Size: ";
char string_font_size_large[] PROGMEM = "Large";
char string_font_size_small[] PROGMEM = "Small";
char string_temp_unit[] PROGMEM = "Unit: ";
char string_temp_farenheit[] PROGMEM = "Farenheit";
char string_temp_celsius[] PROGMEM = "Celsius";
char string_paused[] PROGMEM = "Paused ";
char string_set[] PROGMEM = "Setting ";
char string_running[] PROGMEM = "Running ";
char string_ready[] PROGMEM = "Ready ";
void lcd_print(char* string, int col = -1, int row = -1) {
if (col >= 0 && row >= 0)
lcd.setCursor(col, row);
strcpy_P(buffer, string);
lcd.print(buffer);
}
#define MODE_TEMP 0
#define MODE_TIMER 1
#define MODE_STOPWATCH 2
#define MODE_LIGHTS 3
#define MODE_SOUND 4
#define CHANGE_DELAY 1016
#define UPDATE_DELAY 976
// Variables
int8_t mode = MODE_TEMP;
unsigned long startup_time;
int8_t button;
int8_t last_button;
unsigned long last_update = 0;
int8_t large_font = 0;
int8_t farenheit_unit = 1;
float current_temp;
unsigned long timer_setting = 120;
unsigned long timer_start_time;
// Startup With Timer Paused
bool timer_running = 1;
bool timer_paused = 1;
unsigned long timer_elapsed = 0;
bool stopwatch_running = 0;
bool stopwatch_paused = 0;
unsigned long stopwatch_start_time;
unsigned long stopwatch_elapsed;
int8_t r;
int8_t octave_mode = 0;
prog_int16_t noteFreqArr[] = {
49.4, 52.3, 55.4, 58.7, 62.2, 65.9, 69.9, 74, 78.4, 83.1, 88, 93.2,
98.8, 105, 111, 117, 124, 132, 140, 148, 157, 166, 176, 186,
198, 209, 222, 235, 249, 264, 279, 296, 314, 332, 352, 373,
395, 419, 444, 470, 498, 527, 559, 592, 627, 665, 704, 746,
790, 837, 887, 940, 996, 1050, 1110, 1180, 1250, 1320, 1400, 1490,
1580, 1670, 1770, 1870, 1990, 2100};
//F5 E5 D#5 D5 C#5 C5 B4 A#4 A4 G#4 G4 F#4
//F4 E4 D#4 D4 C#4 C4 B3 A#3 A3 G#3 G3 F#3
//F3 E3 D#3 D3 C#3 C3 B2 A#2 A2 G#2 G2 F#2
//F2 E2 D#2 D2 C#2 C2 B1 A#1 A1 G#1 G1 F#1
//F1 E1 D#1 D1 C#1 C1 B0 A#0 A0 G#0 G0 F#0
//F0 E0 D#0 D0 C#0 C0
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2;
// calculate the delay value between transitions 1 second's worth of
// microseconds, divided by the frequency, then split in half since there
// are two phases to each cycle
long numCycles = frequency * length/ 1000;
// calculate the number of cycles for proper timing multiply frequency,
// which is really cycles per second, by the number of seconds to get the
// total number of cycles to produce
// for the calculated length of time...
for (long i=0; i < numCycles; i++){
// write the buzzer pin high to push out the diaphram
digitalWrite(A4,HIGH);
// wait for the calculated delay value
delayMicroseconds(delayValue);
// write the buzzer pin low to pull back the diaphram
digitalWrite(A4,LOW);
// wait again or the calculated delay value
delayMicroseconds(delayValue);
}
}
void playNote(long noteInt, long length, int8_t octave_mode, long breath = 25) {
length = length - breath;
long noteInt2 = noteInt + 12; //1 octave up
long noteInt3 = noteInt + 24; //2 octaves up
long noteInt4 = noteInt + 36; //3 octaves up
long playLoop = length / 100; //divide length by 4, for use in play sequence
if(octave_mode == 0) { //octave_mode 0 sequence
for (long i=0; i < playLoop; i++){
buzz(4, noteFreqArr[noteInt], 20);
delay(5);
buzz(4, noteFreqArr[noteInt2], 20);
delay(5);
buzz(4, noteFreqArr[noteInt3], 20);
delay(5);
buzz(4, noteFreqArr[noteInt4], 20);
delay(5);
}
} else if(octave_mode == 1) { //octave_mode 1 sequence
for (long i=0; i < playLoop; i++){
buzz(4, noteFreqArr[noteInt3], 20);
delay(5);
buzz(4, noteFreqArr[noteInt4], 20);
delay(5);
buzz(4, noteFreqArr[noteInt3], 20);
delay(5);
buzz(4, noteFreqArr[noteInt4], 20);
delay(5);
}
} else if(octave_mode == 2) { //octave_mode 2 sequence
for (long i=0; i < playLoop; i++){
buzz(4, noteFreqArr[noteInt3], 20);
delay(5);
buzz(4, noteFreqArr[noteInt3], 20);
delay(5);
buzz(4, noteFreqArr[noteInt3], 20);
delay(5);
buzz(4, noteFreqArr[noteInt2], 20);
delay(5);
}
} else if(octave_mode == 3) { //octave_mode 3 sequence
for (long i=0; i < playLoop; i++){
buzz(4, noteFreqArr[noteInt4], 40);
delay(5);
buzz(4, noteFreqArr[noteInt2], 40);
delay(5);
}
}
if(breath > 0) { //take a short pause or 'breath' if specified
delay(breath);
}
}
void play_can_can(int start_octave_mode = 0, int cycles = 4) {
octave_mode = start_octave_mode;
for (int i=0; i<cycles; i++) {
//main course
playNote(12, 500, octave_mode);
playNote(5, 1000, octave_mode);
playNote(7, 250, octave_mode);
playNote(10, 250, octave_mode);
playNote(9, 250, octave_mode);
playNote(7, 250, octave_mode);
playNote(12, 500, octave_mode);
playNote(12, 500, octave_mode);
playNote(12, 250, octave_mode);
playNote(14, 250, octave_mode);
playNote(9, 250, octave_mode);
playNote(10, 250, octave_mode);
playNote(7, 500, octave_mode);
playNote(7, 500, octave_mode);
playNote(7, 250, octave_mode);
playNote(10, 250, octave_mode);
playNote(9, 250, octave_mode);
playNote(7, 250, octave_mode);
playNote(5, 250, octave_mode);
playNote(17, 250, octave_mode);
playNote(16, 250, octave_mode);
playNote(14, 250, octave_mode);
playNote(12, 250, octave_mode);
playNote(10, 250, octave_mode);
playNote(9, 250, octave_mode);
playNote(7, 250, octave_mode);
playNote(5, 1000, octave_mode);
playNote(7, 250, octave_mode);
playNote(10, 250, octave_mode);
playNote(9, 250, octave_mode);
playNote(7, 250, octave_mode);
playNote(12, 500, octave_mode);
playNote(12, 500, octave_mode);
playNote(12, 250, octave_mode);
playNote(14, 250, octave_mode);
playNote(9, 250, octave_mode);
playNote(10, 250, octave_mode);
playNote(7, 500, octave_mode);
playNote(7, 500, octave_mode);
playNote(7, 250, octave_mode);
playNote(10, 250, octave_mode);
playNote(9, 250, octave_mode);
playNote(7, 250, octave_mode);
playNote(5, 250, octave_mode);
playNote(12, 250, octave_mode);
playNote(7, 250, octave_mode);
playNote(9, 250, octave_mode);
playNote(5, 250, octave_mode);
delay(250);
octave_mode++;
if (octave_mode>3)
octave_mode=0;
}
}
//There, the notes are A4, B4, G4, G3, D4.
// G A F octave lower F C
// Bb C Ab octave lower Ab Eb
void play_close_encounters(int start_octave_mode = 0, int cycles = 4) {
int o=0;
for (int i=0; i<10; i++) {
led_off();
analogWrite(LED4, 255);
buzz(4, noteFreqArr[43], 250);
delay(150);
led_off();
analogWrite(LED6, 255);
buzz(4, noteFreqArr[53], 250);
delay(150);
led_off();
analogWrite(LED5, 255);
buzz(4, noteFreqArr[45], 300);
delay(300);
led_off();
analogWrite(LED1, 255);
buzz(4, noteFreqArr[33], 450);
delay(300);
led_off();
analogWrite(LED2, 255);
buzz(4, noteFreqArr[38], 650);
delay(3000);
}
//data 0,(23,25,21,5,144,255) ' main tune
//data (161,33,207,97,97,97,97,255) ' response 1
//data (74,80,72,104,195,255) ' tune 2
//data (161,97,97,97,97,97,98,15,33,255)' response 2
//data (87,89,21,229,128,255) ' ship joins in
//data (23,25,21,165,128,255) ' final phrase
/*
for (int i=0; i<cycles; i++) {
buzz(4, noteFreqArr[10+o], 250);
delay(200);
buzz(4, noteFreqArr[8+o], 250);
delay(200);
buzz(4, noteFreqArr[12+o], 300);
delay(400);
buzz(4, noteFreqArr[0+o], 450);
delay(400);
buzz(4, noteFreqArr[5+o], 650);
delay(3000);
o+=12;
}
*/
/*
o=24;
for (int i=0; i<1; i++) {
buzz(4, noteFreqArr[19+o], 250);
delay(150);
buzz(4, noteFreqArr[29+o], 250);
delay(150);
buzz(4, noteFreqArr[21+o], 300);
delay(300);
buzz(4, noteFreqArr[9+o], 450);
delay(300);
buzz(4, noteFreqArr[14+o], 650);
delay(3000);
}
*/
}
void set_light_mode(int8_t m, int8_t preview = 1) {
lcd_print(string_mode_lights, 0,0);
lcd_print(string_mode_lights1, 0,1);
if (m == 0) {
light_mode = 0;
lcd_print(string_light_mode0,6,1);
if (preview)
led_off();
}
else if (m == 1) {
light_mode = 1;
lcd_print(string_light_mode1,6,1);
if (preview)
led_on();
}
else if (m == 2) {
light_mode = 2;
lcd_print(string_light_mode2,6,1);
if (preview)
led_cylon(4);
}
else if (m == 3) {
light_mode = 3;
lcd_print(string_light_mode3,6,1);
if (preview)
led_pulse(2);
}
}
void switch_mode(int new_mode) {
char *mode_string;
switch(new_mode) {
case MODE_TEMP:
mode_string = string_mode_temp;
break;
case MODE_TIMER:
mode_string = string_mode_timer;
break;
case MODE_STOPWATCH:
mode_string = string_mode_stopwatch;
break;
case MODE_LIGHTS:
randomSeed(millis());
mode_string = string_mode_lights;
break;
case MODE_SOUND:
led_off();
mode_string = string_mode_sound;
break;
}
lcd.clear();
lcd_print(string_mode_title, 0, 0);
lcd_print(mode_string, 0, 1);
mode = new_mode;
delay(CHANGE_DELAY);
lcd.clear();
// Setup initial display if needed
switch(mode) {
case MODE_TEMP:
break;
case MODE_TIMER:
break;
case MODE_STOPWATCH:
break;
case MODE_LIGHTS:
set_light_mode(light_mode,0);
break;
case MODE_SOUND:
play_close_encounters(0,4);
break;
}
}
void setup() {
startup_time = millis();
//Serial.begin(115200);
// Rotary Encoder
pinMode(ENC_A, INPUT); digitalWrite(ENC_A, HIGH);
pinMode(ENC_B, INPUT); digitalWrite(ENC_B, HIGH);
pinMode(A4, OUTPUT); // Buzzer
pinMode(A3, INPUT); // Temp Sensor
pinMode(A2, INPUT); // Buttons
lcd.begin(16, 2);
load_custom_characters();
}
void loop() {
last_button = button;
button = read_button();
if (button != last_button) {
// Change Mode
if (button == 4) {
mode++;
if (mode > MODE_SOUND)
mode = MODE_TEMP;
switch_mode(mode);
}
// Toggle Font Size
else if (button == 3) {
if (mode == MODE_TEMP || mode == MODE_TIMER || mode == MODE_STOPWATCH) {
large_font = large_font? 0 : 1;
lcd.clear();
lcd_print(string_font_size_title);
if (large_font)
lcd_print(string_font_size_large, 0, 1);
else
lcd_print(string_font_size_small, 0, 1);
//Sleepy::loseSomeTime(2016);
delay(CHANGE_DELAY);
lcd.clear();
}
}
// Multi-Button #1
else if (button == 2) {
if (mode == MODE_TEMP) {
// Switch Units
lcd.clear();
if (mode == MODE_TEMP) {
lcd_print(string_mode_temp, 0,0);
lcd_print(string_temp_unit, 0,1);
if (farenheit_unit) {
farenheit_unit = 0;
lcd_print(string_temp_celsius);
}
else {
farenheit_unit = 1;
lcd_print(string_temp_farenheit);
}
}
else {
mode = MODE_TEMP;
lcd_print(string_mode_title, 0, 0);
lcd_print(string_mode_temp, 0, 1);
}
delay(CHANGE_DELAY);
lcd.clear();
}
else if (mode == MODE_TIMER) {
// Pause/Resume Timer
if (timer_running) {
if (timer_paused) {
// Resume
timer_start_time = millis() - timer_elapsed;
timer_paused = 0;
}
else {
// Pause
timer_elapsed = millis() - timer_start_time;
timer_paused = 1;
}
}
}
else if (mode == MODE_STOPWATCH) {
// Pause/Resume/Start Stopwatch
if (stopwatch_running) {
if (stopwatch_paused) {
// Resume
stopwatch_start_time = millis() - stopwatch_elapsed;
stopwatch_paused = 0;
}
else {
// Pause
stopwatch_elapsed = millis() - stopwatch_start_time;
stopwatch_paused = 1;
}
}
else {
// Start
stopwatch_start_time = millis();
stopwatch_running = 1;
}
}
else if (mode == MODE_LIGHTS) {
}
}
// Multi-Button #2
else if (button == 1) {
if (mode == MODE_TIMER) {
// Reset Timer
timer_running = 0;
timer_paused = 0;
// Prevent this button press from propagating to the
// timer setting mode and starting the timer again.
delay(500);
}
else if (mode == MODE_STOPWATCH) {
// Reset Stopwatch
stopwatch_running = 0;
stopwatch_paused = 0;
}
else if (mode == MODE_LIGHTS) {
light_mode++;
if (light_mode>3)
light_mode=0;
set_light_mode(light_mode);
}
}
}
// TEMP MODE UPDATE
if (mode == MODE_TEMP) {
if ((millis()-last_update > UPDATE_DELAY) || last_update==0) {
current_temp = temp_c();
if (large_font) {
if (farenheit_unit)
current_temp = temp_to_f(current_temp);
print_big_temp(current_temp);
lcd.setCursor(12,0);
lcd.write(7);
if (farenheit_unit)
lcd.write('F');
else
lcd.write('C');
}
else {
lcd_print(string_mode_title, 0,0);
lcd_print(string_mode_temp_short, 0,0);
lcd.setCursor(10,0);
lcd.print(temp_to_f(current_temp), 1);
lcd.write(7);
lcd.write('F');
lcd.setCursor(10,1);
lcd.print(current_temp, 1);
lcd.write(7);
lcd.write('C');
if (farenheit_unit) {
lcd.setCursor(7,0);
lcd.write('-');
lcd.write(6);
}
else {
lcd.setCursor(7,1);
lcd.write('-');
lcd.write(6);
}
}
last_update = millis();
}
}
// TIMER MODE
else if (mode == MODE_TIMER) {
if ((millis()-last_update > UPDATE_DELAY) || last_update==0) {
if (!large_font) {
lcd_print(string_mode_timer, 0,0);
}
last_update = millis();
}
if (timer_running) {
if (timer_paused) {
print_time(0, timer_elapsed, 8, 0);
if (!large_font)
lcd_print(string_paused, 8,1);
}
else {
print_time(timer_start_time, millis(), 8,0);
if (!large_font)
lcd_print(string_running, 8,1);
// Is the timer done?
if ( (millis()-timer_start_time) > (timer_setting*1000)-100 ) {
timer_running = 0;
led_cylon();
}
}
}
// Set the timer
else {
print_time(0, timer_setting*1000, 8,0);
if (!large_font) {
lcd_print(string_set, 0,0);
lcd_print(string_rotarty_set2, 0,1);
}
r = get_rotary_input();
if (r == 1) {
timer_setting+=30;
}
else if (r == -1) {
if (timer_setting > 30)
timer_setting-=30;
}
else if (r == 0) {
timer_start_time = millis();
timer_running = 1;
// Prevent the button press that exits
// timer setting mode from propagating.
delay(500);
lcd.clear();
}
}
}
// Stop Watch MODE
else if (mode == MODE_STOPWATCH) {
if ((millis()-last_update > UPDATE_DELAY) || last_update==0) {
if (!large_font) {
lcd_print(string_mode_stopwatch1, 0,0);
lcd_print(string_mode_stopwatch2, 0,1);
}
last_update = millis();
}
if (stopwatch_running) {
if (stopwatch_paused) {
print_time(0, stopwatch_elapsed, 6, 0);
if (!large_font)
lcd_print(string_paused, 6,1);
}
else {
print_time(stopwatch_start_time, millis(), 6, 0);
if (!large_font)
lcd_print(string_running, 6,1);
}
}
else {
print_time(0, 0, 6, 0);
if (!large_font)
lcd_print(string_ready, 6,1);
}
}
else if (mode == MODE_LIGHTS) {
//if ((millis()-last_update > UPDATE_DELAY) || last_update==0) {
//last_update = millis();
//}
}
else if (mode == MODE_SOUND) {
}
}
void print_time(unsigned long start, unsigned long end, int col, int row) {
char b[16];
unsigned long t;
if (mode == MODE_TIMER && timer_running)
t = timer_setting - ((end - start) / 1000);
else
t = ((end - start) / 1000);
word h = t / 3600;
byte m = (t / 60) % 60;
byte s = t % 60;
if (mode == MODE_STOPWATCH) {
sprintf(b, "%02u:%02u:%02u:%u", h, m, s, ((end-start)/100)%10);
}
else
sprintf(b, "%02u:%02u:%02u", h, m, s);
if (large_font) {
if (h > 0) {
print_big_char(b[0]-48, 0, 0);
print_big_char(b[1]-48, 4, 0, 2);
print_big_char(b[3]-48, 8, 0);
print_big_char(b[4]-48, 12, 0);
}
else {
print_big_char(b[3]-48, 0, 0);
print_big_char(b[4]-48, 4, 0, 2);
print_big_char(b[6]-48, 8, 0);
print_big_char(b[7]-48, 12, 0);
}
}
else {
lcd.setCursor(col, row);
lcd.print(b);
}
}
// vim: ft=cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment