Timer program not working properly

I am traying for count up program, when count up value will reach to set value it will stop, and also other push buttons configured to set up, set down, Reset. but while running the program its not stops when set value triggered, Kindly support with correct code, code uploaded for reference
#include <LiquidCrystal_I2C.h> //Libraries
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD HEX address 0x27 -- change according to yours
#include <EEPROM.h>
#include "Countimer.h" //GitHub - inflop/Countimer: This is simple timer and counter Arduino library.
Countimer tdown;

#define bt_start A0
#define bt_stop A1
#define bt_reset A2
#define bt_set A3
#define bt_up A4
#define bt_down A5
#define relayout 13
int set = 0;
int flag1=0, flag2=0;
int hh=0, mm=0, ss=0, ms=0;
int time_mS=0, time_s=0, time_m=0, time_h=0;
bool timerStart = false;

void setup() {
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
pinMode(bt_start, INPUT_PULLUP);
pinMode(bt_stop, INPUT_PULLUP);
pinMode(bt_reset, INPUT_PULLUP);
pinMode(bt_up, INPUT_PULLUP);
pinMode(bt_down, INPUT_PULLUP);
pinMode(bt_set, INPUT_PULLUP);
pinMode(relayout,OUTPUT); // declare Buzzer as output

Serial.begin (9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor (0,0);
lcd.print("");
lcd.setCursor (0,1);
lcd.print(" GLOBAL ");
lcd.setCursor (0,2);
lcd.print(" INSTRUMENTS ");
lcd.setCursor (0,3);
lcd.print("
");
delay(2000);
lcd.clear();

noInterrupts(); // disable all interrupts
TCCR1A = 0; // set entire TCCR1A register to 0 //set timer1 interrupt at 1kHz // 1 ms
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // set timer count for 1khz increments
OCR1A = 1999; // = (1610^6) / (10008) - 1
//had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
// turn on CTC mode
TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11); // enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
interrupts(); // enable
}
void print_time(){
time_s = time_s-1;
if(time_s<0){time_s=59; time_m = time_m-1;}
if(time_m<0){time_m=59; time_h = time_h-1;}
}
void tdownComplete(){digitalWrite(relayout, HIGH);
timerStart = false; // Stop stopwatch
delay(100);;}

void loop(){
tdown.run();
if(digitalRead (bt_set) == 0){
if(flag1==0 && flag2==0){flag1=1;
set = set+1;
delay(100);
if(set>4){set=0;}
}
}else{flag1=0;}

if(digitalRead (bt_up) == 0){
if(set==0){timerStart = true; flag2=1;}
if(set==1){time_mS++;}
if(set==2){time_s++;}
if(set==3){time_m++;}
if(set==4){time_h++;}
if(time_mS>999){time_mS=000;}
if(time_s>59){time_s=0;}
if(time_m>59){time_m=0;}
if(time_h>59){time_h=0;}
if(set>0){eeprom_write();}
delay(100);
}

if(digitalRead (bt_down) == 0){
if(set==0){timerStart = false; flag2=0;}
if(set==1){time_mS--;}
if(set==2){time_s--;}
if(set==3){time_m--;}
if(set==4){time_h--;}
if(time_mS<0){time_mS=999;}
if(time_s<0){time_s=59;}
if(time_m<0){time_m=59;}
if(time_h<0){time_h=99;}
if(set>0){eeprom_write();}
delay(100);
}

if(digitalRead (bt_start) == 0){
timerStart = true; // Start stopwatch
delay(100);
}

if(digitalRead (bt_stop) == 0){
digitalWrite(relayout, HIGH);
timerStart = false; // Stop stopwatch
delay(100);
}

if(digitalRead (bt_reset) == 0){
ms=0, ss=0, mm=0, hh=0;
time_mS=0, time_s=0, time_m=0, time_h=0;// Reset stopwatch
delay(100);
}

lcd.setCursor(0,0);
if(timerStart == false && set==0){lcd.print(" Timer ");}
if(timerStart == false && set==1){lcd.print(" set Timer mS ");}
if(timerStart == false && set==2){lcd.print(" Set Timer SS ");}
if(timerStart == false && set==3){lcd.print(" Set Timer MM ");}
if(timerStart == false && set==4){lcd.print(" Set Timer HH ");}

if(time_s==ss && time_m==mm && time_h==hh && flag2==1){flag2=0;
analogWrite(bt_stop, LOW);(timerStart == false);
delay(100);
}
lcd.setCursor (0,0);
if(timerStart == true){lcd.print("SET TIME:- ");}
lcd.setCursor (1,1);
lcd.print((time_h/10)%10);
lcd.print(time_h%10);
lcd.print(":");
lcd.print((time_m/10)%10);
lcd.print(time_m%10);
lcd.print(":");
lcd.print((time_s/10)%10);
lcd.print(time_s%10);
lcd.print(":");
lcd.print((time_mS/100)%10);
lcd.print((time_mS/10)%10);
lcd.print(time_mS%10);

lcd.setCursor (0,2);
if(timerStart == true){lcd.print("RUN TIME:- ");}
lcd.setCursor (1,3);
lcd.print((hh/10)%10);
lcd.print(hh%10);
lcd.print(":");
lcd.print((mm/10)%10);
lcd.print(mm%10);
lcd.print(":");
lcd.print((ss/10)%10);
lcd.print(ss%10);
lcd.print(":");
lcd.print((ms/100)%10);
lcd.print((ms/10)%10);
lcd.print(ms%10);
digitalWrite(relayout, LOW);
}
void eeprom_write(){
EEPROM.write(1, time_mS);
EEPROM.write(2, time_s);
EEPROM.write(3, time_m);
EEPROM.write(4, time_h);
}

void eeprom_read(){
time_mS = EEPROM.read(1);
time_s = EEPROM.read(2);
time_m = EEPROM.read(3);
time_h = EEPROM.read(4);
}

ISR(TIMER1_COMPA_vect){
if(timerStart == true){ms=ms+1;
if(ms>999){ms=0;ss=ss+1;
if(ss>59){ss=0; mm=mm+1;}
if(mm>59){mm=0; hh=hh+1;}
}
}
}

seems like there are numerous flaws

  • the STOP condition is true at startup. where is the corresponding analogdigitalWrite() that allow the stop pin to be HGH?
  • was this intended to be a conditional state or set timerStart to false

i may be overlooking a few others

welcome to the arduino-forum.
I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

best regards Stefan

thanks for reply, problem solved

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.