This question is not for a project, I am just trying to understand what happens and why.
The sketch below works as expected as long as the ISR trigger is FALLING. The red led in the main loop is blinking, apparently without any disturbance from the interrupts. The question is what goes wrong, and why, when the ISR trigger is set to LOW.
int sensorPin3=3;
int ledPinGreen=12;
int ledPinRed=11;
int sensorStatus;
byte ledGreenStatus;
void setup() {
pinMode(ledPinGreen,OUTPUT);
pinMode(ledPinRed,OUTPUT);
pinMode(sensorPin3,INPUT_PULLUP);
ledGreenStatus=HIGH ;
Serial.begin(9600);
}
void loop() {
attachInterrupt(digitalPinToInterrupt(sensorPin3), sensorOn, LOW);
digitalWrite(ledPinRed,HIGH);
delay (5000);
digitalWrite(ledPinRed,LOW);
delay (5000);
}
void sensorOn()
{
static unsigned long lastInterruptTime=0;
unsigned long interruptTime=millis();
if(interruptTime-lastInterruptTime>100)
{
digitalWrite(ledPinGreen,ledGreenStatus);
ledGreenStatus =! ledGreenStatus;
lastInterruptTime=interruptTime;
}
}
If the sensor is activated and the signal on interrupt pin 3 is LOW, sensorOn will be executed. What happens if the ISR has been executed once? Does the program return to the main loop to the place where the interrupt was initiated and then right back to the ISR, or does it stay in the ISR without getting back to the mainloop for as long as the signal stays LOW?
If the sensor is activated for a couple of seconds: 1) if the program keeps running the ISR, I would not expect activity from the red led (main loop), but that led is blinking, with delay of about 2 seconds (without the interrupt that should be 5 seconds). This way it looks to me that the program returns to the main loop, but the delay time is wrong, so it doesn’t return to the right place, or the timing is not correct anymore. Why doesn’t this happen with the ISR trigger set to FALLING? Is the amount of interrupts disturbing the timing of the main loop delay?
2) if the program keeps executing the ISR but returns to the main loop every time in between, I would expect the millis() to be updated every time. In that case I would also expect the green led to blink because of the debouncing time. However the green led is not bliking. This makes me think that the program stays in the ISR so the millis() are not updated and the green led won’t blink. But that doesn’t match with the red led behaviour.
3) if the sensor is only activated for a short amount of time, the red led turns on or off the moment the signal on the interrupt pin is no longer LOW. No visible delay. I don’t understand why a longer LOW time on the interrupt pin makes a blink with about 2 seconds delay and a short LOW time makes no delay at all.
Thank you in advance for your help!