Using state's to control RGB (Tri-Color) led using a push button

I need to change the code to check the state of the push-button. The first time the button is pressed, freeze the current colour. On the second and subsequent presses, jump to a new (random) colour and freeze on the new colour.

I tried using states as per the Arduino tutorial, but its not working.

My button is on Pin 10

int red = 12;
int green = 14;
int blue = 15;

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;

void setup()
{ 
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
redNow = random(255);
blueNow = random(255);
greenNow = random(255);
redNew = redNow;
blueNew = blueNow;
greenNew = greenNow;
}

#define fade(x,y) if (x>y) x--; else if (x<y) x++;

void loop() { 
        if ((redNow == redNew) && 
          (blueNow == blueNew) && 
          (greenNow == greenNew)) 
        { 
                redNew = random(255); 
                blueNew = random(255); 
                greenNew = random(255); 
        } 
        fade(redNow,redNew) 
        fade(blueNow,blueNew) 
        fade(greenNow,greenNew) 
        analogWrite(blue, blueNow); 
        analogWrite(red, redNow); 
        analogWrite(green, greenNow); 
        delay(20); 
}

regios287:
I tried using states as per the Arduino tutorial, but its not working.

Please post that sketch and we will try to see what is wrong. The sketch you posted has no code for handling a button.

It sorta sounds like you confuse button state, and button toggle.
Maybe what you are wanting is: While the button state is pressed, find random light numbers, then when released, hold that number.
Could that be what you want?

except that on start up (it sounds) as if it auto generating 'colors'.. and the first press stops it.

seems though that setting a simple variable would help/accomplish though.. (to get the initial pus out the way)..

and then you suggested.. you might be able ot break it up into 2 separate states/processes..

although you could just keep it as 1 event (randomly generate color, write & stop/pause)

TwoThree things:

(1) Those buttons don't always make good contact with a breadboard as their legs are too short.

(2) You have wired up your button wrong. The pulldown resistor should be connected to the same pin of the switch as the wire going to the Arduino input. You have connected it to the wire going to the power supply.

(3) Have you connected the red power rails on each side of the breadboard? You have connected VCC to the red line on one side, but connected the button to the red line on the other side - these two lines aren't connected unless you add a wire to do so.