{"id":29396,"date":"2021-04-06T09:48:51","date_gmt":"2021-04-06T07:48:51","guid":{"rendered":"http:\/\/alessandrobuzzi.com\/?p=29396"},"modified":"2021-04-06T09:48:51","modified_gmt":"2021-04-06T07:48:51","slug":"arduino-ir-relay-control","status":"publish","type":"post","link":"https:\/\/alessandrobuzzi.com\/?p=29396","title":{"rendered":"Arduino IR multiple relay control"},"content":{"rendered":"\n<pre class=\"wp-block-syntaxhighlighter-code\">\/*\n\n * alessandrobuzzi.com\n *\/\n\n#include &lt;IRremote.h>\n#include &lt;Arduino.h>\n\n#define DECODE_NEC         \/\/ special decoder for all protocols\n#define IR_RECEIVE_PIN      7\n\n\/\/ Relay\nconst int SW1 = 8;  \/\/ the Arduino pin, which connects to the IN pin of relay\nconst int SW2 = 9;  \/\/ the Arduino pin, which connects to the IN pin of relay\nconst int SW3 = 10;  \/\/ the Arduino pin, which connects to the IN pin of relay\n\nvoid setup() {\n    Serial.begin(115200);\n    \/\/Start the receiver, enable feedback LED and take LED feedback pin from the internal boards definition\n    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);\n    Serial.print(F(\"Ready to receive IR signals at pin \"));\n    Serial.println(IR_RECEIVE_PIN);\n    \/\/ relays off\n   pinMode(SW1, OUTPUT);\n   pinMode(SW2, OUTPUT);\n   pinMode(SW3, OUTPUT);\n   digitalWrite(SW1, HIGH); \/\/ Relay is active low, so HIGH will turn it off at startup\n   digitalWrite(SW2, HIGH); \n   digitalWrite(SW3, HIGH); \n}\n\nvoid loop() {\n    \/*\n     * Check if received data is available and if yes, try to decode it.\n     * Decoded result is in the IrReceiver.decodedIRData structure.\n     *\n     * E.g. command is in IrReceiver.decodedIRData.command\n     * address is in command is in IrReceiver.decodedIRData.address\n     * and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData\n     *\/\n    if (IrReceiver.decode()) {\n\n        \/\/ Print a short summary of received data\n        IrReceiver.printIRResultShort(&amp;Serial);\n        Serial.println();\n        \/\/Enable receiving of the next value\n        IrReceiver.resume(); \/\/ Enable receiving of the next value\n\n        \/*\n         * Finally, check the received data and perform actions according to the received command\n         *\/\n        if (IrReceiver.decodedIRData.command == 0xC) {\n            \/\/ 1\n            digitalWrite(SW1, LOW);\n        } else if (IrReceiver.decodedIRData.command == 0x18) {\n            \/\/ 2\n            digitalWrite(SW1, HIGH);\n        }else if (IrReceiver.decodedIRData.command == 0x8) {\n            \/\/ 4\n             digitalWrite(SW2, LOW);\n        }else if (IrReceiver.decodedIRData.command == 0x1C) {\n            \/\/ 5\n            digitalWrite(SW2, HIGH);\n        }else if (IrReceiver.decodedIRData.command == 0x42) {\n            \/\/ 7\n            digitalWrite(SW3, LOW);\n        }else if (IrReceiver.decodedIRData.command == 0x52) {\n            \/\/ 8\n            digitalWrite(SW3, HIGH);  \/\/ tasto 8\n        }else if (IrReceiver.decodedIRData.command == 0x9) {\n            \/\/ DOWN\n            digitalWrite(SW1, HIGH); \/\/ All off \n            digitalWrite(SW2, HIGH);\n            digitalWrite(SW3, HIGH);\n        }else if (IrReceiver.decodedIRData.command == 0x7) {\n            \/\/ UP\n             digitalWrite(SW1, LOW); \/\/ All on \n             digitalWrite(SW2, LOW);\n             digitalWrite(SW3, LOW);\n        }\n\n\n        \n    }\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-29396","post","type-post","status-publish","format-standard","hentry","category-non-categorizzato"],"acf":[],"_links":{"self":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/posts\/29396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29396"}],"version-history":[{"count":0,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=\/wp\/v2\/posts\/29396\/revisions"}],"wp:attachment":[{"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alessandrobuzzi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}