const int buttonPin = 2;
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
int buttonState = 0;
int colorIndex = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
colorIndex = (colorIndex + 1) % 7;
switch (colorIndex) {
case 0:
setColor(255, 0, 0);
break;
case 1:
setColor(0, 255, 0);
break;
case 2:
setColor(0, 0, 255);
break;
case 3:
setColor(0, 255, 255);
break;
case 4:
setColor(255, 0, 255);
break;
case 5:
setColor(255, 255, 0);
break;
case 6:
setColor(255, 255, 255);
break;
}
delay(100);
}
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}