-3

I am attempting to create a program that will randomly choose a PWM value for a RGB LED from a given Array. It works fine with the first color, blue. Bu then I nest in a second color, green, I loose the blue from displaying and only the green displays.

void loop() {
  // put your main code here, to run repeatedly:

  int x[9] = {0, 32, 64, 96, 128, 160, 192, 224, 256};  //setup Array X for brightness options
  int blueVariable = 0;                                 //Blue LED
  int greenVariable = 0;                                //Green LED
  for (int blueLed = 0; blueLed > -1; ) {               //for loop to choose PWM option
    analogWrite(11, x[blueVariable]);                   //Initilize the PWM function on pin 11 to brightness of blueVariable
  //  if (blueLed == 255) blueLed = 0;                    //
    blueVariable = random(0,8);                         //Random function to decide on blueVariable value
  delay(500);


     for (int greenLed = 0; greenLed > -1; ) {
       analogWrite(10, x[greenVariable]);
      //  if (g == 255) g = 0;             // switch direction at peak
        greenVariable = random(0,255);
     delay(500);
     }
  }

}
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    Formatting your code properly would be a huge help to everyone (yourself included). – Paul R Feb 22 '17 at 07:38
  • 4
    Why `greenVariable = random(0,255)`, you only have 9 brightness values. Moreover your loops are endless, there is no exit condition. – Michael Feb 22 '17 at 08:01
  • Also I see no **PWM** here possibly that is what `analogWrite` function is for but without context we do not know what it does. What are the operands? How is your LED connected to MCU ? Which MCU you got ? What PWM are you usnig (SW,Timer/Counter, PWMA module) ? Arduino is not magic word it is just a framework so you do not see/understand what are you doing ... Where is this code placed/called (main thread, ISR, ... )? – Spektre Feb 22 '17 at 09:23
  • Michael, your right that was from a previous coding that should have been changed. – Nicholas Rice Feb 22 '17 at 14:34

1 Answers1

1

You have two Problems:

First you hooked your "for loop" for the green color in(!) the for loop for the Blue color. Based on the fact that the loops running infinite you only loop through the second for loop.

The second Problem (perhaps not a problem, but the reason why you don't see Blue) is your initialization of the blueVariable as 0. If you run the first time, you write the value 0 to the PWM Pin. After that you change the variable, but do not write to the PWM Pin, because you get stuck in your "infinite green Loop".

Btw, like said in the comments from Michael, you should change the 255 to 8 AND in your array you should change the last value (256) to 255 because the 8bit PWM means 256 values from 0-255.

Example:

int x[9] = {0, 32, 64, 96, 128, 160, 192, 224, 255};    // Changed Value

void loop() {
  int blueVariable = 0;                                 //Blue LED
  int greenVariable = 0;                                //Green LED

  while(1) {                                            // Because it was infinite already i changed it to while(1)
    blueVariable = random(0,8);                         //Put in front of analogWrite()
    analogWrite(11, x[blueVariable]);                   
    delay(500);

    // Deleted the scond loop
    greenVariable = random(0,8);                        // Value changed from 255 to 8; Also put in front of analogWrite
    analogWrite(10, x[greenVariable]);
    delay(500);
  }        
}
H. Puc
  • 77
  • 1
  • 8
  • Thank you H. Puc. some of the errors were from changing the code from a increasing and decreasing bi rightness(hence the for loop), I should have started each attempt from scratch not modified the existing code. After reading your edits, and applying them I see where my mistake was. In fact I was able to properly add the red portion of my RGB in to proper working order. – Nicholas Rice Feb 22 '17 at 14:32