Moin,
ich versuche mich gerade an einer kleinen Schaltung für den Arduino.
Per Button möchte ich 2 LED ansteuern.
Beim Start sind beide LED aus.
Drücke ich den Knopf geht die erste LED an
Drücke ich den Knopf wieder geht die erste aus und die zweite an
Drücke ich wieder geht die zweite wieder aus.
0 .. 1 .. 2 .. 0 ..1 .. etc
Dazu habe ich folgendes Skript geschrieben:
// constants won't change. Used here to set a pin number:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin1 = 13;
const int ledPin2 = 12;
// Variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int ledState1 = LOW;
int ledState2 = LOW;
int mode = 0;
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// set the digital pin as output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void mode0 () {
ledState1 = LOW;
ledState2 = LOW;
mode = "0";
}
void mode1 () {
ledState1 = HIGH;
ledState2 = LOW;
mode = "1";
}
void mode2 () {
ledState1 = LOW;
ledState2 = HIGH;
mode = "2";
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// check mode
if (mode == 0) {
mode1 ();
}
if (mode == 1) {
mode2 ();
}
if (mode == 2) {
mode0 ();
}
digitalWrite(ledPin1, ledState1);
digitalWrite(ledPin2, ledState2);
delay (500);
}
}
Der erste klick schaltet auch tatsöchlich die erste LED an.
Danach funzt nix mehr.
Was mache ich falsch?
Cappy