Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int numOfInputs = 5;
const int inputPins[numOfInputs] = {30,31,32,33,34,};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW,LOW,LOW,LOW,LOW};
bool inputFlags[numOfInputs] = {LOW,LOW,LOW,LOW,LOW};
long lastDebounceTime[numOfInputs] = {0,0,0,0,0};
long debounceDelay = 5;
//LCD Menu Logic
const int numOfScreens = 3;
int currentScreen = 0;
String screens[numOfScreens][2] =
{
{"Pulsdauer","ms"},
{"Frequenz", "s"},
{"Wiederholungen","mal"}
};
int parameters[numOfScreens];
int x = 0;
int y = 0;
int t = 0;
int s = 0;
void setup()
{
pinMode(40, OUTPUT);
pinMode(41, OUTPUT);
pinMode(42, OUTPUT);
pinMode(43, OUTPUT);
for(int i = 0; i < numOfInputs; i++)
{
pinMode(inputPins[i], INPUT);
digitalWrite(inputPins[i], HIGH); // pull-up 20k
}
//Serial.begin(9600);
lcd.begin();
}
void loop()
{
setInputFlags();
resolveInputFlags();
}
void setInputFlags() {
for(int i = 0; i < numOfInputs; i++)
{
int reading = digitalRead(inputPins[i]);
if (reading != lastInputState[i])
{
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay)
{
if (reading != inputState[i])
{
inputState[i] = reading;
if (inputState[i] == HIGH)
{
inputFlags[i] = HIGH;
}
}
}
lastInputState[i] = reading;
}
}
void resolveInputFlags()
{
for(int i = 0; i < numOfInputs; i++)
{
if(inputFlags[i] == HIGH)
{
inputAction(i);
inputFlags[i] = LOW;
printScreen();
//showxyt(i);
Messunglaeuft(i);
}
}
}
void inputAction(int input)
{
if(input == 0)
{
if (currentScreen == 0)
{
currentScreen = numOfScreens-1;
}
else
{
currentScreen--;
}
}
if(input == 1)
{
if (currentScreen == numOfScreens-1)
{
currentScreen = 0;
}
else
{
currentScreen++;
}
}
if(input == 2 && currentScreen == 0)
{
parameterChangex(0);
x = x+100;
}
if(input == 3 && currentScreen == 0)
{
parameterChangex(1);
x = x-100;
}
if(input == 2 && currentScreen == 1)
{
parameterChangey(0);
y = y+1000;
}
if(input == 3 && currentScreen == 1)
{
parameterChangey(1);
y = y-1000;
}
if(input == 2 && currentScreen == 2)
{
parameterChanget(0);
t++;
}
if(input == 3 && currentScreen == 2)
{
parameterChanget(1);
t--;
}
}
void parameterChangex(int key)
{
if(key == 0)
{
parameters[currentScreen]= parameters[currentScreen]+100;
}
else if(key == 1)
{
parameters[currentScreen]= parameters[currentScreen]-100;
}
}
void parameterChangey(int key)
{
if(key == 0)
{
parameters[currentScreen]++;
}
else if(key == 1)
{
parameters[currentScreen]--;
}
}
void parameterChanget(int key)
{
if(key == 0)
{
parameters[currentScreen]++;
}
else if(key == 1)
{
parameters[currentScreen]--;
}
}
//void showxyt(int input)
//{
// if (input == 4)
// {
// lcd.clear();
// lcd.print("x=");
// lcd.print(x);
// lcd.print("y=");
// lcd.print(y);
// lcd.print("t=");
// lcd.print(t);
// delay(1000);
// }
//}
void Messunglaeuft (int input)
{
while (input == 4 && t != 0 && s < t)
{
lcd.clear();
lcd.print("Messunglaeuft");
lcd.setCursor(0,1);
lcd.print("x=");
lcd.print(x);
lcd.print(" ");
lcd.print("y=");
lcd.print(y);
lcd.print(" ");
lcd.print("t=");
lcd.print(t);
lcd.print(" ");
digitalWrite(40, HIGH);
digitalWrite(41, HIGH);
digitalWrite(42, LOW);
digitalWrite(43, LOW);
delay(x/2);
digitalWrite(42, HIGH);
digitalWrite(43, HIGH);
digitalWrite(40, LOW);
digitalWrite(41, LOW);
delay(x/2);
s++;
digitalWrite(40, LOW);
digitalWrite(41, LOW);
digitalWrite(42, LOW);
digitalWrite(43, LOW);
delay(y);
}
if( s == t && t != 0)
{
lcd.clear();
lcd.print("Messung");
lcd.setCursor(0,1);
lcd.print("abgeschlossen");
s = 0;
}
}
void printScreen()
{
lcd.clear();
lcd.print(screens[currentScreen][0]);
lcd.setCursor(0,1);
lcd.print(parameters[currentScreen]);
lcd.print(" ");
lcd.print(screens[currentScreen][1]);
lcd.print(" ");
//Variablen Test
//(lcd.print("x=");lcd.print(x);lcd.print("y=");lcd.print(y);lcd.print("t=");lcd.print(t);
}
Ist jetzt noch mit 5 Tasten statt mit dem Joystick. Hab mich mit dem Joystick noch nicht ausreichend befasst. Funktioniert auch schon alles wie gewünscht. Solltet ihr Zeit haben, wäre es cool, wenn jemand den Code kurz überprüft und evtl Tipps zum beschönigen gibt. Da ich bisher nur ein Anfänger bin
Lesezeichen