I'm new to Arduino, C++ and programming in general. I need to figure out a way to get each press of the push button to cycle through the RGB LED from red, green, blue, yellow, magenta, cyan and white. I can get all seven colors to cycle through on a delay but I cannot figure out how to change this to being controlled by the push button. Perhaps I've used the wrong search terms but everything that I find is for simply turning an LED on and off. I previously figured out how to do that but my understanding isn't strong enough to translate this to each button press causing a change in the color. Here is my code for the on/off push button
int ledState = LOW;
int lastKnownButtonState = LOW;
unsigned long lastTimeButtonChanged = 0;
unsigned int debounceDelay = 50;
int buttonState = LOW;
unsigned int ledPin = 13;
unsigned int buttonPin = 8;
void setup() {
pinMode( ledState, LOW);
pinMode( lastKnownButtonState, LOW);
pinMode( lastTimeButtonChanged, 0);
pinMode( buttonState, LOW);
pinMode( ledPin, OUTPUT);
pinMode( buttonPin, INPUT);
}
void loop() {
int buttonValue = digitalRead(buttonPin);
if (buttonValue != lastKnownButtonState) {
lastTimeButtonChanged = millis();
}
if((millis() - lastTimeButtonChanged) > debounceDelay) {
if (buttonValue != buttonState) {
buttonState = buttonValue;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
digitalWrite(ledPin, ledState);
lastKnownButtonState = buttonValue;
}
And this is my code for cycling through the colors on a delay
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
///Activates pin 9 for the red anode
digitalWrite(redPin, HIGH); //Red
delay(1000);
//Turns of the RGB LED
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(greenPin, HIGH); //Green
delay(1000);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(bluePin, HIGH); //Blue
delay(1000);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(redPin, HIGH); //Yellow
digitalWrite(greenPin, HIGH);
delay(1000);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(redPin, HIGH); //Magenta
digitalWrite(bluePin, HIGH);
delay(1000);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(greenPin, HIGH); //Cyan
digitalWrite(bluePin, HIGH);
delay(1000);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(redPin, HIGH); //White
digitalWrite(bluePin, HIGH);
digitalWrite(greenPin, HIGH);
delay(1000);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
If someone could help me out with this or point me to something that would help me I would really appreciate because I'm lost. Thanks