Mood Lamp without changing the colors

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];


}

oldLed[3] = 0;Oops

Max is 2!

Railroader:
Max is 2!

Happy Birthday to Max!

A present.... array - Arduino Reference

Ok.I get it the array's mistake, but taht wasn't the problem.The leds still aren't changing it's colors.

So, post the code as it looks now.

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];


}
ledValue[3] =...

  analogWrite(red, ledValue);
    analogWrite(green, ledValue);
    analogWrite(blue, ledValue);
    delay(25);
  }

Oops

analogWrite needs a single value, not an array

Use // and comments in the code explain the idea.
Use Serial.print and Serial Monitor to print out strategic values during execution.

    analogWrite(red, ledValue);
    analogWrite(green, ledValue);
    analogWrite(blue, ledValue);

You are trying to pass an array where a byte is expected. Try this:

      analogWrite(red, ledValue[1]);
      analogWrite(green, ledValue[2]);
      analogWrite(blue, ledValue[3]);

groundFungus:

      analogWrite(red, ledValue[1]);

analogWrite(green, ledValue[2]);
      analogWrite(blue, ledValue[3]);

Ooops... [3]

Yeah, you are right. Array indexes start at 0. I need coffee.

for (int x = 1; x < 256; x++)
   {
      ledValue[0] = oldLed[0] + (x * (newLed[0] - oldLed[0]) / 255);
      ledValue[1] = oldLed[1] + (x * (newLed[1] - oldLed[1]) / 255);
      ledValue[2] = oldLed[2] + (x * (newLed[2] - oldLed[2]) / 255);
      analogWrite(red, ledValue[0]);
      analogWrite(green, ledValue[1]);
      analogWrite(blue, ledValue[2]);
      delay(25);
   }
   // Re setting the old array
   oldLed[0] = ledValue[0];
   oldLed[1] = ledValue[1];
   oldLed[2] = ledValue[2];

The problem was simple. I forgot the number specifying the element of the array in the digitalWrite.Thanks everyone for the help.

Reply #11.......?

Railroader:
Reply #11.......?

sp. "#9"

TheMemberFormerlyKnownAsAWOL:
sp. "#9"

Sorry, my mistake searching backwards finding #11 first.
Honor the proper helper, it's You.

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