/*************************************
Thomas Maritschnegg
2AHME KOP
Übung:Rollo Steuerung mit Statemachine
Datum: 04.04.2024
*************************************/
#define LED_GRUEN_OBEN 5
#define LED_ROT_UNTEN 6
#define TASTER_AUF 2
#define TASTER_ZU 3
int tzu = 0;
int tauf = 0;
bool tz;
bool otz;
bool ta;
bool ota;
int state = 1;
int warten = 0;
int ezu = false;
int eauf = false;
void setup() {
// put your setup code here, to run once:
pinMode(LED_GRUEN_OBEN, OUTPUT);
pinMode(LED_ROT_UNTEN, OUTPUT);
pinMode(TASTER_AUF, INPUT_PULLUP);
pinMode(TASTER_ZU, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
otz = tz;
tz = digitalRead(TASTER_ZU);
if ((otz != tz) && (tz == LOW)) {
ezu=true;
tzu++;
}
ota= ta;
ta = digitalRead(TASTER_AUF);
if ((ota!= ta) && (ta == LOW)){
eauf =true;
tauf++;
}
switch (state) // Statevariable
{
case 1:
Serial.println("Ich bin im Zustand Oben");
digitalWrite(LED_GRUEN_OBEN, HIGH);
digitalWrite(LED_ROT_UNTEN, LOW);
if (ezu == true){
state=2;
}
break;
case 2:
Serial.println("Ich bin im Zustand zu");
digitalWrite(LED_GRUEN_OBEN, LOW);
digitalWrite(LED_ROT_UNTEN, HIGH);
digitalWrite(LED_ROT_UNTEN, LOW);
if (tauf == 2){
state =3;
}
warten++;
if (warten == 5000) {
state = 4;
}
break;
case 3:
Serial.println("Ich bin im Zustand Stop");
digitalWrite(LED_GRUEN_OBEN, LOW);
digitalWrite(LED_ROT_UNTEN, LOW);
if (tauf == 3){
tauf=0;
state=5;
}
if (tzu == 3) {
tzu=0;
state=2;
}
break;
case 4:
Serial.println("Ich bin im Zustand UNTEN");
digitalWrite(LED_GRUEN_OBEN,LOW);
digitalWrite(LED_ROT_UNTEN,HIGH);
if (eauf == true){
state = 5;
}
break;
case 5:
Serial.println("Ich bin im Zustand AUF");
digitalWrite(LED_ROT_UNTEN, LOW);
digitalWrite(LED_GRUEN_OBEN, HIGH);
delay(200);
digitalWrite(LED_GRUEN_OBEN, LOW);
warten --;
if (warten == 0) {
state = 1;
}
if (tzu==2){
state = 3;
}
break;
}
}