Help debug very simple program - Pushbutton to select random color for LED


I am trying to write a simple program change the color of a RGB LED once a pushbutton is pressed. I am getting strange results. Once the pushbutton is pressed, the led changes to 3 different colors (separated by 1 second delay) before settling down on a final color. Why is this?

Here is the code:

//RGB LED pins

int ledDigitalOne[] = {11, 12, 13}; //the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePin

const boolean ON = LOW;      //Define on as LOW (this is because we use a common Anode RGB LED (common pin is connected to +5 volts)
const boolean OFF = HIGH;   //Define off as HIGH

int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status
int randx = 0;

//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};    
const boolean GREEN[] = {OFF, ON, OFF}; 
const boolean BLUE[] = {OFF, OFF, ON}; 
const boolean YELLOW[] = {ON, ON, OFF}; 
const boolean CYAN[] = {OFF, ON, ON}; 
const boolean MAGENTA[] = {ON, OFF, ON}; 
const boolean WHITE[] = {ON, ON, ON}; 
const boolean BLACK[] = {OFF, OFF, OFF}; 

//An Array that stores the predefined colors (allows us to later randomly display a color)

void setup(){

    for(int i = 0; i < 3; i++){
       pinMode(ledDigitalOne [ i ] , OUTPUT);   //Set the three LED pins as outputs
    pinMode(inPin, INPUT);    // declare pushbutton as input


void loop(){

      setColor(ledDigitalOne, COLORS[randx]);    //Set the color of led one to a random color
     val = digitalRead(inPin);  // read input value
     if (val == HIGH) {      // check if the input is HIGH (button pushed)
          randx = random(0, sizeof(COLORS) / 2);   //get a random number within the range of colors

void setColor(int* led, boolean* color){
   for(int i = 0; i < 3; i++){
      digitalWrite(led[i], color[i]);

// A version of setColor that allows for using const boolean colors

void setColor(int* led, const boolean* color){
  boolean tempColor[] = {color[0], color[1], color[2]};
  setColor(led, tempColor);

As you can see when the pushbutton is pressed a new random number should be selected and when the loop is run again the new random number should be used to select the color from the color array. But I wonder why when I push the button is just doesn’t select a new color and stay on that color, instead of cycling through 3-4 different colors then settling on one color.

Any help debugging this code would be appreciated! Thanks

I am guessing your inPin does not have a pullup or pulldown resistor, so it floats around for a while.

Try this:
pinMode(inPin, INPUT_PULLUP); // declare pushbutton as input with internal pullup resistor enabled

and this:
if (val == LOW) { // check if the input is LOW from the pressed button connecting it to Gnd.

Hmm when I use 'pinMode(inPin, INPUT_PULLUP); '

it reports that INPUT_PULLUP was not declared in this scope. How do I fix this?

also I have the pushbutton currently wired to +5V and the other end to digital pin 7, should it be GND to pin 7 instead?

Actually my code is acting really strange, even when I don’t push the button it keeps flipping through the colors. I am very confused

Use this then

pinMode(inPin, INPUT);
digitalWrite (inPin, HIGH);

Want button to connect pin to Gnd when pressed. No connections made to +5V, the internal pullup takes care of that.

Set up the pin modes OUT of the scope its currently in. I’m guessing its in a user function, why is your pinModes NOT in setup()/

Me thinks this will be a problem;

for(int i = 0; i < 3; i++){
       pinMode(ledDigitalOne, OUTPUT)

Shouldn’t it be

for(int i = 0; i < 3; i++){
       pinMode(ledDigitalOne[i], OUTPUT)

@Hrendor66 - The code I had was this way with the ledDigitalOne [ i ] . The forum removed the brackets though as I didn't format the text for code. I am not sure how you insert formatted code? Also I setup the pins in the set() function. Thanks so much for checking the code and helping to debug!

@ CrossRoads - It works now! Thanks so much, I had the whole pushbutton thing wrong. Now it works as expected! Thanks so much, I learned a lot in the process. Thanks!

Way to go then. On to more complex things!