Controlling 2 Colour LED strip

hi friends ,

i need some help with the coding ,
i am trying to control two colour LED strip (White and Warm White) with the help of IR remote.

i have to assign total 5 buttons which do following task :

1st button - on/off both the led
2nd button - colour tune from yellow colour to white ( PWM)
3rd button - vice versa of 2nd button
4th button - brightness + (keeping the previous settings)
5th button - brightness -

following is my code which is not wokring properly with the tuning of colour and brightness part

#include <IRremote.h>

int out=9;
int out2=6;
int before;
int white;
int yellow;
int steps=5;
const int IR_RECEIVE_PIN = 10;

void setup() {
   Serial.begin(115200); //Start Serial monitor in 115200
   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
   before=0; //LED is turned off
  white=255;
  yellow=255;
   pinMode(out,OUTPUT);
   pinMode(out2,OUTPUT);
}


void loop() {

  if (IrReceiver.decode()) {

        // value from receiver in Living room
         if (IrReceiver.decodedIRData.decodedRawData == 0xED127F80) {
                if(before==0){ 
      digitalWrite(out,HIGH);
      before=1; 
    }
   
    else {
         digitalWrite(out,LOW);
         digitalWrite(out2,LOW);//if the LED was turned on, then we turn it off
      before=0;
      white=255;
      yellow=255;  
    
           }}
         if (IrReceiver.decodedIRData.decodedRawData == 0xFD027F80 && before==1){ //Code to switch to yellow tone
      if(white-255/steps<0 && yellow+255/steps>255){ 
      analogWrite(out,white);
      analogWrite(out2,yellow);
    }
    else{
    white=white-255/steps;
    yellow=yellow+255/steps;
    analogWrite(out,white);
    analogWrite(out2,yellow);
  }}

   if (IrReceiver.decodedIRData.decodedRawData == 0xFC037F80 && before==1) { //Code to switch to white tone
    if(white+255/steps>255 && yellow-255/steps<0){
      analogWrite(out,white);
      analogWrite(out2,yellow);

    }
    else{    
    white=white+255/steps;
    yellow=yellow-255/steps;
    analogWrite(out,white);
    analogWrite(out2,yellow);
  }}

  if (IrReceiver.decodedIRData.decodedRawData == 0xFA057F80 && before==1){ //Code to decrease the brightness
      if(white-255/steps<0 && yellow-255/steps<0){ 
      analogWrite(out,white);
      analogWrite(out2,yellow);
    }
    else{
    white=white-255/steps;
    yellow=yellow-255/steps;
    analogWrite(out,white);
    analogWrite(out2,yellow);
  }}

   if (IrReceiver.decodedIRData.decodedRawData == 0xF9067F80 && before==1) { //Code to increase the brightness
    if(white+255/steps>255 && yellow+255/steps>255){
      analogWrite(out,white);
      analogWrite(out2,yellow);

    }
    else{    
    white=white+255/steps;
    yellow=yellow+255/steps;
    analogWrite(out,white);
    analogWrite(out2,yellow);
  }}
  
     
    }
       IrReceiver.resume(); 
  }
Step 1 works. Step 2, 3, 4, 5 do not work. What is step 5 supposed to do?

Is this correct?

I tested your code.

  1. Works. It turns white and yellow on then off... but you forgot to include the line for the yellow LED.
  2. Works. BUT you must use #1 to turn white and yellow ON first, then pres #2 50 times.
  3. Works. BUT you must use #1 to turn white and yellow ON first, then pres #3 50 times.
  4. Works. BUT you must use #1 to turn white and yellow ON first, then press #4 50 times.
  5. Works. BUT you must use #1 to turn white and yellow ON first, then press #4 50 times.

I am quite sure most of your problems were from giving non-descriptive names to all your variables. We are past the days of assembly and BASIC. You can name the yellow LED pin "YellowLedPin" rather than "out2" and button 1 "Button1" rather than "0xED127F80".

I added ONE line and renamed all of your "non-descriptive" variables.

#include <IRremote.h>

int yellowLedPin = 9;
int whiteLedPin  = 6;
int before = 0; // LEDs are turned OFF
int whitePWMvalue = 255;
int yellowPWMvalue = 255;
int steps = 5;
const int IR_RECEIVE_PIN = 10;

#define BUTTON1 0xED127F80
#define BUTTON2 0xFD027F80
#define BUTTON3 0xFC037F80
#define BUTTON4 0xFA057F80
#define BUTTON5 0xF9067F80

void setup() {
  Serial.begin(115200); //Start Serial monitor in 115200
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
  pinMode(whiteLedPin, OUTPUT);
  pinMode(yellowLedPin, OUTPUT);
}



