counter with push butons

Hello guys! I need help. I'm developing a counter with two push buttons. So the deal is, when button1 is pression starts to count time, and the same happens to the button2, then the code compares the times and if the time1 is biggest then time2 the counter increases, if time1 is smallest than the time2 the counter decreases. But it is doesn't working. so, please help me. Thanks.

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
#define button1      4 
#define button2      5

boolean var1 = false;
boolean var2 = false;
int count = 0;
unsigned long time1 = 0;
unsigned long time2 = 0;

void setup(){
lcd.begin(16, 2);
pinMode(button1, INPUT);   
pinMode(button2, INPUT);
}
void loop(){
if(digitalRead(button1) == HIGH){
time1 = millis();
var1 = true;
}
if(digitalRead(button2) == HIGH){
time2 = millis();
var2 = true;
}
if ((var1 == true)&&(var2 == true)){
if(time1 > time2){
count++;
time1 = 0;
time2 = 0;
var1 = false;
var2 = false;
lcd.setCursor(0,0);
lcd.print(count);
}
if(time1 < time2){
count--;
time1 = 0;
time2 = 0;
var1 = false;
var2 = false;
lcd.setCursor(0,0);
lcd.print(count);
}
}
}

Huh? I do not understand the requirements.

To post:

  1. Use CTRL-T in the Arduino IDE to autoformat your code.
  2. Paste the autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.

You did #2 but NOT #1, resulting in nearly unreadable code.

Sorry about that!

#include<LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
#define button1      4
#define button2      5

boolean var1 = false;
boolean var2 = false;
int count = 0;
unsigned long time1 = 0;
unsigned long time2 = 0;

void setup() {
  lcd.begin(16, 2);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
}
void loop() {
  if (digitalRead(button1) == HIGH) {
    time1 = millis();
    var1 = true;
  }
  if (digitalRead(button2) == HIGH) {
    time2 = millis();
    var2 = true;
  }
  if ((var1 == true) && (var2 == true)) {
    if (time1 > time2) {
      count++;
      time1 = 0;
      time2 = 0;
      var1 = false;
      var2 = false;
      lcd.setCursor(0, 0);
      lcd.print(count);
    }
    if (time1 < time2) {
      count--;
      time1 = 0;
      time2 = 0;
      var1 = false;
      var2 = false;
      lcd.setCursor(0, 0);
      lcd.print(count);
    }
  }
}

I think your description is vague. The code isn't quite close enough to determine what you are trying to accomplish. if you could elaborate more maybe we could help more.

what i see is when both buttons are pressed time1 equals time2, then nothing happens because both if statements " if (time1 < time2)" and " if (time1 > time2)" are false.

So the deal is, when button1 is pression starts to count time

No, the deal is when button1 BECOMES pressed (NOT IS pressed), you want to record the time.

Look at the state change detection example.

Hi,
The question is;
What is the application?
What is it for?

Are you trying to make a reaction timer/scorer?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: