I am trying to get a rgb colour LED to change through each color every button push. I am having a hard time because the arduino is processing to fast. I want it to wait for the button to be let go before changing the variable "state". What it does right now is that when the button is pressed, it immediately changes the state from 1 to 2 to 3. I want it to change only when i let go of the button. Thank you for trying.
int pushButton = 7;
int r = 2;
int g = 3;
int b = 4;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop() {
int state = digitalRead(pushButton);
if (state == 1) {
digitalWrite(r, HIGH);
state = state + 1;
}
if (state == 2) {
digitalWrite(b, HIGH);
digitalWrite(r, LOW);
state = state + 2;
}
if (state == 3) {
digitalWrite(b, LOW);
digitalWrite(r, LOW);
digitalWrite(g, HIGH);
}
if (state == 0) {
digitalWrite(r, LOW);
digitalWrite(b, LOW);
digitalWrite(g, LOW);
}
Serial.println(state);
delay(1);
}
Aucun commentaire:
Enregistrer un commentaire