verwende Funktionen für Zustandsänderung
This commit is contained in:
parent
5de12c41bb
commit
e28463d0d9
1 changed files with 62 additions and 25 deletions
|
@ -27,8 +27,9 @@ const int LED_G = 9; // grüne LED
|
||||||
const int LED_Y = 8; // gelbe LED
|
const int LED_Y = 8; // gelbe LED
|
||||||
const int LED_R = 7; // rote LED
|
const int LED_R = 7; // rote LED
|
||||||
|
|
||||||
// hier wird der aktuelle Zustand gespeichert
|
// hier wird der aktuelle und vorherige Zustand gespeichert
|
||||||
byte state = STATE_OFF;
|
byte state_current = NULL;
|
||||||
|
byte state_previous = NULL;
|
||||||
|
|
||||||
// hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime.
|
// hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime.
|
||||||
unsigned long stateBegan;
|
unsigned long stateBegan;
|
||||||
|
@ -57,7 +58,7 @@ void setup() {
|
||||||
pinMode(LED_R, OUTPUT);
|
pinMode(LED_R, OUTPUT);
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
testLeds();
|
testLeds();
|
||||||
setStateOnLeds();
|
changeStateTo(STATE_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Schaltet alle LEDs nacheinander an
|
// Schaltet alle LEDs nacheinander an
|
||||||
|
@ -70,11 +71,56 @@ void testLeds() {
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bildet den aktuellen Zustand auf die LEDs ab
|
// wechselt zu neuen Zustand
|
||||||
void setStateOnLeds() {
|
void changeStateTo(byte state_new) {
|
||||||
digitalWrite(LED_R, (state == STATE_OFF));
|
state_previous = state_current;
|
||||||
digitalWrite(LED_Y, (state == STATE_HALF));
|
state_current = state_new;
|
||||||
digitalWrite(LED_G, (state == STATE_ON));
|
transition();
|
||||||
|
}
|
||||||
|
|
||||||
|
// behandelt die Zustandübergänge
|
||||||
|
boolean transition() {
|
||||||
|
if (state_previous == STATE_OFF && state_current == STATE_ON) {
|
||||||
|
digitalWrite(LED_R, LOW);
|
||||||
|
digitalWrite(LED_G, HIGH);
|
||||||
|
stateBegan = millis();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (state_previous == STATE_ON && state_current == STATE_ON) { // STATE_ON ist reflexiv
|
||||||
|
stateBegan = millis();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (state_previous == STATE_ON && state_current == STATE_HALF) {
|
||||||
|
digitalWrite(LED_G, LOW);
|
||||||
|
digitalWrite(LED_Y, HIGH);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (state_previous == STATE_ON && state_current == STATE_OFF) {
|
||||||
|
digitalWrite(LED_G, LOW);
|
||||||
|
digitalWrite(LED_R, HIGH);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (state_previous == STATE_HALF && state_current == STATE_OFF) {
|
||||||
|
digitalWrite(LED_Y, LOW);
|
||||||
|
digitalWrite(LED_R, HIGH);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (state_previous == NULL && state_current == STATE_OFF) {
|
||||||
|
digitalWrite(LED_G, LOW);
|
||||||
|
digitalWrite(LED_Y, LOW);
|
||||||
|
digitalWrite(LED_R, HIGH);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// information über aktuellen Zustand auf die Serielle Verbindung schreiben
|
||||||
|
void sendState() {
|
||||||
|
if (state_current == STATE_ON || state_current == STATE_HALF) {
|
||||||
|
Serial.print("1");
|
||||||
|
} else {
|
||||||
|
Serial.print("0");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long calcStateTime() {
|
unsigned long calcStateTime() {
|
||||||
|
@ -90,34 +136,25 @@ unsigned long calcStateTime() {
|
||||||
void loop() {
|
void loop() {
|
||||||
// Einschalter auslesen
|
// Einschalter auslesen
|
||||||
if (debounceBtnOn.update() && debounceBtnOn.read()) {
|
if (debounceBtnOn.update() && debounceBtnOn.read()) {
|
||||||
state = STATE_ON;
|
changeStateTo(STATE_ON);
|
||||||
stateBegan = millis();
|
|
||||||
setStateOnLeds();
|
|
||||||
}
|
}
|
||||||
// Ausschalter auslesen
|
// Ausschalter auslesen
|
||||||
if (debounceBtnOff.update() && debounceBtnOff.read()) {
|
if (debounceBtnOff.update() && debounceBtnOff.read()) {
|
||||||
state = STATE_OFF;
|
changeStateTo(STATE_OFF);
|
||||||
setStateOnLeds();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auswertung des aktuellen Zustandes
|
// Auswertung des aktuellen Zustandes
|
||||||
// ggf Zustand wechseln
|
// ggf Zustand wechseln
|
||||||
if (state == STATE_ON) {
|
if (state_current == STATE_ON) {
|
||||||
if (calcStateTime() >= TIME_HALF) {
|
if (calcStateTime() >= TIME_HALF) {
|
||||||
state = STATE_HALF;
|
changeStateTo(STATE_HALF);
|
||||||
setStateOnLeds();
|
|
||||||
}
|
}
|
||||||
} else if (state == STATE_HALF && calcStateTime() >= TIME_OFF) {
|
} else if (state_current == STATE_HALF && calcStateTime() >= TIME_OFF) {
|
||||||
state = STATE_OFF;
|
changeStateTo(STATE_OFF);
|
||||||
setStateOnLeds();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// aktuellen Zustand auf die Serielle Verbindung schreiben
|
// kommunizieren
|
||||||
if (state == STATE_ON || state == STATE_HALF) {
|
sendState();
|
||||||
Serial.print("1");
|
|
||||||
} else {
|
|
||||||
Serial.print("0");
|
|
||||||
}
|
|
||||||
delay(10);
|
delay(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue