Go Down

Topic: RGB led/led strip controlled bij IR remote (Read 398 times) previous topic - next topic

guido010

Jan 12, 2019, 08:42 pm Last Edit: Jan 12, 2019, 09:08 pm by guido010
hi.

welcome to my fist try to make an RGB led controlled by an IR remote.

so you are probably wondering why i would want this; so me and my dad bought an RGB Led led strip with controller for behind the television it broke and we did not wan't to buy a new one so i made this, it doesn't work for led strips on this moment then i would need to add some transistors and stuff and a RGB led strip to test it... so i started with this. it took me around 2 hours to code because i am not really good at coding, but i like it and it works for what i wanted to do.
the link for the library is at the bottom of the post
the photo is an attachment of the post because i did not know how to get it in the post
you wil probably need to change the IR receive codes because you are probably using a differed remote

i hope you like it and can use it.

Code: [Select]
#include <IRremote.h>

/*
PINS:
Red pin - Pin 8 on the arduino
Green pin - pin 9 on the arduino
Blue pin - pin 10 on the arduino
Receive pin from the IR receiver - pin 7 on the arduino

use the library from: https://github.com/z3t0/Arduino-IRremote
*/

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;

int Rpin = 8;
int Gpin = 9;
int Bpin = 10;

// Program variables
int redVal   = 255; // Variables to store the values to send to the pins
int greenVal = 1;   // Initial values are Red full, Green and Blue off
int blueVal  = 1;

int P = 0;     // Loop counter    
int wait = 10; // 50ms (.05 second) delay; shorten for faster fades

void setup(){
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
  irrecv.enableIRIn();
  irrecv.blink13(true);

  pinMode(Rpin, OUTPUT);
  pinMode(Gpin, OUTPUT);
  pinMode(Bpin, OUTPUT);
}

void loop(){
  if (irrecv.decode(&results)){
 
        if (results.value == 0XFFFFFFFF)
          results.value = key_value;

          Serial.println(results.value, HEX);

        switch(results.value){
          case 0xFF02FD:
          Serial.println("ON");
          setColor(0, 0, 0);  // on/white
          break;

          case 0xFF827D:
          Serial.println("OFF");
          setColor(255, 255, 255);  // off
          break;
          
          case 0xFF1AE5:
          Serial.println("Dark red");
          setColor(0, 255, 255);  // Dark red
          break;

          case 0xFF2AD5:
          Serial.println("light red");
          setColor(20, 245, 245);  // light red
          break;

          case 0xFF0AF5:
          Serial.println("dark orange");
          setColor(40, 235, 235);  // dark orange
          break;

          case 0xFF38C7:
          Serial.println("Orange");
          setColor(0, 102, 255);  // Orange
          break;

          case 0xFF18E7:
          Serial.println("Yellow");
          setColor(0, 0, 255);  // Yellow
          break;

          case 0xFF9A65:
          Serial.println("Less dark green");
          setColor(255, 72, 234);  // Less dark green
          break;
          
          case 0xFFAA55:
          Serial.println("Green");
          setColor(255, 0, 255);  // Green
          break;

          case 0xFF8A75:
          Serial.println("Dark green");
          setColor(255, 153, 255);  // Dark green
          break;

          case 0xFFB847:
          Serial.println("Less light blue");
          setColor(204, 51, 51);  // Less light blue
          break;

          case 0xFF9867:
          Serial.println("Darkest blue");
          setColor(255, 255, 0); // Darkest blue
          break;

          case 0xFFA25D:
          Serial.println("dark blue");
          setColor(255, 204, 153); // dark blue
          break;

          case 0xFF926D:
          Serial.println("less dark blue");
          setColor(250, 250, 25);  // less dark blue
          break;

          case 0xFFB24D:
          Serial.println("Black");
          setColor(250, 250, 250);  // Black
          break;

          case 0xFF7887:
          Serial.println("Brown");
          setColor(102, 204, 255);  // Brown
          break;

          case 0xFF58A7:
          Serial.println("Pink1");
          setColor(0, 255, 153);  // Pink1
          break;

          case 0xFF22DD:
          Serial.println("White");
          setColor(0, 0, 0);  // White
          break;

          case 0xFF12ED:
          Serial.println("Pink2");
          setColor(0, 255, 153);  // Pink2
          break;

          case 0xFF32CD:
          Serial.println("Pink3");
          setColor(0, 255, 153);  // Pink3
          break;

          case 0xFFF807:
          Serial.println("light blue1");
          setColor(102, 0, 51);  // light blue1
          break;

          case 0xFF30CF:
          Serial.println("Jump3");
          int i;
          for (i = 0; i < 5; i++)
          {
          Jump3();  // Jump3
          }
          break;

          case 0xFF906F:
          Serial.println("Flash");
          int o;
          for (o = 0; o < 5; o++)
          {
          Flash();
          }
          break;

          case 0xFFB04F:
          Serial.println("fade");
          int u;
          for (u = 0; u < 800; u++)
          {
          fade();
          }
          break;
        }
        key_value = results.value;
        irrecv.resume();
  }
}

