Avoid code in loop() to check a button status

Ok Blynkers… I have a simple question, I know it is very trivial.

I would like to avoid to write code in loop(), so this is my code that check continuously
if a button is pressed or not. Is possible to check the status of this button outside of loop()?
I always use “timer” function but this time I need to check the status of the button in every moment and not any specific time:

void loop()
{
  Blynk.run();

  if (digitalRead(pin_ok) == LOW) {
  lcd.clear();
  lcd.noBacklight();
  Blynk.virtualWrite (12, "OK, ricevuto");
  if (myservo.read() > 1){
    abbassa_bandiera();  
  }
  }

either use a timer with a very short interval, say 100mS. Or use interrupts.

3 Likes

What @Toro_Blanco said; you can find more info here

1 Like

Ok guys, I always used timer to send data from my hardware to Blynk server. My dubt is if sending data every 100mS may cause Blynk flood error or this may required a lot of resources Blynk server side. Also because I have to check 5 buttons simultaneasly and experienced that sending too many data, the ESP8266 goes off line. What do you think about? Thanks!

so seperate it from your code, and run a 1 second timer update for blynk update only.
I’ve done this for my thermostat. That routine (check buttons) runs every 100ms and updates ‘targetT’. Then every second an update is send to the blynk app with the new targetT (but ONLY if targetT has changed!).

So 2 routines:

  • checkButtons (every 100ms)
  • updateBlynk (every 1000ms, with an ‘hasChanged’ bool check)
    update_variable globally declared
1 Like

I got it! Perfect, thank you @wolph42

Your avatar is confusing )

1 Like

changed… :hugs: