Pressing a button to change the color of a RGB LED

Hi,

I want my Chainable RGB LED to switch to another color every time I press my pushbutton.
So for instance, when i press it once the LED goes green, when i press it again it goes red and so on.

I am struggling to find the best way to do this and get the code right, can anyone help me with this?

I am also using a base shield on my arduino.

Cheers

Post your code between code tags

** **[code]** **
your code here
** **[/code]** **
will result in

 your code here

Point out where your problem is;what you expect your code to do and what it actually does.

Hint:
use a global variable (or static variable) to remember which colour you're currently showing.

I want my Chainable RGB LED to switch to another color every time I press my pushbutton.

Do you want the colour to change whilst the button is pressed and to remain at the current colour when the button is released or, more likely, do you want the colour to change when the button becomes pressed and for the colour to remain the same when the button is released ? If the latter then look at the StateChangeDetection example in the IDE.

Do you want to have control over the sequence of colours or would you like it to be random ? If you want to control the sequence then how many colours will be in the sequence ?

I do not have a code yet, I am trying to figure out how i am going to program this. I want the three colors to be and to go from green to yellow and finally to red, and as you said I want the color to change when the button becomes pressed. So I'm looking at a StateChangeDetection right?

So I'm looking at a StateChangeDetection right?

Yes.
Depending on the quality of your pushbutton you may need to deal with the problem of the switch contacts bouncing and producing several on/off sequences for one button press but I suggest that you put that aside for now and keep things simple.

Do you know how to display the colours on the LEDs ?

The state change detection example assumes an active high switch (on = high). The switch is wired from input to Vcc. That requires an external pull down resistor to keep the input from floating when the switch is open (off). A better way is to wire the switch from ground to the input and enable the internal pullup with pinMode(pin, INPUT_PULLUP). The logic is reversed, LOW is pushed (on) and HIGH is not pushed (off). If the switch is wired that way a cap (0.1uf) across the switch should provide hardware debounce.

I've done it! Thanks for the helpful replies!

This topic can be closed :slight_smile:

4365135:
This topic can be closed

Topics here don't get closed, but you might like to edit the title of the first post and add [solved] or similar. Why not post your code for others to pick holes in er I mean learn from?

Don't be afraid to post your code for comment and for others to learn from in the future. The most important thing is that it works, so well done.