I need to do a timer with 2 buttons(the first to run and pause, and the second to reset), and LCD 16x2

The timer dont work at all, there is the circuit and the code /, someone please test and help me its for today.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,4,5,6,7);
int botao1 = 8;
int botao2 = 9;
int estadoBotao1 = 0;
int estadoBotao2 = 0;
int sec, min, hora, zero;

void cronometro();
void pausa();

void setup(){
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Cronometro do");
  delay(200);
  lcd.setCursor(1,1);
  lcd.print("Ian Rapini ._.");
  delay(800);
  lcd.clear();

  
  pinMode(botao1, INPUT_PULLUP);
  pinMode(botao2, INPUT_PULLUP);
  sec = 0;
  min = 0;
  hora = 0;
  zero = 0;
  delay(300);
}

void loop(){
    
    pausa();
    cronometro();
}

void cronometro(){
  if(!digitalRead(botao2)){
    sec = 0;
    min = 0;
    hora = 0;
    zero = 0;
    lcd.clear();
  }
  else{
  
   lcd.setCursor(0,0);
   lcd.print("Iniciando");
    
   int sec = 0;
   int min = 0;
   int hora = 0;
   int zero = 0;
  
    lcd.clear();
    lcd.home();
  
     if(millis() - zero > 1)
   {  
     sec++;
     zero = millis();
   }
   
   if(sec > 59)
   {
     lcd.setCursor(10, 1);
     lcd.print("  ");
     sec = 0;
     min++;
   }
  
   if(min > 59)
   {
     lcd.setCursor(5, 1);
     lcd.print("  ");
     min = 0;
     hora++;
   }
    
  if(hora == 24){ 
    hora = 0;
  }
  
   lcd.setCursor(2, 0);
   lcd.print(hora);
   lcd.setCursor(4, 0);
   lcd.print("h");
  
   lcd.setCursor(0, 1);
   lcd.print(min);
   lcd.setCursor(2, 1);
   lcd.print("m");
    
   lcd.setCursor(5, 1);
   lcd.print(sec);
   lcd.setCursor(7, 1);
   lcd.print("s");       
  }
}  

void pausa()
{
  static int trava = 0, pausa = 0;
  {
    if(!digitalRead(botao1) && !trava)             
    { 
      trava = 1;                                 
      pausa = !pausa;                                       

    }
    if(digitalRead(botao1))                      
    {
      trava = 0;   
    }
    
  }
  while(pausa); 
}
1 Like

There are 1000 millis() in a second.

Nice job with a readable diagram and code posted in code tags. First time posters seldom do as good a job.

zero needs to be unsigned long

You shouldn't name a variable the same as a function name. Also, if pausa ever becomes true you will be stuck right here until you reset:

while(pausa);

I'm not sure what you are trying to do with your buttons but since you have those inputs declared as INPUT_PULLUP then you only need the buttons connected to GND and the inputs. Not sure about those resistors.

The first one to pause and run, and the second to reset

This and your wiring don't match. You wired your buttons as active high with external pulldown resistors, but you wrote your code as active low with internal pullup resistors.

I can't make sense of this:

What do you want: active high or active low?

Bro, still dont work, can you test in your pc and reajust the code?

If you build/test it in a simulator, others don't have to: https://wokwi.com/arduino/new?template=arduino-uno

Also, the function cronometro() is very long and does too many things. You can separate the code that updates the counter from the code that handles the displaying: that will make it easier to follow and debug.

You can also use www.tinkercad.com to simulate.

There are a multitude of issues with your code. Here are a few:

  1. You shouldn't name a variable the same as a function name. Also, if pausa ever becomes true you will be stuck right here until you reset:
    while(pausa);

  2. zero needs to be an unsigned long

  3. You have sec, min, hora, and zero redefined as locals in the cronometro() function. This causes sec, min, hora to never increment.

  4. There is no reason to call the following every time in cronometro() function. It causes the LCD to blink:

lcd.setCursor(0,0);
lcd.print("Iniciando");
lcd.clear();
lcd.home();

  1. There is no reason to update the LCD unless seconds increases or a button is pushed. It is a waste of time.

How can I delete this post?

Why? Did you get it working?

No i change my code and made a new post

You cannot delete the thread.


Do not open a multiple topics on the same thing, just add to this thread.

1 Like

The only acceptable option is for you to post the updated version in this thread as a new post.

I made a Stopwatch with 2 buttons, I want that a button run the timer and pause if I press it again, and the other to reset the chronometer. I did the stopwach run but i can't make it pause, rerun and reset, can you guys help me?
there is the circuit and the code


#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,4,5,6,7);
int botao1 = 8;
int botao2 = 9;
int estadoBotao1 = 0;
int estadoBotao2 = 0;
int sec, min, hora;
unsigned long zero;

void cronometro();
void pausa();

void setup(){
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Cronometro do");
delay(200);
lcd.setCursor(1,1);
lcd.print("Ian Rapini ._.");
delay(800);
lcd.clear();

pinMode(botao1, INPUT);
pinMode(botao2, INPUT);
sec = 0;
min = 0;
hora = 0;
zero = 0;
delay(300);
}

void loop(){

cronometro();

}

void cronometro(){
estadoBotao2 = digitalRead(botao2);
estadoBotao1 = digitalRead(botao1);

lcd.clear();
lcd.print("Clique no Botao");
delay(100);

if(estadoBotao2 == HIGH){
sec = 0;
min = 0;
hora = 0;
zero = 0;
lcd.clear();

while(estadoBotao1 == LOW){

 lcd.clear();
 lcd.home();

 if(millis() - zero > 1)

{
sec++;
zero = millis();
}

if(sec > 59)
{
lcd.setCursor(10, 1);
lcd.print(" ");
sec = 0;
min++;
}

if(min > 59)
{
lcd.setCursor(5, 1);
lcd.print(" ");
min = 0;
hora++;
}

if(hora == 24){
hora = 0;
}

lcd.setCursor(2, 0);
lcd.print(hora);
lcd.setCursor(4, 0);
lcd.print("h");
delay(100);
lcd.setCursor(0, 1);
lcd.print(min);
lcd.setCursor(2, 1);
lcd.print("m");
delay(100);
lcd.setCursor(5, 1);
lcd.print(sec);
lcd.setCursor(7, 1);
lcd.print("s");
delay(100);
}
if(estadoBotao1 == HIGH){
while(estadoBotao2 == LOW){
sec = 0;
min = 0;
hora = 0;
zero = 0;
lcd.clear();
}
}
}
}

(Has anyone asked about code tags yet?
Formatting?)

Which "today"?

What stops estadoBotao2 being LOW?

'Cause I want that button 1 only reset and the 2 run and pause

I'm sorry, I don't understand your reply to my questions.