Hi there,
As I have written in the link below I´m controlling my stepper with interrupts
Now I need to disable the interrupt routine to do some other stuff.
But when I call the function noInterrupts(); this will have no effect.
The interrupt will still trigger I do not understand why ?
Does someone know if the interrupt is enabled by thees functions?
@Costas your link explains how “attachInterrupt” is used
But how to use "detachInterrupt " ??
I tried several versions but I got always Error messages from the compiler.
detachInterrupt (); causes : too few arguments to function ‘void detachInterrupt(uint8_t)’
detachInterrupt (timer0_ISR); causes : invalid conversion from ‘void (*)()’ to ‘uint8_t {aka unsigned char}’ [-fpermissive]
@Brummer take a look through this and pull out the bits you need. I haven’t looked at the sketch for 6 months but I do have interrupt stuff in sketches I use fairly regularly.
// DoorInterrupt.ino 15 July 2016 Blynk project name VPinTest (not fully working)
// test with a momentary or regular switch between GND and D3 (no resistors required)
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
//#define HIGH 255 // alternative method for LED's
//#define LOW 0 // alternative method for LED's
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
WidgetLED led1(V1); // green - door closed
WidgetLED led2(V2); // red - door open
int Door_Sensor_Pin = 0; // choose the Door_Sensor_Pin, D3 on WeMos, has builtin 10K pullup resistor
float Milliseconds;
int reedswitchstatus; // 1 is connected (door closed) and 0 is notconnected (door open)
int ledState = LOW;
int btnState = HIGH;
SimpleTimer timer;
void setup()
{
Serial.begin(115200);
pinMode(Door_Sensor_Pin, INPUT_PULLUP); // declare Door_Sensor_Pin as input
//attachInterrupt(Door_Sensor_Pin, highInterrupt, RISING); // use for Gnd connection
attachInterrupt(Door_Sensor_Pin, changeInterrupt, CHANGE); // use for Gnd connection
Blynk.begin(auth, "xxxxxxxxxx", "xxxxxxxxxx");
while (Blynk.connect() == false) {
// wait for connection to Blynk server before proceeding
}
Blynk.virtualWrite(V1, 0); // green LED off until reedswitch is checked
Blynk.virtualWrite(V2, 0); // red LED off until reedswitch is checked
digitalWrite(Door_Sensor_Pin, ledState);
timer.setInterval(100L, updateLEDs); // every half second (500L milliseconds) update the LED's
}
void changeInterrupt(){ // can't update LED's within interrupt as it will reset the ESP
Serial.println("Door Opened or Closed"); // LOW, (default state is HIGH, door open)
reedswitchstatus = !reedswitchstatus;
detachInterrupt(Door_Sensor_Pin);
Milliseconds = millis()+50; //debounce delay
while (Milliseconds > millis()) ;
attachInterrupt(Door_Sensor_Pin, changeInterrupt, CHANGE);
}
void lowInterrupt(){ // can't update LED's within interrupt as it will reset the ESP. NOT CURRENTLY USED.
Serial.println("Door Closed"); // LOW, (default state is HIGH, door open)
reedswitchstatus = 1;
detachInterrupt(Door_Sensor_Pin);
Milliseconds = millis()+50; //debounce delay
while (Milliseconds > millis()) ;
attachInterrupt(Door_Sensor_Pin, highInterrupt, RISING);
}
void highInterrupt(){ // can't update LED's within interrupt as it will reset the ESP. NOT CURRENTLY USED.
Serial.println("Door Open"); // HIGH, default state
reedswitchstatus = 0;
detachInterrupt(Door_Sensor_Pin);
Milliseconds = millis()+50; //debounce delay
while (Milliseconds > millis()) ;
attachInterrupt(Door_Sensor_Pin, lowInterrupt, FALLING);
}
void updateLEDs(){
if((reedswitchstatus == 1) || (digitalRead(Door_Sensor_Pin) == LOW)){ // door closed, illuminate GREEN LED
//if(digitalRead(Door_Sensor_Pin) == LOW){ // door closed, illuminate GREEN LED
// btnState is used to avoid sequential toggles
if (btnState != LOW) {
Serial.print("Door sensor is now ");
Serial.println(ledState);
// Toggle LED state
ledState = !ledState;
Blynk.virtualWrite(V1, (255 * ledState)); // Green LED
Blynk.virtualWrite(V2, (255 * !ledState)); // Red LED
Blynk.virtualWrite(V0, ledState); // Blynk button
}
btnState = LOW;
}
else {
btnState = HIGH;
}
}
BLYNK_WRITE(V0) // Button to open and close door
{
ledState = param.asInt();
digitalWrite(Door_Sensor_Pin, ledState);
Serial.print("Button status is ");
Serial.println(ledState);
if(ledState == 1){
led1.on();
led2.off();
}
else{
led1.off();
led2.on();
}
}
BLYNK_WRITE(V3) // Timer1 // todo
{
int Timer1 = param.asInt();
if (Timer1 == 1) {
digitalWrite(Door_Sensor_Pin, 0); // door closed
Serial.println("Timer closed the door");
}
//else{
// digitalWrite(Door_Sensor_Pin, 1); // door open
// Serial.println("Timer opened the door");
//}
Serial.print("Got a value: ");
Serial.println(param.asStr());
}
void loop()
{
Blynk.run();
timer.run();
}