#include <IRremote.h> // Include the library
// Define the IRrecv Pin
#define Remote_RECEIVER 2 // Signal Pin of IR receiver
uint16_t Relay[] = {32, 33, 25, 26}; // Array holding the GPIOs connected to the relays
bool Relay_state[sizeof(Relay) / sizeof(Relay[0])] = {false}; // Each relay's state
#define size (sizeof(Relay) / sizeof(Relay[0])) // Define the size of the array
IRrecv Remote(Remote_RECEIVER); // Instantiate an IRrecv Object
void setup() {
Serial.begin(115200); // Initialize serial communication
Remote.enableIRIn(); // Start the receiver
// Set all of the GPIOs connected to the relays to OUTPUT MODE
for (int i = 0; i < size; i++) {
pinMode(Relay[i], OUTPUT);
digitalWrite(Relay[i], LOW); // Ensure all relays are initially off
}
}
void loop() {
if (Remote.decode()) { // Decode the data if available
Serial.print("Received command: ");
Serial.println(Remote.decodedIRData.command); // Print the received command
switch (Remote.decodedIRData.command) {
case 48: // Command 1
Relay_state[0] = !Relay_state[0];
digitalWrite(Relay[0], Relay_state[0]);
break;
case 24: // Command 2
Relay_state[1] = !Relay_state[1];
digitalWrite(Relay[1], Relay_state[1]);
break;
case 122: // Command 3
Relay_state[2] = !Relay_state[2];
digitalWrite(Relay[2], Relay_state[2]);
break;
case 16: // Command 4
Relay_state[3] = !Relay_state[3];
digitalWrite(Relay[3], Relay_state[3]);
break;
case 162: // Command for all relays
for (int i = 0; i < size; i++) {
Relay_state[i] = !Relay_state[i];
digitalWrite(Relay[i], Relay_state[i]);
}
break;
default:
// Handle other commands if necessary
break;
}
Remote.resume(); // Receive the next value
}
}