Using Interrupts

Dear friends. I want to use 3 buttons on Arduino Uno. My project has 2 different operating modes and the third button will be the stop button. It will shut down both 2 modes. I put the shutdown button on the interrupt pin. I want to change the value of the 'X' and then use that X to end do-while loop. My problem is The value of x does not change.

Here are my codes. I look forward to your ideas. I'm also open different ideas to make these ending operation.

const int openbutton = 5;
const int sleepbutton = 4;
const int forvard = 6;
const int reverse = 7;

int openbuttonval; 
int sleepbuttonval;
int x;

void setup() {
pinMode(openbutton, INPUT);
pinMode(sleepbutton, INPUT);
pinMode(forvard, OUTPUT);
pinMode(reverse, OUTPUT);
attachInterrupt(0, close, RISING);

}

void loop() {
x = 0;
openbuttonval = digitalRead(openbutton);
sleepbuttonval = digitalRead(sleepbutton);
if (openbuttonval == HIGH) {
opclose();
}
else if (sleepbuttonval == HIGH) {
sleep();
}


}

void close() {
openbuttonval = LOW;
sleepbuttonval = LOW;
digitalWrite(led, HIGH);
x = 1;

}

void opclose() {
digitalWrite(reverse, HIGH);
delay(250);
digitalWrite(reverse, LOW);
delay(25);
do {
digitalWrite(forvard, HIGH);
delay(475);
digitalWrite(forvard, LOW);
delay(25);
digitalWrite(reverse, HIGH);
delay(475);
digitalWrite(reverse, LOW);
delay(25);
digitalWrite(led2,HIGH);
} while (x == 0);
digitalWrite(forvard, HIGH);
delay(250);
digitalWrite(forvard, LOW);
delay(10);

}

void sleep() {
do {
...
}
while (x == 0);
}

If the buttons are being pressed by a human there is no need to use any interrupt - humans are sloowww.

The problem is that you have all those delay()s and WHILEs that prevent your program from checking the buttons frequently.

Have a look at how millis() is used to manage timing without blocking in Several things at a time

Use IF rather than WHILE and allow the loop() function to take care of the iteration.

And please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

You could try with declaring the variables shared with the ISR as volatile.

Because all these values only hold bytes, they should be declared as bytes
(to avoid the necessity to disable interrupts on each access from the main code).

volatile byte openbuttonval;
volatile byte sleepbuttonval;
volatile byte x;

You should read about interrupts here https://www.gammon.com.au/interrupts

Are you using an external pulldown resistor on pin 2 (since you want to be interrupted by a RISING signal)?