Help with a countdown timer

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();
    }
}

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.

What do you want it to do ?

I just need a countdown time (not more that 90 seconds) that I can quickly adjust the time up or down using buttons. That is what the code is supposed to do. Eventually I need it to talk to a relay when the timer hits zero.

Naming pins for what they are supposed to do makes a lot of sense.

      if (digitalRead(button)==1) return;

I can not, for the life of me, figure out what this is supposed to do.

You didn't really sew a button to that pin, did you?

So I should probably keep looking for code that I can use. I was pretty excited when I found the code as it did exactly what I needed it for my project. When I have a better grasp I would like to be able to write the code myself but I am not knowledge enough right now.

PaulS:
You didn't really sew a button to that pin, did you?

Nope but I am a pretty good with a needle and thread.

Furthermore, digitalRead(...) is documented to return LOW or HIGH. Will it ever return 1? Maybe.

So I should probably keep looking for code that I can use.

No. You should make minor changes THAT YOU UNDERSTAND, to make the code more meaningful TO YOU.

The simplest is to change variable names to meaningful ones. You attached switches, not buttons, to the Arduino. The one connected to pin 0 does something specific. Name the variable to reflect what the pin does. Use Pin in the name. If the switch is supposed to start the timer, name the variable startPin.

Then, look at the rest of the code. When there is a useless name, like z in the signal() function, try to understand what it is doing, and, therefore, what a more meaningful name would be.

You'll soon find that coding if far simpler that you ever thought possible, when the code makes sense.

Thanks Paul. When I get home I will look at this some more and hopefully make some good progress.

On an Arduino Uno (and probably other Arduinos), it is a good idea to avoid pins 0 and 1. Novices often get into trouble with these pins when they go to debug because these pins are also used for hardware serial I/O.

vaj4088:
also used for hardware serial I/O.

I was wondering why the were marked TX & RX. Now I know. So if I ever get more into this (which I plan on) can you have two Unos talk to each other through the serial I/O?

Thanks vaj4088

can you have two Unos talk to each other through the serial I/O?

Of course. Even better, though, is to have them talk to each other using two other pins, and SoftwareSerial, and keep the hardware serial pins for talking to the PC (for debugging purposes).