Hallo zusammen.
Ich habe nachdem ich mir so einige Videos, Hilfeseiten und Foreneinträge angesehe habe, zum ersten Mal mit Arduino beschäftigt.
Programmiersprache ist eigentlich also eine Fremdsprache für mich.
Mein ersten Projekt "Krokodil" habe ich soweit fertig... Nur komme ich am Schluss nicht weiter.
Das Programm läuft ein "Schluss"-Void (void verloren) durch und sollte dann wieder auf Anfang (zustand1) springen.
Leider habe ich aber das Problem, dass die Tasten aus dem Loop nicht erkannt werden.
Vielleicht könnte mir hier jemand helfen...
(Ich weiß, dass ich mit vielen Variablen und If-Befehlen arbeite, aber wie gesagt = Anfänger)
Hier der komplette Code:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,4); // set the LCD address to 0x3F for a 16 chars and 2 line display
int tasterSelect=4;
int tasterHoch=7;
int tasterRunter=8;
int relais=9;
int reed=10;
int red = 3;
int green = 6;
int blue = 5;
int HochGedrueckt = 0 ;
int RunterGedrueckt = 0;
int SelectGedrueckt = 0;
int tasterStatusHoch = LOW; // aktuelles Signal vom Eingangspin
int tasterGedruecktHoch = 0; // abfragen ob Taster gedrückt war
int tasterStatusRunter = LOW; // aktuelles Signal vom Eingangspin
int tasterGedruecktRunter = 0; // abfragen ob Taster gedrückt war
int tasterStatusSelect = LOW; // aktuelles Signal vom Eingangspin
int tasterGedruecktSelect = 0; // abfragen ob Taster gedrückt war
int entprellZeit = 200; // Zeit für Entprellung, anpassen!
unsigned long tasterZeitHoch = 0; // Zeit beim drücken des Tasters
unsigned long tasterZeitSelect = 0; // Zeit beim drücken des Tasters
unsigned long tasterZeitRunter = 0; // Zeit beim drücken des Tasters
int ledred;
int ledgreen;
int ledblue;
int counter = 0;
int vorgabe = 1;
int fuellangabe = 0;
int fuellung = 0;
int zufallszahl = 0;
int menu1 = 0;
int menu2 = 0;
int menu6 = 0;
int menuplay = 0;
int zustand = 0;
int play;
int stop;
int zustandplay;
int reedState = 0;
boolean reedzustand = true;
int checkauf;
int checkzu;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.backlight();
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(tasterRunter, INPUT);
pinMode(tasterSelect, INPUT);
pinMode(tasterHoch, INPUT);
pinMode(reed, INPUT);
pinMode(relais, OUTPUT);
analogWrite(blue, 0);
analogWrite(red, 0);
analogWrite(green, 255);
lcd.print("Schnapsi");
lcd.setCursor(0,1);
lcd.print("Das kleine Krokodil");
lcd.setCursor(0,2);
lcd.print("Viel Spass!");
delay(300);
menu1 = 1;
zustand = 1;
fuellung = 1;
zustand1();
}
void loop()
{
/////////////////////taster /////////
///////////////hoch///////////
tasterStatusHoch = digitalRead(tasterHoch);
Serial.println(tasterStatusHoch);
if (tasterStatusHoch == HIGH)
{
tasterZeitHoch = millis();
tasterGedruecktHoch = 1;
}
if ((millis() - tasterZeitHoch > entprellZeit) && tasterGedruecktHoch == 1)
{
HochGedrueckt = 1;
tasterGedruecktHoch = 0;
}
///////////runter /////////////
tasterStatusRunter = digitalRead(tasterRunter);
if (tasterStatusRunter == HIGH)
{
tasterZeitRunter = millis();
tasterGedruecktRunter = 1;
}
if ((millis() - tasterZeitRunter > entprellZeit) && tasterGedruecktRunter == 1)
{
RunterGedrueckt = 1;
tasterGedruecktRunter = 0;
}
///////////select /////////////
tasterStatusSelect = digitalRead(tasterSelect);
if (tasterStatusSelect == HIGH)
{
tasterZeitSelect = millis();
tasterGedruecktSelect = 1;
}
if ((millis() - tasterZeitSelect > entprellZeit) && tasterGedruecktSelect == 1)
{
SelectGedrueckt = 1;
tasterGedruecktSelect = 0;
}
///////////////////////////
////taster zustand 1 /////
if (HochGedrueckt == 1 && zustand == 1)
{
HochGedrueckt = 0;
menu1--;
zustand1();
delay (200);
}
if (RunterGedrueckt == 1 && zustand == 1)
{
RunterGedrueckt = 0;
menu1++;
zustand1();
delay (200);
}
if (SelectGedrueckt == 1 && zustand == 1 && menu1 == 1)
{
SelectGedrueckt = 0;
menu1 = 0;
menu2 = 1;
zustand = 2;
zustand2();
}
if (SelectGedrueckt == 1 && zustand == 1 && menu1 == 2)
{
SelectGedrueckt = 0;
menu1 = 0;
zustand = 5;
zustand5();
delay (200);
}
if (SelectGedrueckt == 1 && zustand == 1 && menu1 == 3)
{
SelectGedrueckt = 0;
menu1 = 0;
menu6 = 1;
zustand = 6;
zustand6();
delay (200);
}
////taster zustand 2 /////
if (HochGedrueckt == 1 && zustand == 2)
{
HochGedrueckt = 0;
menu2 = 1;
delay (200);
zustand2();
}
if (RunterGedrueckt == 1 && zustand == 2)
{
RunterGedrueckt = 0;
menu2 = 2;
delay (200);
zustand2();
}
if (SelectGedrueckt == 1 && zustand == 2 && menu2 == 1)
{
SelectGedrueckt = 0;
menu1 = 1;
zustand = 3;
zustand3();
delay (200);
}
if (SelectGedrueckt == 1 && zustand == 2 && menu2 == 2)
{
SelectGedrueckt = 0;
menu1 = 1;
zustand = 4;
zustand4();
delay (200);
}
if (SelectGedrueckt == 1 && zustand == 2 && menu2 == 3)
{
SelectGedrueckt = 0;
menu1 = 1;
zustand = 6;
zustand6();
delay (200);
}
///////// taster zustand 4 /////
if (HochGedrueckt == 1 && zustand == 4)
{
vorgabe--;
HochGedrueckt = 0;
zustand4();
delay (200);
}
if (RunterGedrueckt == 1 && zustand == 4)
{
vorgabe++;
RunterGedrueckt = 0;
zustand4();
delay (200);
}
if (SelectGedrueckt == 1 && zustand == 4)
{
SelectGedrueckt = 0;
zustand = 1;
lcd.clear();
lcd.print("Fester Wert");
lcd.setCursor(0,1);
lcd.print("eingestellt");
delay (2000);
play = 1;
playtime();
delay (200);
}
///////// taster zustand 5 /////
if (SelectGedrueckt == 1 && zustand == 5)
{
analogWrite(green, 0);
analogWrite(red, 0);
analogWrite(blue, 255);
digitalWrite(relais, LOW);
delay (500);
lcd.clear();
lcd.print("Spuelen Stop");
delay (1000);
lcd.clear();
lcd.print("Spuelen Beendet");
delay (3000);
zustand = 1;
analogWrite(blue, 0);
zustand1();
}
if (RunterGedrueckt == 1 && zustand == 5)
{
digitalWrite(relais, LOW);
delay (500);
lcd.clear();
lcd.print("Spuelen Stop");
delay (1000);
lcd.clear();
lcd.print("Spuelen Beendet");
delay (3000);
zustand = 1;
zustand1();
}
if (HochGedrueckt == 1 && zustand == 5)
{
digitalWrite(relais, LOW);
delay (500);
lcd.clear();
lcd.print("Spuelen Stop");
delay (3000);
lcd.clear();
lcd.print("Spuelen Beendet");
delay (2000);
zustand = 1;
zustand1();
}
//////////////// taster zustand 6 //////////
if (HochGedrueckt == 1 && zustand == 6)
{
menu6--;
zustand6();
delay (200);
}
if (RunterGedrueckt == 1 && zustand == 6)
{
menu6++;
zustand6();
delay (200);
}
if (SelectGedrueckt == 1 && zustand == 6)
{
fuellangabe = fuellung;
zustand = 1;
play = 1;
playtime();
}
}
void zustand1(){
/////////////////// zustand 1 /////////////
switch (menu1)
{
case 0:
menu1 = 1;
break;
case 1:
lcd.clear();
lcd.print("MENUE");
lcd.setCursor(0,1);
lcd.print(">Spielmodus");
lcd.setCursor(0,2);
lcd.print(" Spuelen");
lcd.setCursor(0,3);
lcd.print(" Fuellangabe");
break;
case 2:
lcd.clear();
lcd.print("MENUE");
lcd.setCursor(0,1);
lcd.print(" Spielmodus");
lcd.setCursor(0,2);
lcd.print(">Spuelen");
lcd.setCursor(0,3);
lcd.print(" Fuellangabe");
break;
case 3:
lcd.clear();
lcd.print("MENUE");
lcd.setCursor(0,1);
lcd.print(" Spielmodus");
lcd.setCursor(0,2);
lcd.print(" Spuelen");
lcd.setCursor(0,3);
lcd.print(">Fuellangabe");
break;
case 4:
menu1 = 3;
break;
}
}
void zustand2()
/// auswahl zufall / fester wert //// zustand 2 /////////
{
switch (menu2)
{
case 0:
menu2 = 1;
break;
case 1:
lcd.clear();
lcd.print("MENUE");
lcd.setCursor(0,1);
lcd.print(">Zufall");
lcd.setCursor(0,2);
lcd.print(" Fester Wert");
break;
case 2:
lcd.clear();
lcd.print("MENUE");
lcd.setCursor(0,1);
lcd.print(" Zufall");
lcd.setCursor(0,2);
lcd.print(">Fester Wert");
break;
case 3:
menu2 = 1;
break;
}
}
void zustand3()
{
/// zufall genieren ///
zufallszahl = random(1,5);
zufallszahl = random(1,8);
zufallszahl = random(1,10);
lcd.clear();
lcd.print("Zufallszahl erstellt");
vorgabe = zufallszahl;
delay (1000);
play = 1;
playtime();
}
void zustand4()
{
/// fester wert //
lcd.clear();
lcd.print("Fester Wert einstellen");
lcd.setCursor(0,1);
lcd.print("+ / - druecken");
lcd.setCursor(0,2);
lcd.print(vorgabe);
lcd.print(" ausgewaehlt");
}
void zustand5()
{
/// spuelen //
lcd.clear();
lcd.print("Spuelen startet");
delay (1000);
digitalWrite(relais, HIGH);
}
void zustand6()
{
switch (menu6)
{
case 0:
menu6 = 1;
break;
case 1:
lcd.clear();
lcd.print("FUELLANGABE");
lcd.setCursor(0,1);
lcd.print(">2 CL");
lcd.setCursor(0,2);
lcd.print(" 1 CL");
fuellangabe = 2;
break;
case 2:
lcd.clear();
lcd.print("FUELLANGABE");
lcd.setCursor(0,1);
lcd.print(" 2 CL");
lcd.setCursor(0,2);
lcd.print(">1 CL");
fuellangabe = 1;
break;
case 3:
menu6 = 2;
break;
}
}
void playtime()
{
stop = 0;
reedzustand = true;
reedState =digitalRead(reed);
if (reedState == HIGH && play == 1 && stop == 0)
{
if (reedzustand == true)
{
stop = 1;
analogWrite(green, 0);
analogWrite(red, 255);
lcd.clear();
lcd.print("Bitte oeffnen");
analogWrite(red, 0);
analogWrite(red, 255);
delay (100);
analogWrite(red, 0);
delay (100);
analogWrite(green, 255);
delay (1000);
reedzustand = false;
playtime();
}
}
if (reedState == LOW && play == 1 && stop == 0)
{
if (reedzustand == true)
{
analogWrite(red, 0);
analogWrite(green, 255);
lcd.clear();
delay (2000);
play = 2;
reedzustand = false;
playtime();
}
}
reedState =digitalRead(reed);
if (reedState == HIGH && play == 2 && stop == 0)
{
if (reedzustand == true)
{
stop = 1;
play = 0;
schnapp();
reedzustand = false;
}
}
else {playtime();}
}
void schnapp()
{
counter++;
lcd.clear();
analogWrite(green, 0);
analogWrite(red, 255);
delay (100);
analogWrite(red, 0);
delay (100);
analogWrite(red, 255);
lcd.print("SCHNAPP!!!");
lcd.setCursor(0,1);
lcd.clear();
lcd.print("ANZAHL:");
lcd.setCursor(0,2);
lcd.print(counter);
if (counter == vorgabe)
{
analogWrite(blue, 255);
delay (100);
analogWrite(blue, 0);
analogWrite(red, 255);
delay (100);
analogWrite(red, 0);
analogWrite(blue, 255);
delay (100);
lcd.clear();
lcd.print("VERLOREN");
analogWrite(blue, 0);
analogWrite(red, 255);
delay (100);
analogWrite(red, 0);
analogWrite(blue, 255);
delay (100);
analogWrite(red, 255);
analogWrite(blue, 0);
delay (100);
analogWrite(red, 0);
analogWrite(blue, 255);
delay (100);
analogWrite(blue, 0);
analogWrite(red, 255);
delay (100);
analogWrite(red, 0);
analogWrite(blue, 255);
delay (100);
analogWrite(red, 255);
analogWrite(blue, 0);
delay (100);
analogWrite(red, 0);
analogWrite(blue, 0);
delay (3000);
lcd.clear();
verloren();
}
if (counter != vorgabe)
{
delay (3000);
play = 1;
playtime();
}
}
void verloren()
{
lcd.clear();
lcd.print("Verloren!");
analogWrite(green, 0);
analogWrite(blue, 255);
analogWrite(red, 0);
if (fuellung == 1)
{
digitalWrite(relais, HIGH);
delay (3000);
digitalWrite(relais, LOW);
}
if (fuellung == 2)
{
digitalWrite(relais, HIGH);
delay (3000);
digitalWrite(relais, LOW);
}
lcd.clear();
lcd.print("PROST");
vorgabe = 0;
fuellung = 1;
counter = 0;
stop = 0;
zufallszahl = 0;
menu1 = 1; (NACHTRAG)
analogWrite(blue, 0);
lcd.clear();
zustand1();
}
[EDIT] Bildlink aus den Sketch genommen. Uwe [/EDIT]
