0

I would like to timer for how long a button is not pressed. So actually if(buttonState == LOW 'for two seconds') than execute the code that comes after it. But the problem is while the user is still pressing the button the timer already starts.

Code:

int ledPin[] = {7,8,9,10};
int ledPin11 = 11;
long randNumber;
const int buttonPin = 2;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
unsigned long previousMillis = 0;
const long interval = 2000;
void setup() {
 Serial.begin(9600);
 pinMode(LED_BUILTIN, OUTPUT);
 randomSeed(analogRead(0));
 randNumber = random(1, 16);
 Serial.println(randNumber);
 for (int i = 0; i < 4; i++) {
 pinMode(ledPin[i], OUTPUT);
 }
 pinMode(buttonPin, INPUT);
 }
 void loop() {
 displayBinary(randNumber); //functie 'display binary'
 unsigned long currentMillis = millis();
 if(currentMillis - previousMillis >= interval){
 previousMillis = currentMillis;
 if(buttonState == LOW){
 antwoord(buttonPushCounter,randNumber);
 delay(5000);
 displayBinary(randNumber);
 }
 }
 buttonState = digitalRead(buttonPin);
 if (buttonState != lastButtonState) { // als de button word ingedrukt krijgt buttonPushCounter + 1
 if (buttonState == HIGH) {
 buttonPushCounter++;
 Serial.print("Nummer hoe vaak is gedrukt");
 Serial.println(buttonPushCounter);
 } else {
 Serial.println("off");
}
delay(250);
 }
}
void antwoord(int teller, int antwoord) {
 if (teller == antwoord) {
 for (int i = 0; i < 4; i++) {
 pinMode(ledPin[i], LOW);
 }
 digitalWrite(LED_BUILTIN, HIGH);
 delay(250);
 digitalWrite(LED_BUILTIN, LOW);
 delay(250);
 digitalWrite(LED_BUILTIN, HIGH);
 delay(250);
 } else if (teller > antwoord || teller < antwoord) {
 for (int i = 0; i < 4; i++) {
 pinMode(ledPin[i], LOW);
 }
 digitalWrite(LED_BUILTIN, HIGH);
 }
}
void displayBinary(byte numToShow) //van byte naar decimaal
 {
 for (int i = 0; i < 4; i++) {
 if (bitRead(numToShow, i) == 1) {
 digitalWrite(ledPin[i], HIGH);
 } else {
 digitalWrite(ledPin[i], LOW);
 }
 }
 }
asked Dec 8, 2016 at 12:31

2 Answers 2

2

I would start a timer when the button changes state. Then you timeout if the button is released and it hasn't changed state for too long:

void loop()
{
 static int lastButtonState;
 static unsigned long buttonChangeTime;
 int buttonState = digitalRead(buttonPin);
 unsigned long now = millis();
 // Record time when button changes state.
 if (buttonState != lastButtonState)
 buttonChangeTime = now;
 // Timeout if button is not pushed for too long.
 if (buttonState == LOW && now - buttonChangeTime >= interval)
 Serial.println("Timeout.");
 lastButtonState = buttonState;
}

Don't forget to update lastButtonState on each iteration.

answered Dec 8, 2016 at 13:10
0

Your question is not clear enough.

I hope I get what you want.

Do while loop about the pressing...

if (button == LOW)
 while(!digitalread(button)){};
Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
answered Dec 8, 2016 at 12:56
2
  • Why the if? If it's true, the while won't continue, giving basically the same effect :) Commented Dec 8, 2016 at 13:48
  • 1
    the if here is just if he want to do something before, is just habit. Commented Dec 8, 2016 at 14:15

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.