[solved] Random RGB lamp

Hello folks

Since I started with arduino I wanted to make an RGB lamp for the ambiance I did some attempts with out results but now I have this really simple code that change a random color each 4sec. it gets a random number between 0 and 255 for each color and using PWM writing down on each color, and it works it change the color each 4 sec.

int pinRojo=10;
int pinVerde=6;
int pinAzul=9;

int del=4000;

void setup(){

  pinMode(pinRojo,OUTPUT);
  pinMode(pinVerde,OUTPUT);
  pinMode(pinAzul,OUTPUT);
}

void loop(){
colorSetup(random(0,255),random(0,255),random(0,255));
delay(del);
}

void colorSetup (byte rojo, byte verde, byte azul){ 
  
analogWrite(pinRojo,rojo);
  analogWrite(pinVerde,verde);
  analogWrite(pinAzul,azul);
  
  }

It also looks good but I'm thinking that smooth the changing of color until the new color will be nicer instead of changing suddenly form color to color because the colors could bee really different...

Any idea ?

Hi ehrja,

Lets say you want to move smoothly from one colour to the next in 2 seconds. To make the change smooth to the eye, you need many changes in each second, perhaps 50. Thats 100 changes in total, with a 20ms pause between each change.

So pick your next random colour but also keep your current colour also. Use a for loop to perform the 100 steps and use the map () function for red, green and blue to work out the colour at each step.

Paul

If you want some code that already does this to see how it works, look at the RGB mood lap sketch at my code repository, link in the signature below.

Try this (I cannot test it for you but it compiles OK).

int pinRojo=10;
int pinVerde=6;
int pinAzul=9;

int del=4000;

void setup(){

  pinMode(pinRojo,OUTPUT);
  pinMode(pinVerde,OUTPUT);
  pinMode(pinAzul,OUTPUT);
}

void loop(){
colorSetup(random(0,255),random(0,255),random(0,255));
delay(del);
}

void colorSetup (byte rojo, byte verde, byte azul){ 
  
  static byte rojo2, verde2, azul2;
  
  for (byte i = 1; i <=100; i++) {
  
    analogWrite(pinRojo,map(i, 1, 100, rojo2, rojo));
    analogWrite(pinVerde,map(i, 1, 100, verde2, verde));
    analogWrite(pinAzul,map(i, 1, 100, azul2, azul));
    delay(20);
    
    }
    
  rojo2 = rojo;
  verde2 = verde;
  azul2 = azul;
  
  }

marco_c

I couldn't make it work with my leonardo pro mini, I change the pines and nothing, by the way I see some libraryes that seem to be interesting for me because i have a project, already finished, with a 4digit 7 segment display, I'll check your code about that. thank you.

PaulRB

Yesterday I check the reference of map() the example looks good for me but I never hear about map() function...
I was thinking in the shower about that and I don't get it yet, I just have the last part about keeping the values of the last variables, but that is obvious .
well the code that you give me does the job pretty well and is easy to understand.

the function map() is something to add to the tool box.

by the way in this example we can adjust the transition time and the exposition time.
woow, is like the Christmas tree.

in the pictures can't show the transition, but it is really smooth between colors.
Thanks PaulRB