Need help about time countdown loop


hello, has problem project.  used lcd 16x2 , arduino uno clock , time countdown.
i want time start countdown again when has finished first time , start value set first time. has nothing when finished countdown in first time,
just 00 : 00 : 00. try set variable use loop it's not success. please or guide me this, thank sir.


็here code

code: [select]
#include <liquidcrystal.h>  //these libraries need downloaded arduino playground website (liquidcrystal.h should come pre-downloaded with   
#include <time.h>               //the arduino): http://playground.arduino.cc/code/time
#include <wire.h>

int buttonpin1 = 10;
int buttonpin2 = 9;
int buttonpin3 = 8;
int buttonpin4 = 6 ;
int buttonpin5 = 13;
int j ;
int x, y, z,a ;
int tseconds=10, tminutes=0, hours=0;  //this line, along line in void timerfunction(), can adjust amount of time counted down in                                                                    //the timer function
int centiseconds=0, sseconds=0, sminutes=0;
int button1counter = 0;  //this section initializes button counters, how buttons can have multiple operations within same function - more info:       
int button1state = 0;        // http://arduino.cc/en/tutorial/buttonstatechange
int lastbutton1state = 0;
int button2counter = 0;
int button2state = 0;
int lastbutton2state = 0;

#include<servo.h>
servo myservo;
liquidcrystal lcd(12,11,5,4,3,2);

void setup()
{
  lcd.begin(16, 2);
  settime(14,39,00,2,07,2015);  // how can set time clock function (hour,minute,second,day,month,year)
  pinmode(buttonpin1, input_pullup);
  pinmode(buttonpin2, input_pullup);
  pinmode(buttonpin3, input_pullup);
   pinmode(buttonpin4, input_pullup);
    pinmode(buttonpin5, input_pullup);

  myservo.attach(7);
  myservo.write(0);
}

void loop()
{
  button1state = digitalread(buttonpin1);  //this section gives "mode" button ability have 3 seperate operations
  if(button1state != lastbutton1state)
  {
    if(button1state = high)
    {
      button1counter++;
      button2counter = 1;
      delay(200);
    }
  }
  lastbutton1state = button1state;
  if(button1counter > 3)
  {
    button1counter = 1;
  }
  button2state = digitalread(buttonpin2);  //similarly, section allows "start/stop" button ability have 2 seperate functions
  if(button2state != lastbutton2state)
  {
    if(button2state = high)
    {
      button2counter++;
      delay (200);
    }
  }
  lastbutton2state = button2state;
  if(button2counter > 2)
  {
    button2counter = 1;
  }

  switch(button1counter)  //switch case nice changing clock between 3 functions
  {
    case 1:  //the clock defaults clockfunction()
    lcd.clear();
    lcd.setcursor(2, 1);
    lcd.print(":");
    lcd.setcursor(5, 1);
    lcd.print(":");
    clockfunction();
    break;
    case 2:  //if "mode" button pressed, clock switches timerfunction()
    lcd.clear();
    lcd.setcursor(2, 1);
    lcd.print(":");
    lcd.setcursor(5, 1);
    lcd.print(":");
   
    timerfunction();
   
    break;
     case 3: 
     //if "mode" pressed again, clock switches once more stopwatchfunction()
      lcd.clear();
 lcd.setcursor(0, 0);
 lcd.print("servo delay");
  lcd.setcursor(0, 1);
  lcd.print(j);
  delay(100);
    speedx();
    break;

  }
}

void clockfunction()  //this time clock function; works using time.h library
{
  lcd.setcursor(0, 0);
  printdigits(month());
  lcd.setcursor(2, 0);
  lcd.print("/");
  lcd.setcursor(3, 0);
  printdigits(day());
  lcd.setcursor(5, 0);
  lcd.print("/");
  lcd.setcursor(6, 0);
  lcd.print(year());
  lcd.setcursor(0, 1);
  lcd.print(hour());
  lcd.setcursor(3, 1);
  printdigits(minute());
  lcd.setcursor(6, 1);
  printdigits(second());
  delay(100);
}

void timerfunction()  //the timer function made of post: http://pastebin.com/f57045830
{

  if(button2counter == 2 )  //if "start/stop" pressed, timer counts down
  {
    static unsigned long lasttick = 0;
    if (tseconds > 0)
    {
      if (millis() - lasttick >= 1000)
        {
        lasttick = millis();
        tseconds--;
        lcdoutput();
        delay(1000);
 
      }
     }
    if (tminutes > 0)
    {
     if (tseconds <= 0)
      {
        tminutes--;
        tseconds = 60;
      }
    }
    if (hours > 0)
    {
      if (tminutes <= 0)
      {
        hours--;
        tminutes = 60;
      }
    }
 
  }
  else  //if "start/stop" unpressed or pressed second time, display current time, don't count down
  {
    lcdoutput();
  }
 
  if(hours == 00 && tminutes == 00 && tseconds == 00)  //when timer ends, alarm goes off
  {
   
      myservo.write(300);
     delay(j*100);
       myservo.write(0);
lcdoutput();
delay(600);
hours = x;
tminutes = y ;
tseconds = z ;
lcdoutput();
   button2counter = 1;

   
    while(digitalread(buttonpin3) == high)  //the alarm go off until "restart" pressed
    {
   
      lcd.setcursor(0, 1);
      lcd.print("00:00:00");
       }
    }
    if(digitalread(buttonpin3) == low) //when "restart" pressed, timer resets
    {
    hours++;
    x = hours;
      lcdoutput();
     
     
    }
 if(digitalread(buttonpin5) == low) //
    {
    tminutes++;
   y = tminutes;
      lcdoutput();
     
    }
 
 
  if(digitalread(buttonpin4) == low && button2counter == 1)  //resets timer when "restart" button pressed, long timer not running
  {
    hours = 0 ;//this part must changed when timer altered, match initial time
    tminutes = 0;
    tseconds = 5;
    z = tseconds;
  lcdoutput();
  }
  if (hours > 12)
  {
     delay(100);
    hours =  0 ;
 
  }
}
void lcdoutput()  //this used display timer on lcd
{
  lcd.setcursor(0, 1);
  printdigits(hours);
  lcd.setcursor(3, 1);
  printdigits(tminutes);
  lcd.setcursor(6, 1);
  printdigits(tseconds);
  delay(100);
}

void printdigits(int digits)  //this void function useful; adds "0" beginning of number, 5 minutes displayed "00:05:00", rather "00:5 :00"
{
  if(digits < 10)
  {
    lcd.print("0");
    lcd.print(digits);
  }
  else
  {
    lcd.print(digits);
  }
}

void speedx()
{
  if(digitalread(buttonpin3) == low )   //if "start/stop" button pressed, time begins running
  {
    delay(100);
    j++;
   
  }
  if(digitalread(buttonpin5) == low )  //if "restart" button pressed, long timer paused, stopwatch resets 00:00.00
  {
    j--;
    delay(100);
  }
 
}


 
]

code: [select]
    if(button1state = high)


i'd start fixing ='s should =='s


Arduino Forum > Using Arduino > Programming Questions > Need help about time countdown loop


arduino

Comments