const int redPin = 2; const int greenPin = 3; const int bluePin = 4; const int buttonPin = 20; int i = 0; int colors[7][3] = { {0, 255, 255}, // Red {255, 0, 255}, // Green {255, 255, 0}, // Blue {0, 0, 255}, // Yellow {255, 0, 0}, // Cyan {0, 255, 0}, // Magenta {0, 0, 0} // White }; int lastButtonState = LOW; int buttonState = LOW; int colorIndex = 0; unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 50; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); setColor(colorIndex); } void loop() { int reading = digitalRead(buttonPin); // If the button state changed (press detected) if (reading != lastButtonState) { lastDebounceTime = millis(); // Reset debounce timer } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; // only toggle the LED if the new button state is HIGH and sufficient time has passed since last "press" (bounce) if (buttonState == HIGH) { colorIndex = (colorIndex + 1) % 7; setColor(colorIndex); i++; } } } lastButtonState = reading; } void setColor(int index) { analogWrite(redPin, colors[index][0]); analogWrite(greenPin, colors[index][1]); analogWrite(bluePin, colors[index][2]); }