Can i the can when can

can can the can if eh when

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)

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