Boiled my project down to this recurring error, and after hours of searching I cant find a problem with the code. An IR message is received, stored in a variable, and written to communicate with another Arduino board to run other tasks. However, when the power button is pressed, the problem occurs. When the power button is pressed once, the linked relay will turn off after a delay using a state machine. When the same power button is pressed again to wake the relay, it'll wake, and turn off again. At the bottom of the loop, I reset the variable to zero but the message is still encoded as a power message causing these extra commands to run. Below are the code itself and serial monitor readings for better clarity. Any help would be appreciated.
#include <IRremote.h>
#define RELAY 9
int RECV_PIN = 10;
#define GATE 11
#define ONE_WIRE_BUS 12
IRrecv irrecv(RECV_PIN);
decode_results results;
boolean power_message, POFHT, section_1 ;
char message, old_message;
int power_count = 0;
float Celcius = 0, Fahrenheit = 0;
String Cooling_State, Power_State, fans, Running_Fans;
unsigned long Message_Interval = 10;
unsigned long PSO_Interval = 15000;
unsigned long previousMillis_A = 0, previousMillis_B = 0;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(GATE, OUTPUT);
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH);
Power_State = "ON";
fans = "ENABLED";
delay(1000);
Serial.write('a');
message = 0;
}
void loop() {
unsigned long currentMillis = millis();
if (irrecv.decode(&results)) { //RECIEVE IR MESSAGES - START
if (results.value == 0xE0E040BF) { //POWER
message = 'a';
}
if (results.value == 0xE0E0E01F) { //VOLUME UP
message = 'b';
}
delay(1);
irrecv.resume(); // Receive the next value
} //RECIEVE IR MESSAGES - END
if (message == 'a' || message == 'b') { //FILTER MESSAGE BEFORE SENDING - START
Serial.println("");
Serial.print(message);
Serial.print("\t");
Serial.print("CHECK POINT");
Serial.println("");
if (message == 'a') {//ADJUST MESSAGE INTERVAL TIMING - START
Message_Interval = 600;
}
else {
Message_Interval = 10;
} //ADJUST MESSAGE INTERVAL TIMING - END
if (currentMillis - previousMillis_A >= Message_Interval) { // SEND MESSAGES - START
if (message == 'a') { // SECTION 1 - ONCE PER LOOP, RESETS AT BOTTOM - START
if (Power_State == "ON" && section_1 == false) {
Serial.println("PRE - POWER OFF"); //FOR DEBUGGING
Power_State = "OFF";
section_1 = true; //SECTION 1 CHANGED TO TRUE TO AVOID ACTIVATING LOWER HALF OF POWER STATE OFF
power_message = true;//I WANT TO TURN OFF
}
if (Power_State == "OFF" && section_1 == false && fans == "ENABLED") {
Serial.println(""); //FOR DEBUGGING
Serial.println("CANCEL PRE - POWER OFF"); //FOR DEBUGGING
Serial.println(""); //FOR DEBUGGING
Power_State = "ON";
section_1 = true;
power_message = false;//I WANT TO TURN ON
}
} // SECTION 1 - ONCE PER LOOP, RESETS AT BOTTOM - END
if (Power_State == "ON") {
Serial.write(message);
}
if (Power_State == "OFF" && fans == "DISABLED") { //FANS DISABLED CONFIRMS POWER HAS BEEN CUT BY RELAY
Serial.println("WAKE UP FROM DEEP SLEEP"); //FOR DEBUGGING
digitalWrite(RELAY, HIGH);
Power_State = "ON";
power_message = false;
fans = "ENABLED";
delay(1000);
Serial.write(message);
}
message = 0;
previousMillis_A = currentMillis;
} // SEND MESSAGES - END
} //FILTER MESSAGE BEFORE SENDING - END
if (currentMillis - previousMillis_B >= PSO_Interval) { //POWER SETTINGS FOR DELAYED POWER OFF
if (power_message == true && Power_State == "OFF") { //ON to OFF - 10 MINS
Serial.println("GOING TO DEEP SLEEP"); //FOR DEBUGGING
digitalWrite(RELAY, LOW);
power_message = false;
fans = "DISABLED";
}
previousMillis_B = currentMillis;
}
delay(100);
section_1 = false;
old_message = message;
message = 0;
}