0

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);
}
10
  • the first if - else statement is incorrectly formulated
    – jsotola
    Apr 4 '20 at 22:49
  • your code does not block, as far as i can tell .... you should be able to add more functions to loop() without a problem
    – jsotola
    Apr 5 '20 at 1:24
  • the alarm_On() code does not need the if (ledState) code block .............. use ledState = ((ledState == LOW) ? HIGH : LOW); followed by digitalWrite(redLed , ledState);, etc.
    – jsotola
    Apr 5 '20 at 1:27
  • It does block the code if I want to do anything with the components/sensors that are mentioned in the arlam_on & alarm_Off functions. The only case where it doesn't interfere is when the code doesn't involve those components
    – Zaffresky
    Apr 5 '20 at 15:28
  • 1
    @ThomasWeller, no, the code does not compile ... the compiler returns a expected ';' before '{' token error .... the OP has been negligent in presenting a non-working code
    – jsotola
    Apr 5 '20 at 22:36
0

Your code is not blocked, but ON or OFF are called each loop so your change are to the "common" components are overwritten quite fast.

Do not track the state within the IRQ. You could do something like:

volatile byte alarm = 1;
byte alarm_state = 1;

void ISR_Alarm () {
    alarm = 1;  
}

void loop() {
    if (alarm){
        alarm = 0;

        if (alarm_state) {
            alarm_state = 0;
            alarm_Off();
        } else {
            alarm_state = 1;
            alarm_On();
        }
    }

    // other code
}
1
  • I was using the "blocking" in a different context but indeed the code is not blocking. It was easier to understand with your code example. Thanks
    – Zaffresky
    Apr 9 '20 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.