Hesam Moshiri
Published © CC BY-NC-SA

Infrared Remote Control Decoder & Switcher using Arduino

Infrared Remote Control Decoder & Switcher using Arduino

IntermediateFull instructions provided2 hours1,060
Infrared Remote Control Decoder & Switcher using Arduino

Story

Read more

Code

Code

C/C++
#include <IRremote.h>
#include <EEPROM.h>


byte keyCounter = 0;
int data1 = -1, data2 = -1, data3 = -1;
byte out2Toggle = 0;


void setup() {
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(4, INPUT);
  IrReceiver.begin(3, DISABLE_LED_FEEDBACK);
}


void loop() {


  data1 = EEPROM.read(0);
  data2 = EEPROM.read(1);
  data3 = EEPROM.read(2);
  
  while (digitalRead(4) == 0)
  {
    if (IrReceiver.decode())
    {
      delay(200);
      if (keyCounter == 0)
      {
        data1 = IrReceiver.decodedIRData.decodedRawData;
        digitalWrite(2, HIGH);
        delay(2000);
        digitalWrite(2, LOW);
        EEPROM.write(0, data1);
        keyCounter ++;
      } else if (keyCounter == 1) {
        data2 = IrReceiver.decodedIRData.decodedRawData;
        digitalWrite(1, HIGH);
        delay(2000);
        digitalWrite(1, LOW);
        EEPROM.write(1, data2);
        keyCounter ++;
      } else if (keyCounter == 2) {
        data3 = IrReceiver.decodedIRData.decodedRawData;
        digitalWrite(0, HIGH);
        delay(2000);
        digitalWrite(0, LOW);
        EEPROM.write(2, data3);
        keyCounter = 0;
      }
      IrReceiver.resume();
    }
  }


  if (IrReceiver.decode())
  {
    delay(200);
    if (data1 == IrReceiver.decodedIRData.decodedRawData)
    {
      digitalWrite(2, HIGH);
      delay(250);
      digitalWrite(2, LOW);
      delay(250);
    }


    if (data2 == IrReceiver.decodedIRData.decodedRawData)
    {
      switch (out2Toggle)
      {
        case 0:
          digitalWrite(1, HIGH);
          out2Toggle = 1;
          break;
        case 1:
          digitalWrite(1, LOW);
          out2Toggle  = 0;
          break;
      }
    }


    if (data3 == IrReceiver.decodedIRData.decodedRawData)
    {
      digitalWrite(0, HIGH);
      delay(250);
      digitalWrite(0, LOW);
      delay(250);
    }
    IrReceiver.resume();
  }
}

Credits

Hesam Moshiri

Hesam Moshiri

51 projects • 32 followers
https://www.youtube.com/c/MyVanitar/videos

Comments