Weekend Project: IR and RGB leds

Welcome to this first weekend project!

To do this, you will need

And that is about it.

Let’s start with connecting the LED’s.

Depending on what you got, this method can vary.

I wanted some bigger lights, so I had to use something to handle the higher voltage and current. To do this, I added a ULN2003 transistor array. From the input pins on the ULN2003, I connected a wire down to the pin 9, 10 and 6 on the Arduino. If you want to use some other pins, make sure they got PWM (marked with pwm or ~ on the silkscreen).

 

The reason we want PWM is so we can dim the different colors, instead of only having the option to turn them on or off.

With the LED’s connected, it is time to add the IR receiver. For the receiver I decided to use pin 2 on the Arduino. This is where the receiver sends the received signal which then should be decoded.

The decoding of the signal can be done using this library: http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html

To get started easily, I made a little sketch that can receive the signals, and turn the different buttons into action.

The values in this sketch is based on this remote http://www.ebay.co.uk/itm/150543436727?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649 and when the button “Prev” is pressed, the color red will be selected. The brightness of red can then be turned up or down using the – and + buttons just below. “Next” will switch to green, and “Play/Pause” switches to blue.

Holding down any button for about 1 second will make the Arduino repeat the command.

httpv://www.youtube.com/watch?v=z59jtnc5SFU

#include <IRremote.h>

#define blinkDelay 100
#define valueSteps 25

#define lowFlash 25
#define repeatInterval 3

int RECV_PIN = 2;

IRrecv irrecv(RECV_PIN);

decode_results results;

int ledValues[3] = {
  255, 255, 255 };

const int ledPins[3] = {
  9, 10, 6 };

int activeLED = -1;

int value = 255;

boolean power = true;

#define debug 1

void setup()
{
  irrecv.enableIRIn(); // Start the receiver

  pinMode(ledPins[0], OUTPUT); //R
  pinMode(ledPins[1], OUTPUT); //G
  pinMode(ledPins[2], OUTPUT); //B

  setPWM(); //Set the output pins to the selected values
}

long lastCommand;
int repeats;

void loop()
{
  if (irrecv.decode(&results))
  {
    if (results.value != 4294967295)
    {
      if (results.value != lastCommand) repeats = 0; //We are using a new command now

      if (power)
      {
        lastCommand = results.value;
        executeCommand(lastCommand);
      }
      else
      {
        if (results.value == 16753245) //Power
        {
          power = true;

          setPWM(); //Set the output pins to the selected values
        }
      }
    }
    else
    {
      repeats++;
      if (repeats >= repeatInterval) executeCommand(lastCommand);
    }

    irrecv.resume(); // Receive the next value
  }
}

void ledBlink()
{
  if (ledValues[activeLED] == 0)
  {
    analogWrite(ledPins[activeLED], lowFlash);
    delay(blinkDelay);
    analogWrite(ledPins[activeLED], ledValues[activeLED]);
  }
  else
  {
    analogWrite(ledPins[activeLED], 0);
    delay(blinkDelay);
    analogWrite(ledPins[activeLED], ledValues[activeLED]);
  }
}

void setPWM()
{
  analogWrite(ledPins[0], ledValues[0]);
  analogWrite(ledPins[1], ledValues[1]);
  analogWrite(ledPins[2], ledValues[2]);
}

void executeCommand(long com)
{
  if (com == 16754775) //Vol+
  {
    ledValues[activeLED] += valueSteps;
    if (ledValues[activeLED] > 255)
    {
      ledValues[activeLED] = 255;

      ledBlink(); //Flash led
    }

    setPWM(); //Set the output pins to the selected values
  }
  else if (com == 16769055) //Vol-
  {
    ledValues[activeLED] -= valueSteps;

    if (ledValues[activeLED] < 0)
    {
      ledValues[activeLED] = 0;

      ledBlink(); //Flash led
    }

    setPWM(); //Set the output pins to the selected values
  }

  else if (com == 16720605) //Prev (R)
  {
    activeLED = 0; //0 = RED in array

    ledBlink(); //Flash selected led
  }

  else if (com == 16712445) //Next (G)
  {
    activeLED = 1; //0 = GREEN in array

    ledBlink(); //Flash selected led
  }

  else if (com == 16761405) //Play (B)
  {
    activeLED = 2; //0 = BLUE in array

    ledBlink(); //Flash selected led
  }

  else if (com == 16753245) //Power
  {
    power = false;

    analogWrite(ledPins[0], 0);
    analogWrite(ledPins[1], 0);
    analogWrite(ledPins[2], 0);
  }
}

Leave a Reply