Meet conditions along a certain amount of time

Hello my Friends. I am programming a Sumo robot
The problem is:
I want my robot to have a tactile switch so I can know wheter I am pushing a bot or no. I want the robot to move to other side if he has been pushing for around 5 seconds. But I do not know how to write that the button has to be pressed for 5 seconds. I tried using things like an if statement + delay but It does it and waits 5 seconds till next Reading, but I dont want that. I want the robot to move when the button is pressed during 5 seconds.
Thanks

If you want to check if a button is pressed for some period of time this code should work

btnVal = digitalRead(btnPin);
if (btnVal == HIGH) {    // assumes btn is LOW when pressed
   lastBtnPressMillis = millis();   // btn not pressed so reset clock
}
if (millis() - lastBtnPressMillis > = interval) {
   // button has been pressed for longer than interval
}

...R

PD: Pardon my english hahaha.

I'll pardon your English, but not your cross-posting.

(you do realise we have a Spanish section of the forum)

Oh, sorry, I did not read that, I read the "Read before posting" but I think missed it or something.

Robin2: If you want to check if a button is pressed for some period of time this code should work

btnVal = digitalRead(btnPin);
if (btnVal == HIGH) {    // assumes btn is LOW when pressed
   lastBtnPressMillis = millis();   // btn not pressed so reset clock
}
if (millis() - lastBtnPressMillis > = interval) {
   // button has been pressed for longer than interval
}

...R

Should this be in void loop(), because my arduino keeps telling me this

ROBOT_SUMO.ino: In function 'void loop()': ROBOT_SUMO:132: error: 'lastBtnPressMillis' was not declared in this scope ROBOT_SUMO:134: error: 'lastBtnPressMillis' was not declared in this scope ROBOT_SUMO:134: error: expected primary-expression before '=' token ROBOT_SUMO:134: error: 'interval' was not declared in this scope 'lastBtnPressMillis' was not declared in this scope

Did you declare the variables?

if (millis() - lastBtnPressMillis > = interval) {

The operator is >= (no space).

Post YOUR complete code.

Godomasta: Should this be in void loop(), because my arduino keeps telling me this

I did not post a complete piece of code. I had assumed you would know to declare the necessary variables at the top of your code.

Where it goes depends on where you want to use it.

...R

Thanks all for the help! In return, a small working example. board used is really small and only has 5 digital outputs (Velleman VMA108) and runs on 5V USB power.

About the code:
Interval is set to 2 seconds (2000ms = 2 seconds), not 5 seconds.

My setup:
<>

What does it do:
If you press the button (no longer than 2 seconds), the LED lights up, if you let go of the button, LED = off again.
After pressing the button for 2 seconds or longer AND releasing it (only after releasing it), the LED will blink 3 times.

Note1:
some other variables are defined, like relays, a 2nd LED and reset. LED in the picture is LED2 = external LED. Don’t mind the others:)

Note2 that got me fooled:
Do not use lastBtnPressMillis as an INT, because milis () (milis=uptime of the program) grows every milisecond and an INT cannot grow beyond 32.000-something (basically after around 32 seconds, the INT = at max value and interval IF statement is always met, blinking never stops). A DOUBLE is as big as a float in Arduino IDE, a float is 3.4028235E+38 max, more than enough for milis to grow and not go into ‘blink loop’.

//board used vme108
int button1 = 0; //P0 = PB0, used 4.7K ohm resistor, because 5volts 
int LED1 = 1; //P1 = PB1 onboard LED
int LED2 = 2; //P2 = PB2 externalLED, used 510 ohm resistor, because 5volts
int RELAY1 =3; //P3 = PBx
int RELAY2 =4; //P4 = PBx
int RESET =5; //P5 = PB5, do not use, is reset
int buttonState1=0;
int interval=2000;
double lastBtnPressMillis; // double = float, cannot use int, since max value will be hit after 32 seconds, then continues blink of LED

void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(button1, INPUT);
 // next blink 3 times to indicate BOOT has finished
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
delay(200);
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
delay(200);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
delay(200);
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
delay(200);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
delay(200);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);

}
void loop() {

buttonState1=digitalRead(button1); 
  if (buttonState1 == 0) // BUTTON1 PRESSED
  {
digitalWrite(LED2, HIGH); // light up LED
}
     
 if (buttonState1 == 1) // BUTTON1 NOT PRESSED
 {
 digitalWrite(LED2, LOW); // turn off LED
      if (millis() - lastBtnPressMillis >= interval) {
      // button has been pressed for longer than interval, blink 3 times + reset clock
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,LOW);
      delay(200);
      digitalWrite(LED1,LOW);
      digitalWrite(LED2,HIGH);
      delay(200);
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,LOW);
      delay(200);
      digitalWrite(LED1,LOW);
      digitalWrite(LED2,HIGH);
      delay(200);
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,LOW);
      delay(200);
      digitalWrite(LED1,LOW);
      digitalWrite(LED2,LOW);   
      } // end button pressed longer than interval code 
     lastBtnPressMillis = millis();   // btn not pressed so reset clock, also reset if we just blinked 3 times
    } // end if buttonState
} // end void loop

Thanks again for the from above posts in this thread.

Kind Regards,
Bas

Meet conditions along a certain amount of time
09-08-2015, 21:19:15 Last Edit: 07-03-2018, 20

:o