In my sketch when an interrupt gets triggered, a state variable is toggled from high to low and vice versa. Depending upon the variable state I have an if statement in the main loop that will call alarm_On function if state is high and the else part of the conditional statement will call alarm_Off function if the interrupt is triggered a second time. Thus I want this if condition to run only when the interrupt is triggered. In all other circumstances I want to run other code but if it concerns the same components e.g. the leds, the other code doesn't execute. If it's not a common component then it works fine. How can I overcome this "common component" issue?
// Analog Input devices
# define humiditySensor A5
// Alarm input
# define alarmButton 2 // Pushbutton
// Output Devices
# define redLed 10
# define yellowLed 11
# define greenLed 12
# define waterPump 9 // LED on relay
# define spk 5
// Variables
volatile byte alarm = LOW;
void setup() {
pinMode (alarmButton, INPUT);
pinMode (redLed, OUTPUT);
pinMode (yellowLed, OUTPUT);
pinMode (greenLed, OUTPUT);
pinMode (waterPump, OUTPUT);
attachInterrupt(digitalPinToInterrupt(alarmButton), ISR_Alarm, RISING);
Serial.begin(9600);
}
void loop() {
if (alarm == 1) {
alarm_On();
} else if (alarm == 0) {
alarm_Off();
}
int humVal = analogRead(humiditySensor);
Serial.println(humVal);
if (humVal >= 500) {
digitalWrite(redLed, HIGH);
} else {
digitalWrite(redLed, LOW);
}
} // void loop
void ISR_Alarm () {
alarm = !alarm; // toggle alarm function if pushed more than once
}
void alarm_On() {
tone (spk, 100);
digitalWrite(waterPump, HIGH);
static byte ledState = LOW;
static unsigned long oldTime = 0;
if (millis() - oldTime >= 250) { // loop to blink leds in emergency
oldTime = millis();
if (ledState == LOW) {
digitalWrite(redLed, HIGH);
digitalWrite(yellowLed, HIGH);
digitalWrite(greenLed, HIGH);
ledState = HIGH;
} else {
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, LOW);
ledState = LOW;
}
}
} // end of alarm on function
void alarm_Off () {
noTone (spk);
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(waterPump, LOW);
} // end of alarm off function
void high_Humidity() {
digitalWrite(waterPump, HIGH);
}
void low_Humidity() {
digitalWrite(waterPump, LOW);
}
if - else
statement is incorrectly formulated – jsotola Apr 4 '20 at 22:49loop()
without a problem – jsotola Apr 5 '20 at 1:24if (ledState)
code block .............. useledState = ((ledState == LOW) ? HIGH : LOW);
followed bydigitalWrite(redLed , ledState);
, etc. – jsotola Apr 5 '20 at 1:27expected ';' before '{' token
error .... the OP has been negligent in presenting a non-working code – jsotola Apr 5 '20 at 22:36