1

I am trying to time how long button is held for. The code seems to work but with two problems:

  1. the millis values are being read wrongly. If you hold the button for 1 second, it returns holdTime of about 3000 ms. A quick click returns holdTime of about 1000 ms.

  2. Each successive button press adds to the previous millis value so holdTime just gets higher and higher and doesn't measure how long the button has just been pressed for

Any ideas or advice for newbie welcome!

#include <LiquidCrystal.h>
const int button1Pin = 1; // pushbutton 1 pin
const int ledPin = 13; // LED pin
int button1State, button2State; // variables to hold the pushbutton states
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int startPressed = 0;
int endPressed = 0;
int holdTime = 0;
void setup() {
 // Set up the pushbutton pins to be an input:
 pinMode(button1Pin, INPUT);
 // Set up the LED pin to be an output:
 pinMode(ledPin, OUTPUT); 
 digitalWrite(ledPin, LOW); 
\
}
void loop() {
 while (digitalRead(button1Pin) == LOW) {}
 button1State = digitalRead(button1Pin);
 startPressed = 0;
 endPressed =0;
 holdTime = 0;
 // if button1 or button 2 are pressed (but not both)
 if (button1State == LOW) {
 digitalWrite(ledPin, LOW); // turn the LED on
 startPressed = millis();
 } else {
 digitalWrite(ledPin, HIGH); // turn the LED off
 endPressed = millis();
 holdTime = endPressed - startPressed;
 }
 lcd.print("You pressed for");
 lcd.setCursor(0, 1);
 lcd.print(holdTime);
 lcd.print("ms");
 while (digitalRead(button1Pin) == LOW) {}
 lcd.clear();
 startPressed =0;
 endPressed =0;
 holdTime = 0;
}
ocrdu
1,7953 gold badges12 silver badges24 bronze badges
asked Jan 10, 2021 at 22:45
3
  • use unsigned long to store milliseconds Commented Jan 11, 2021 at 6:15
  • You may use this library: arduinogetstarted.com/tutorials/arduino-button-library, you can count the time that the button is pressed down. Greetings Commented Jan 11, 2021 at 19:39
  • thank you for your advice - i will check on these things Commented Jan 11, 2021 at 22:38

1 Answer 1

1

Your source has several flaws, and one error that generates the output you see. This is the analysis of the error.

In loop() you start by waiting for the button pin to get HIGH, following by another read of the pin's state that will most probably return the same, HIGH. Note: Only if your button bounces with just the right timing, this last read will return LOW.

while (digitalRead(button1Pin) == LOW) {}
button1State = digitalRead(button1Pin);

Next you check the pin's state, and let's assume that it is HIGH. Only the else part is executed, and it calculates the holdtime as the difference of endPressed and startPressed. The former has the current value of millis(), but the latter is 0 as you initialized it.

That's why you're getting always growing values.

How to solve?

Think about the while loops, do you need them? Remember that loop() is already called repeatedly.

You may need a state variable that stores the last pin's state, so you can detect changes.

answered Jan 11, 2021 at 7:50
1
  • thank you for your advice - i will check on these things Commented Jan 11, 2021 at 22:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.