I have used attachInterrupt()
to reset my program to the beginning. It actually interrupts while running the 3 states as shown below inside of the if
statement but when I press the actual button it does not exit, it goes to the function and continuing to the next state. It only works (means exiting/resetting) after all three states have executed completely.
if(buttoncount<2){
originalstate();
alertstate();
evacuationstate();
I would like to make a button to stop the program while running in the middle and start again.
Code:
// Declaring pins
// Input pins
const int sensor1button_pin = 6;
const int sensor2button_pin = 3;
const int sensor3button_pin = 4;
const int sensor4button_pin = 5;
const int sensor5button_pin = 2;
const int sensor6button_pin = 15;
const int sensor7button_pin = 15;
// Output pins
const int led1_pin = 8;
const int led2_pin = 9;
const int led3_pin = 10;
const int led4_pin = 11;
const int buzzerpin = 15;
// Initialy buzzer at false mode
boolean flag1;
boolean flag2;
int flag3 ;
// for leds
int ledState1 = LOW; // Original
int ledState2 = LOW; // Alert
int ledState3 = LOW; // Evacuate
int ledState4 = LOW; // Isolate
// Buttons
int button1_state = LOW;
int button2_state = LOW;
int button3_state = LOW;
int button4_state = LOW;
int button6_state = LOW;
int button7_state = LOW;
int button5_state = LOW;
// Other variables
int buttoncount = 0;
int reset_switch=0;
void setup() {
// INPUT pins
pinMode(sensor1button_pin, INPUT);
pinMode(sensor2button_pin, INPUT);
pinMode(sensor3button_pin, INPUT);
pinMode(sensor4button_pin, INPUT);
pinMode(sensor5button_pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(sensor5button_pin), clearreset, CHANGE);
pinMode(sensor6button_pin, INPUT);
pinMode(sensor7button_pin, INPUT);
// OUTPUT pins
pinMode(buzzerpin, OUTPUT);
pinMode(led1_pin, OUTPUT);
pinMode(led2_pin, OUTPUT);
pinMode(led3_pin, OUTPUT);
pinMode(led4_pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
button1_state = digitalRead (sensor1button_pin);
button2_state = digitalRead (sensor2button_pin);
button3_state = digitalRead (sensor3button_pin);
button4_state = digitalRead (sensor4button_pin);
button5_state = digitalRead (sensor5button_pin);
button6_state = digitalRead (sensor6button_pin);
button7_state = digitalRead (sensor7button_pin);
Serial.println(digitalRead (sensor5button_pin));
if( button1_state == HIGH || button2_state == HIGH || button3_state == HIGH || button4_state == HIGH) {
delay(280);
buttoncount = buttoncount + 1;
}
if (button1_state == HIGH || button2_state == HIGH || button3_state == HIGH || button4_state == HIGH) {
if(buttoncount<2){
originalstate();
alertstate();
evacuationstate();
}
}
}
// LED lighting arrangements
void originalstate() {
digitalWrite(led1_pin, HIGH);
digitalWrite(led2_pin, LOW);
digitalWrite(led3_pin, LOW);
digitalWrite(led4_pin, LOW);
delay(1500);
}
void alertstate() {
digitalWrite(led1_pin, LOW);
digitalWrite(led2_pin, HIGH);
digitalWrite(led3_pin, LOW);
digitalWrite(led4_pin, LOW);
delay(1500);
}
void evacuationstate() {
digitalWrite(led1_pin, LOW);
digitalWrite(led2_pin, LOW);
digitalWrite(led3_pin, HIGH);
digitalWrite(led4_pin, LOW);
delay(1500);
}
void isolatestate() {
digitalWrite(led1_pin, LOW);
digitalWrite(led2_pin, LOW);
digitalWrite(led3_pin, LOW);
digitalWrite(led4_pin, HIGH);
}
// Clear or reset back to the begin
void clearreset() {
button1_state = LOW;
button2_state = LOW;
button3_state = LOW;
button4_state = LOW;
button5_state = LOW;
button6_state = LOW;
button7_state = LOW;
digitalWrite(led1_pin, LOW);
digitalWrite(led2_pin, LOW);
digitalWrite(led3_pin, LOW);
buttoncount = 0;
exit;
}
set
aflag
variable in the interrupt service function ... check the flag at the beginning of each state function and skipdigitalWrite()
anddelay()
commands if the flag is setstate
variable and call the state functions depending on its value .... use the interrupt to set the state variable to a specific valueif
statements one after another .... merge the contents into one