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);
}
}
}
2 Answers 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.
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)){};
-
Why the if? If it's true, the while won't continue, giving basically the same effect :)aaa– aaa2016年12月08日 13:48:45 +00:00Commented Dec 8, 2016 at 13:48
-
1the if here is just if he want to do something before, is just habit.YoavShtainer– YoavShtainer2016年12月08日 14:15:40 +00:00Commented Dec 8, 2016 at 14:15