void setColor(int red, int green, int blue)
{
  analogWrite(Rpin, red);
  analogWrite(Gpin, green);
  analogWrite(Bpin, blue);  
}

void Jump3()
{
  setColor(0, 255, 255);  // Dark red
  delay(1000);
  setColor(255, 72, 234);  // Less dark green
  delay(1000);
  setColor(255, 204, 153); // dark blue
  delay(1000);
}

void Flash()
{
  setColor(random(0, 255), random(0, 255), random(0, 255)); //pick a random collor
  delay(750);
  setColor(0,0,0);
  delay(500);
}



void fade()
{
  P += 1;      // Increment counter
  if (P < 255) // First phase of fades
  {
    redVal   -= 1; // Red down
    greenVal += 1; // Green up
    blueVal   = 1; // Blue low
  }
  else if (P < 509) // Second phase of fades
  {
    redVal    = 1; // Red low
    greenVal -= 1; // Green down
    blueVal  += 1; // Blue up
  }
  else if (P < 763) // Third phase of fades
  {
    redVal  += 1; // Red up
    greenVal = 1; // Green low
    blueVal -= 1; // Blue down
  }
  else // Re-set the counter, and start the fades again
  {
    P = 1;
  }  

  analogWrite(Rpin,   redVal);   // Write current values to LED pins
  analogWrite(Gpin, greenVal);
  analogWrite(Bpin,  blueVal);  
  delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}


link for the library: https://github.com/z3t0/Arduino-IRremote

SchnoppDog

#1
Jan 14, 2019, 09:53 am Last Edit: Jan 14, 2019, 09:54 am by SchnoppDog
So far so good, but one thing concerns me. I know that it's working, but the values of the colors are twisted. Normally you would it define like this:
Code: [Select]

setColor(255,255,255) //white

Since 255 is for full brightness and 0 for "off". Every Color-Picker normally has a RGB-Value. There you can see the normal values. So to get a full red you would normally type it as "setColor(255,0,0)" instead of "setColor(0, 255,255)".

I justed wanted to mention that, so nobody gets confused.

Maybe I am overseeing something. Correct me if I am wrong.

PaulRB

#2
Jan 14, 2019, 08:15 pm Last Edit: Jan 14, 2019, 08:27 pm by PaulRB
Quote
Maybe I am overseeing something. Correct me if I am wrong.
I think you mean "overlooking". "Overseeing" means managing/directing/being in charge of something.

On the other hand "an oversight" means something that was missed/forgotten. English is so confusing!

Anyway, the reason that 255=off and 0=on is not clear. Perhaps if you posted a schematic, we could tell you.

My best guess at the moment is that you are using a pair of transistors to drive each channel. For example, a non logic level MOSFET with a small npn pulling it's gate down to 0V with a pull-up resistor to 12V. The npn will be inverting the signal.

guido010

So far so good, but one thing concerns me. I know that it's working, but the values of the colors are twisted. Normally you would it define like this:
Code: [Select]

setColor(255,255,255) //white

Since 255 is for full brightness and 0 for "off". Every Color-Picker normally has a RGB-Value. There you can see the normal values. So to get a full red you would normally type it as "setColor(255,0,0)" instead of "setColor(0, 255,255)".

I justed wanted to mention that, so nobody gets confused.

Maybe I am overseeing something. Correct me if I am wrong.
yes i have seen that i also don't understand that but 255,255,255 is off for some reason idk why and also dont know how to fix it

Rintin

You forgot the resistors for the LED.


You use a LED with a common anode. The anode is tied to 5V. To let it glow, you need to pull the cathodes to ground.

An analogWrite(<pin>, 0) pulls it to ground all the time. -> Bright light
An analogWrite(<pin>, 255) pull it to 5V all the time. -> Off

WHen you want setColor(255, 255, 255) to be white, you need to invert the values. Something like analogWrite(Rpin, 255 - red); should do the trick for red.

Go Up