Arduino IR multiple relay control

/*

 * alessandrobuzzi.com
 */

#include <IRremote.h>
#include <Arduino.h>

#define DECODE_NEC         // special decoder for all protocols
#define IR_RECEIVE_PIN      7

// Relay
const int SW1 = 8;  // the Arduino pin, which connects to the IN pin of relay
const int SW2 = 9;  // the Arduino pin, which connects to the IN pin of relay
const int SW3 = 10;  // the Arduino pin, which connects to the IN pin of relay

void setup() {
    Serial.begin(115200);
    //Start the receiver, enable feedback LED and take LED feedback pin from the internal boards definition
    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
    Serial.print(F("Ready to receive IR signals at pin "));
    Serial.println(IR_RECEIVE_PIN);
    // relays off
   pinMode(SW1, OUTPUT);
   pinMode(SW2, OUTPUT);
   pinMode(SW3, OUTPUT);
   digitalWrite(SW1, HIGH); // Relay is active low, so HIGH will turn it off at startup
   digitalWrite(SW2, HIGH); 
   digitalWrite(SW3, HIGH); 
}

void loop() {
    /*
     * Check if received data is available and if yes, try to decode it.
     * Decoded result is in the IrReceiver.decodedIRData structure.
     *
     * E.g. command is in IrReceiver.decodedIRData.command
     * address is in command is in IrReceiver.decodedIRData.address
     * and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData
     */
    if (IrReceiver.decode()) {

        // Print a short summary of received data
        IrReceiver.printIRResultShort(&Serial);
        Serial.println();
        //Enable receiving of the next value
        IrReceiver.resume(); // Enable receiving of the next value

        /*
         * Finally, check the received data and perform actions according to the received command
         */
        if (IrReceiver.decodedIRData.command == 0xC) {
            // 1
            digitalWrite(SW1, LOW);
        } else if (IrReceiver.decodedIRData.command == 0x18) {
            // 2
            digitalWrite(SW1, HIGH);
        }else if (IrReceiver.decodedIRData.command == 0x8) {
            // 4
             digitalWrite(SW2, LOW);
        }else if (IrReceiver.decodedIRData.command == 0x1C) {
            // 5
            digitalWrite(SW2, HIGH);
        }else if (IrReceiver.decodedIRData.command == 0x42) {
            // 7
            digitalWrite(SW3, LOW);
        }else if (IrReceiver.decodedIRData.command == 0x52) {
            // 8
            digitalWrite(SW3, HIGH);  // tasto 8
        }else if (IrReceiver.decodedIRData.command == 0x9) {
            // DOWN
            digitalWrite(SW1, HIGH); // All off 
            digitalWrite(SW2, HIGH);
            digitalWrite(SW3, HIGH);
        }else if (IrReceiver.decodedIRData.command == 0x7) {
            // UP
             digitalWrite(SW1, LOW); // All on 
             digitalWrite(SW2, LOW);
             digitalWrite(SW3, LOW);
        }


        
    }
}

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *