Interrupt crashes my microcontroller (and unfortunately my patience too)

Why it doesn´t works very well? Using a relay to expand an existing ON/OFF light circuit and a relay which says me light is ON or OFF. Electrical no problem :laughing:
Programmatically in theory also not but in practice :woozy_face:
To avoid lags in programm I don´t request the input periodically on a fix time. Instead of this I used an interrupt which kills me (and my program). After switching a few times, the microcontroller crashes himself. How I could avoid it?? Any solutions??


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial

#define NEON_GREEN      "#00FF00"
#define BLYNK_GREEN     "#23C48E"
#define BLYNK_BLACK     "#000000"

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";       //Auth Token von Wemos_Inndoor
char ssid[] = "xxxxxxxxxxxx";                           //WLAN SSID
char pass[] = "xxxxxxxxxxxxxxxxxxx";                   //WLAN Password

const int WemosPin_D1 = 5;            //Relay -1K09 Status Relay
const int WemosPin_D2 = 4;            //Relay -1K01 Switch Roomlight
bool Push_Roomlight = 0;              //App Button Raumlicht V20
bool RoomlightState = 0;              // (when 1->Roomlight ON, else 0)
int Flag_Roomlight = 0;               //Flip-Flop-Flag

WidgetLED LED_Roomlight(V50);

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode (WemosPin_D1, INPUT_PULLUP);
  pinMode (WemosPin_D2, OUTPUT);
  attachInterrupt(WemosPin_D1, Func_RoomlightState, CHANGE);
  
  Blynk.setProperty (V20, "onLabel",      "ON");                
  Blynk.setProperty (V20, "offLabel",     "OFF");                
  Blynk.setProperty (V20, "onColor",      BLYNK_BLACK);              
  Blynk.setProperty (V20, "offColor",     BLYNK_BLACK);                
  Blynk.setProperty (V20, "onBackColor",  NEON_GREEN);                
  Blynk.setProperty (V20, "offBackColor", BLYNK_GREEN);                
}


BLYNK_WRITE(V20) {                                                      
  Push_Roomlight = param.asInt();
  Func_RoomlightSwitch();
  if (Push_Roomlight == 1) {
    Flag_Roomlight ++;
  }
  if (Flag_Roomlight == 2) {
    Flag_Roomlight = 0;
  }
}
void Func_RoomlightSwitch() {
  if (Flag_Roomlight == 1) {
    digitalWrite (WemosPin_D2, HIGH);
  }
  if (Flag_Roomlight == 0) {
    digitalWrite (WemosPin_D2, LOW);
  }
}

void Func_RoomlightState() {
  RoomlightState = digitalRead(WemosPin_D1);
  if (RoomlightState == 1) {
    LED_Roomlight.on();
    Blynk.virtualWrite (V20, HIGH);
  }
  if (RoomlightState == 0) {
    LED_Roomlight.off();
    Blynk.virtualWrite (V20, LOW);
  }
}
void loop() {
  Blynk.run();
}

I’m not sure why you need to set these widget properties every time.
Try moving this into your void setup, just after you’ve connected to Blynk.

Pete.

Your posted code seems incomplete… no libraries?

Unsure what is triggering this interrupt, but as you have it set to CHANGE it could theoretically be triggering twice as often as you need (AKA on signal rise and fall of, say, a button press)… or worse, if there is any "bounce’ in the triggering mechanism.

1 Like

No, that wasn´t unfortunaly the solution :laughing:
I just tried all out (commented out the various rows) until I found out thats´s only a problem with the interrupt function which get called every switch-moment. Without it I can set and reset the Flag_Roomlight and switch in the relay -1K01 thousands of times without any problems.

as @Gunner said, we can’t help you without your whole sketch

Hey Gunner, above I completed the full sketch (libs) :wink:
No… but now back to the problem. Parallel to the roomlight is switched the relay -1K09. Now every time when I switch the light on or off, over Blynk-App (Relay -1K01) or manually with the light switch, the roomlight state should change his state.


If the light is turned off, I get a LOW signal in input of D1 and when it´s on a HIGH signal.
But “Debounce” sounds nice, because it happens only when I switch with the relay -1K01. Manually with the switch it works well.

More likely that when the relay triggers either way (but mostly when completing the circuit) your Wemos pin is receiving tens to hundreds of pulses due contact bounce, and your interrupt is triggering on each pulse twice due to CHANGE option.

A simple timer.setTimout with about 100ms could help right?? :sweat_smile:

No… but either a debouncing circuit (best option) or debouncing code will help… then pick either RISING (for triggering on HIGH) or FALLING (for triggering on LOW) for your interrupt.

Do you mean a pulldown-resistor?

No, that is something totally different… G :eyes: gle switch bouncing to see what I am talking about.

Thanks in the meantime, now I took a step forward. I report my solution tomorrow :wink: