interrupting a loop with a button push

debriscreator
Posts: 1
Joined: Mon Sep 27, 2021 11:50 am

interrupting a loop with a button push

Postby debriscreator » Mon Sep 27, 2021 11:52 am

Hello,

I was wondering the best way to go about interrupting a loop with a button push.. heres my code. It's towards the bottom... right now its not working. :) thanks in advance.
  1. void processSlackMessage(String receivedpayload){
  2.  
  3.   DynamicJsonDocument doc(1024);
  4.   deserializeJson(doc, receivedpayload);
  5.   DeserializationError error = deserializeJson(doc, receivedpayload);
  6.   if (error) {
  7.     Serial.print(F("deserializeJson() failed with code "));
  8.     Serial.println(error.c_str());
  9.     return;
  10.   }
  11.   JsonObject obj = doc.as<JsonObject>();
  12.   String type_msg = obj["type"];
  13.   String texto = obj["text"];
  14.   String channel = obj["channel"];
  15.   if(type_msg.equals("message")){
  16.     Serial.println(type_msg + " "+ channel + " " + texto);
  17.     tft.fillScreen(TFT_BLACK);
  18.     tft.setTextColor(TFT_GOLD);
  19.     tft.drawString("======Alert======", 10, 20, GFXFF);
  20.     tft.drawString(texto, 10, 40, GFXFF);
  21.    
  22.     for (int i = 0; i <= 5; i++){
  23.       buttonState=digitalRead(buttonPin);
  24.       if (buttonState == LOW){
  25.        i=0;
  26.        break;
  27.     }
  28.     else {
  29.       digitalWrite(vibePin, HIGH);  
  30.       delay(1000);          
  31.       digitalWrite(vibePin, LOW);  
  32.       delay(500); }
  33.     }
  34.  }
  35. }
Moderators Note: Add Codebox

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: interrupting a loop with a button push

Postby ESP_Dazz » Sat Oct 09, 2021 8:01 am

  • If you want your loop to be indefinitely blocking on the interrupt, you can have the loop block an a semaphore that is given by the ISR
  • If you don't want your loop to be blocking, simply poll some global variable but with an adequate delay so that other tasks get a share CPU time. Don't forget to make that variable volatile.

Who is online

Users browsing this forum: No registered users and 122 guests