void loop() {

  if (IrReceiver.decode()) {

    // value from receiver in Living room
    if (IrReceiver.decodedIRData.decodedRawData == BUTTON1) {
      if (before == 0) {
        digitalWrite (whiteLedPin, HIGH);
        digitalWrite(yellowLedPin, HIGH); // this line was missing
        before = 1;
      }

      else {
        digitalWrite(whiteLedPin, LOW);
        digitalWrite(yellowLedPin, LOW); //if the LED was turned on, then we turn it off
        before = 0;
        whitePWMvalue = 255;
        yellowPWMvalue = 255;

      }
    }
    if (IrReceiver.decodedIRData.decodedRawData == BUTTON2 && before == 1) { //Code to switch to yellow tone
      if (whitePWMvalue - 255 / steps < 0 && yellowPWMvalue + 255 / steps > 255) {
        analogWrite(whiteLedPin, whitePWMvalue);
        analogWrite(yellowLedPin, yellowPWMvalue);
      }
      else {
        whitePWMvalue = whitePWMvalue - 255 / steps;
        yellowPWMvalue = yellowPWMvalue + 255 / steps;
        analogWrite(whiteLedPin, whitePWMvalue);
        analogWrite(yellowLedPin, yellowPWMvalue);
      }
    }


    if (IrReceiver.decodedIRData.decodedRawData == BUTTON3 && before == 1) { //Code to switch to white tone
      if (whitePWMvalue + 255 / steps > 255 && yellowPWMvalue - 255 / steps < 0) {
        analogWrite(whiteLedPin, whitePWMvalue);
        analogWrite(yellowLedPin, yellowPWMvalue);

      }
      else {
        whitePWMvalue = whitePWMvalue + 255 / steps;
        yellowPWMvalue = yellowPWMvalue - 255 / steps;
        analogWrite(whiteLedPin, whitePWMvalue);
        analogWrite(yellowLedPin, yellowPWMvalue);
      }
    }

    if (IrReceiver.decodedIRData.decodedRawData == BUTTON4 && before == 1) { //Code to decrease the brightness
      if (whitePWMvalue - 255 / steps < 0 && yellowPWMvalue - 255 / steps < 0) {
        analogWrite(whiteLedPin, whitePWMvalue);
        analogWrite(yellowLedPin, yellowPWMvalue);
      }
      else {
        whitePWMvalue = whitePWMvalue - 255 / steps;
        yellowPWMvalue = yellowPWMvalue - 255 / steps;
        analogWrite(whiteLedPin, whitePWMvalue);
        analogWrite(yellowLedPin, yellowPWMvalue);
      }
    }

    if (IrReceiver.decodedIRData.decodedRawData == BUTTON5 && before == 1) { //Code to increase the brightness
      if (whitePWMvalue + 255 / steps > 255 && yellowPWMvalue + 255 / steps > 255) {
        analogWrite(whiteLedPin, whitePWMvalue);
        analogWrite(yellowLedPin, yellowPWMvalue);

      }
      else {
        whitePWMvalue = whitePWMvalue + 255 / steps;
        yellowPWMvalue = yellowPWMvalue + 255 / steps;
        analogWrite(whiteLedPin, whitePWMvalue);
        analogWrite(yellowLedPin, yellowPWMvalue);
      }
    }

  }
  IrReceiver.resume();
}
1 Like

Hi ,
Thank you for your input.

First thanks for your advice, i will keep it in mind from next time.

Second, the problem i am facing with the code is , let say after setting the Colour tone by button2 or button3 , when i press button4 to adjust the brightness , it turns on both the leds (loosing the previous state)

There is no difference in "tone" or "brightness" when adjusting the PWM. You can not change a standard LED color.

1 Like

The large memory usage is probably the IRremote information.

Search for "attiny85 irremote library"

This link describes the pulse decoding needed to replace the library...
http://www.technoblogy.com/show?24A9

1 Like

Thank you soo soo much for your help so far :pray: i have tried and it is working. Just last thing i want to ask , when i power on the microcontroller, the whiteledpin and yellowledpin should be HIGH .

I already tried to put this command before the if function but it start to act weird :

digitalWrite (whiteLedPin, HIGH);
digitalWrite (yellowLedPin, HIGH);
before = 3

[quote="sepreta, post:7, topic:1221605"]
act weird
[/quote]

Describe this.

Move the two LED lines from Post#7 into the setup() function. I think "weird" is happening because your code makes those two LEDs turn on (FULL/255) at the start of every loop().

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.