How is it going guys. I am having some trouble wrapping my head around Interrupts and while loops. I am working on a project that involves a BME280 temp sensor, an OLED screen and a rotary encoder. I am able to display temp and humidity values no problem on the screen. But since the OLED screen demands a fillscreenblack followed by some kind of delay (same with the BME280) and the rotary encoder needs to be interrupt based. That's fine and I thought I understood this quite well until now. See what I would like to do is press the button on the encoder to enter a menu and display different options. I was thinking of using an interrupt to enter a while loop where I could display the menu options and then press the button again and break from the while loop and get back to the main void loop. But I cant seem to wrap my head around this concept. Maybe I am thinking about interrupts all wrong, I am not sure, but I dont see why I could not do this. So not to upload quite a bit of code I made a little test environment with a button and an LED. When I press the button the interrupt activates the while loop. This causes the LED blink, but for the life of me I cant figure out how to press the button again to get out. Here is the code let me know what you think. The code as it stands blinks once but I have been trying all sorts of ways to make it behave as described. Sorry for the long read ! Thanks !!
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
volatile int flag = 0;
volatile int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
}
void blink() {
while(flag == 0){
buttonState = digitalRead(interruptPin);
if (buttonState == LOW){
flag = 1;
}
else{
digitalWrite(ledPin, HIGH);
delay(50000);
digitalWrite(ledPin, LOW);
delay(50000);
}
}
}