I' m a beginner at programming and even English so forgive me my mistakes. I'm studying the basic book for arduino programming and my device is the Arduino Uno. I'm have been trying to make a mood lamp(basically 3 led (Red, Green, Blue) that chances gradually it's colors intensity making them look like a single color that changes all the time smoothly).Instead of doing exatly what the book says i tried to do my on code but isn't working so i would like yours help.Basically all the 3 leds stay turned on but has no change in its intensity.Thanks for the help.The code is this:
// Mood Lamp
// Definning arrays
float oldLed[3]; // the last led value
float newLed[3]; // the new led value
float ledValue[3]; // the adding value
// Selecting Gates
int red = 11;
int green = 10;
int blue = 6;
// Obs: 1 = Red, 2 = Green, 3 = Blue
void setup() {
// Initial value for the leds
oldLed[1] = 0;
oldLed[2] = 0;
oldLed[3] = 0;
randomSeed(analogRead(0)); // Where to get the random numbers
}
void loop() {
newLed[1] = random(256);
newLed[2] = random(256);
newLed[3] = random(256);
// Gradualy changing the led Value
for (int x = 1; x < 256; x++) {
ledValue[1] = oldLed[1] + (x * (newLed[1] - oldLed[1]) / 255);
ledValue[2] = oldLed[2] + (x * (newLed[2] - oldLed[2]) / 255);
ledValue[3] = oldLed[3] + (x * (newLed[3] - oldLed[3]) / 255);
analogWrite(red, ledValue);
analogWrite(green, ledValue);
analogWrite(blue, ledValue);
delay(25);
}
// Resetting the old array
oldLed[1] = ledValue[1];
oldLed[2] = ledValue[2];
oldLed[3] = ledValue[3];
}
Put some serial prints in the code to monitor the values of the variables as they are calculated. You will need to insert the serial port initialization in setup(). Make sure that the serial monitor baud rate matches the code.
Serial.begin(9600); // initialize serial port to 9600 baud
The code after I fixed the arrays.(Still don't know how to use the serial print, sorry).
// Mood Lamp
// Definning arrays
float oldLed[3]; // the last led value
float newLed[3]; // the new led value
float ledValue[3]; // the adding value
// Selecting Gates
int red = 11;
int green = 10;
int blue = 6;
// Obs: 0 = Red, 1 = Green, 2 = Blue
void setup() {
// Initial value for the leds
oldLed[0] = 0;
oldLed[1] = 0;
oldLed[2] = 0;
randomSeed(analogRead(0)); // Where to get the random numbers
}
void loop() {
newLed[0] = random(256);
newLed[1] = random(256);
newLed[2] = random(256);
// Gradualy changing the led Value
for (int x = 1; x < 256; x++) {
ledValue[1] = oldLed[0] + (x * (newLed[0] - oldLed[0]) / 255);
ledValue[2] = oldLed[1] + (x * (newLed[1] - oldLed[1]) / 255);
ledValue[3] = oldLed[2] + (x * (newLed[2] - oldLed[2]) / 255);
analogWrite(red, ledValue);
analogWrite(green, ledValue);
analogWrite(blue, ledValue);
delay(25);
}
// Re setting the old array
oldLed[0] = ledValue[1];
oldLed[1] = ledValue[2];
oldLed[2] = ledValue[3];
}