//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4
#define BTN_1 5
#define BTN_2 6
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
uint8_t ipilot = 0;
uint8_t ityp = 0;
uint8_t iwartung = 0;
int start = 1;
int bt2_0 = 1;
int az_mode=0;
char cpilot;
char ctyp;
char cwartung;
typedef enum {
SET_PILOT,
SET_TYP
} Mode;
Mode mode = SET_TYP;
char* Pilot[] = {"Altenkirch","Nela","Eggestein","Appenheimer","Baum"};
char* Typ[] = {"Gast","Schulung","Werkstatt","Verein"};
char* Wartung[] = {"Tag","Stunde"};
void updateDisplay() {
// Print a message to the LCD.
lcd.setCursor(3,0);
// lcd.print("Hello, world!");
if (mode == SET_PILOT) {
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("Pilot ");
lcd.setCursor(6,0);
lcd.print(">");
lcd.setCursor(7, 0);
lcd.print(Pilot[ipilot]);
}
else {
lcd.setCursor(5,0);
lcd.print(": ");
}
if (mode == SET_TYP) {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Co ");
lcd.setCursor(6,1);
lcd.print(">");
lcd.setCursor(7, 1);
lcd.print(Typ[ityp]);
}
else {
lcd.setCursor(2,1);
lcd.print(": ");
}
}
void clearDisplay() {
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
}
void setDisplay_Flug0() {
clearDisplay();
lcd.setCursor(0, 0);
lcd.print("Pilot ");
lcd.setCursor(5,0);
lcd.print(":");
lcd.setCursor(7, 0);
lcd.print(Pilot[ipilot]);
lcd.setCursor(0, 1);
lcd.print("Co ");
lcd.setCursor(5,1);
lcd.print(":");
lcd.setCursor(7, 1);
lcd.print(Typ[ityp]);
az_mode =1;
}
void setDisplay_Flug1() {
clearDisplay();
lcd.setCursor(0, 0);
lcd.print(Pilot[ipilot]);
String s1 = Typ[ityp];
lcd.setCursor(16,0);
lcd.print(s1.substring(0,4));
az_mode =0;
// lcd.print(Wartung[iwartung]);
}
void clearDisplay(int s, int z) {
lcd.setCursor(s,z);
lcd.print(' ');
}
void nextMode() {
switch (mode) {
case SET_PILOT:
mode = SET_TYP;
break;
case SET_TYP:
mode = SET_PILOT;
break;
}
}
void updateValue(int delta) {
switch (mode) {
case SET_PILOT:
ipilot = constrain(ipilot + delta, 0, sizeof(Pilot)/2-1);
break;
case SET_TYP:
ityp = constrain(ityp + delta, 0, sizeof(Typ)/2-1);
break;
}
}
void setStart_Stop() {
lcd.setCursor(0, 3);
lcd.print(start);
}
void setup()
{
pinMode(ENCODER_SW, INPUT_PULLUP);
pinMode(BTN_1, INPUT_PULLUP);
pinMode(BTN_2, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
// attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
lcd.init(); // initialize the lcd
lcd.backlight();
setDisplay_Flug0();
}
//************************************************************
long int modeLastChanged = millis();
long int wait_short = 100;
long int wait_long = 500;
int prevClk = HIGH;
int btn_0 = 0;
void loop() {
if (digitalRead(ENCODER_SW) == LOW && btn_0 == 0) {
modeLastChanged = millis();
btn_0 = 1;
}
if (digitalRead(ENCODER_SW) == LOW && millis() - modeLastChanged > wait_long && btn_0 == 1) {
btn_0 = 2;
setDisplay_Flug0();
}
if (digitalRead(ENCODER_SW) == HIGH && millis() - modeLastChanged < wait_long && btn_0 == 1) {
btn_0=0;
nextMode();
updateDisplay();
}
if (digitalRead(ENCODER_SW) == HIGH && btn_0 == 2) {
btn_0=0;
}
lcd.setCursor(0, 3);
lcd.print(az_mode);
int clk = digitalRead(ENCODER_CLK);
if (clk != prevClk && clk == LOW) {
int dt = digitalRead(ENCODER_DT);
int delta = dt == HIGH ? 1 : -1;
updateValue(delta);
updateDisplay();
}
prevClk = clk;
int bt1 = digitalRead((BTN_1));
if (bt1 == LOW) {
setDisplay_Flug1();
delay (100);
}
int bt2 = digitalRead((BTN_2));
if (bt2 != bt2_0) {
start = bt2 ;
bt2_0 = bt2;
setStart_Stop();
}
}