Like a lot of post here I will start off that I am very new to Arduino. I am trying to get the the sketch below to run which is just a timer that outputs to a 16x2 LCD on a Uno. The code is unmodified from Timer.
It complies with no errors and uploads fine. It shows the sample text on the LCD and beeps but that is it. The original code shows it on an Arduino Leonardo but from what I read there is not that much different between the Leonardo and Uno. In the Arduino IDE if I change my board type to a Leonardo the code will upload and start the timer around 26 seconds and countdown to zero and restart the countdown over and over again. I spent 7 hours on this last night and made no progress maybe the code does not even work but being new to this I am not sure. I did learn a lot in 7 hours so not a total lose.
I attached an image of the fritzing breadboard that I am using. If anyone has any thoughts or even where to being to debug it I would be so grateful.
Thank you,
KB
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 9, 8, 7, 6);
byte char1[8] = { //the darkest
0b01111,
0b01111,
0b01111,
0b01111,
0b01111,
0b01111,
0b01111,
0b01111
};
byte char2[8] = {
0b00111,
0b00111,
0b00111,
0b00111,
0b00111,
0b00111,
0b00111,
0b00111
};
byte char3[8] = {
0b00011,
0b00011,
0b00011,
0b00011,
0b00011,
0b00011,
0b00011,
0b00011
};
byte char4[8] = {
0b00001,
0b00001,
0b00001,
0b00001,
0b00001,
0b00001,
0b00001,
0b00001
};
byte char5[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
byte char0[8] = { //the lightest
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
void setup()
{
lcd.begin(16, 2);
lcd.clear();
lcd.write("Sample Text");
lcd.setCursor(0,1);
lcd.write("Timer 1.0");
Serial.begin(9600);
lcd.createChar(0, char0);
lcd.createChar(1, char1);
lcd.createChar(2, char2);
lcd.createChar(3, char3);
lcd.createChar(4, char4);
lcd.createChar(5, char5);
pinMode(5,OUTPUT); //backlight
pinMode(13,OUTPUT); //led
pinMode(10, OUTPUT); //buzzer
pinMode(1,INPUT); // 4 buttons
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
digitalWrite(5,1); //backlight on
delay(1000);
lcd.clear();
signal(500);
}
int min=0;
int sec=0;
int button=0;
unsigned long mil;
unsigned long milisLeft=0;
unsigned long deadline;
unsigned long lastLight=millis();
void display()
{
lcd.setCursor(5,0);
if (min<10) lcd.write('0');
lcd.print(min);
lcd.write(':');
if (sec<10) lcd.write('0');
lcd.print(sec);
lcd.setCursor(11,0);
lcd.print(" ");
}
int whichButton()
{
for (int i=1; i<5; i++)
{
if (digitalRead(i)==1)
{
if (i!=4) signal(50);
lastLight=millis(); //setting the last time when clicked
return i;
}
}
}
void signal(int z)
{
tone(10,3000);
digitalWrite(13,1);
delay(z);
noTone(10);
digitalWrite(13,0);
}
void choosetime()
{
do {} while (digitalRead(button)==1);
button=0;
bool choice=1; //minutes
lcd.setCursor(4,0);
lcd.println(">");
do
{
display();
button=whichButton();
if (millis()-lastLight>15000) digitalWrite(5,0); //backlight
else digitalWrite(5,1);
if (button==1)
{
if (choice==1) //minutes
{
if (min>0) min--;
else min=99;
}
if (choice==0) //seconds
{
if (sec>0) sec--;
else sec=59;
}
}
if (button==2)
{
if (choice==1) //minutes
{
if (min<99) min++;
else min=0;
}
if (choice==0) //seconds
{
if (sec<59) sec++;
else sec=0;
}
}
if (button==3)
{
do {} while (digitalRead(button)==1);
choice=!choice;
lcd.setCursor(4,0);
lcd.println(" ");
lcd.setCursor(10,0);
lcd.println(" ");
if (choice==0)
{
lcd.setCursor(10,0);
lcd.print("<");
}
else
{
lcd.setCursor(4,0);
lcd.print(">");
}
}
if (button==4)
{
if(sec==0 && min==0) continue;
else signal(200);
}
delay(50);
} while (button!=4 || (sec==0 && min==0));
}
unsigned long start()
{
Serial.println("start!");
long minutes=min;
long seconds=sec;
mil=millis();
unsigned long time=(minutes*60L+seconds)*1000L+millis();
lcd.setCursor(4,0);
lcd.println(" ");
lcd.setCursor(10,0);
lcd.println(" ");
lcd.setCursor(0,1);
for (int i=0; i<16; i++) lcd.write(byte(0));
return time;
}
void alarm()
{
for (int i=0; i<60; i++)
{
for (int j=1; j<5; j++)
{
button=whichButton();
if (digitalRead(button)==1) return;
signal(62);
button=whichButton();
if (digitalRead(button)==1) return;
delay(62);
}
for (int j=1; j<9; j++)
{
button=whichButton();
if (digitalRead(button)==1) return;
delay(62);
}
}
}
void count(long long deadline)
{
milisLeft=deadline-millis();
min=milisLeft/60000;
sec=(milisLeft/1000)%60;
if (milisLeft<10) milisLeft=0;
display();
bar();
if (milisLeft/10==400 || milisLeft/10==300 || milisLeft/10==200 || milisLeft/10==100) signal(50); // signal when 5, 4, 3, 2, 1 secons left
}
void bar()
{
lcd.setCursor(0,1);
int val=map(milisLeft, 0, deadline-mil, 80, -1);
for (int i=0; i<val/5; i++) lcd.write(byte(5));
lcd.write(byte(val%5));
}
void loop()
{
choosetime();
deadline=start();
do
{
button=whichButton();
if (button!=4) count(deadline);
else
{
lcd.setCursor(0,1);
for (int i=0; i<16; i++) lcd.write(byte(5));
signal(100);
break;
}
} while (milisLeft>0);
if (milisLeft==0)
{
min=0;
sec=0;
alarm();
}
}
