#include <LCD5110_Basic.h>
LCD5110 myGLCD(A5,A4,A3,A1,A2);
extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];
extern uint8_t BigNumbers[];
int CodeIn;// used on all serial reads
int KpinNo;
int Koutpin;
String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
int R;// a variable
int Rold;// the old reading
int Rdif;// the difference since last loop
int R2;// a second test variable
int R3;// a second test variable
int Rold2;// a second loop old reading
int Rdif2; // the second test difference
int Rold3;// a second loop old reading
int Rdif3; // the second test difference
int posset = 1;
int screen = 1;
int posset2 = 1;
String spd;
String alt;
String vspd;
String hdg;
String crs;
String ap;
String ath;
String spda;
String hdga;
String alta;
String nav;
String app;
String gps;
String bck;
#include "math.h"
#include "Quadrature.h"
Quadrature quad1(12, 13);
#define buttonrot A0
#define button41 11
#define button42 10
#define button43 9
#define button44 8
#define button51 2
#define button52 3
#define button53 4
#define button54 5
#define button55 6
#define buttonscr 7
void setup()
{
pinMode(buttonrot, INPUT);
digitalWrite(buttonrot, HIGH);
pinMode(button41, INPUT);
digitalWrite(button41, HIGH);
pinMode(button42, INPUT);
digitalWrite(button42, HIGH);
pinMode(button43, INPUT);
digitalWrite(button43, HIGH);
pinMode(button44, INPUT);
digitalWrite(button44, HIGH);
pinMode(button51, INPUT);
digitalWrite(button51, HIGH);
pinMode(button52, INPUT);
digitalWrite(button52, HIGH);
pinMode(button53, INPUT);
digitalWrite(button53, HIGH);
pinMode(button54, INPUT);
digitalWrite(button54, HIGH);
pinMode(button55, INPUT);
digitalWrite(button55, HIGH);
pinMode(buttonscr, INPUT);
digitalWrite(buttonscr, HIGH);
Serial.begin(115200);
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myGLCD.print("Flight", CENTER, 0);
myGLCD.print("Simulator", CENTER, 8);
myGLCD.print("Multi", CENTER, 16);
myGLCD.print("Control", CENTER, 24);
myGLCD.print("Version:1.6", CENTER, 32);
delay(3000);
}
void loop() {
{ENCODER();} //Check the Rotary Encoders
{BUTTONS();} //Check the Buttons
if (Serial.available()) {
CodeIn = getChar();
if (CodeIn == '=') {EQUALS();} // The first identifier is "="
}
}
char getChar()// Get a character from the serial buffer
{
while(Serial.available() == 0);// wait for data
return((char)Serial.read());// Thanks Doug
}
void EQUALS(){ // The first identifier was "="
CodeIn = getChar(); // Get another character
switch(CodeIn) {// Now lets find what to do with it
case 'e':{//Autopilot Course
crs = "";
crs += getChar();
crs += getChar();
crs += getChar();
int crsi = crs.toInt(); // convert it to an integer (Thanks Phill)
if (screen==1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 7, 0);
myGLCD.print("CRS:", 7, 0);
myGLCD.printNumI(crsi, 30, 0);}
break;}
case 'f':{//Autopilot Speed
spd = "";
spd += getChar();
spd += getChar();
spd += getChar();
int spdi = spd.toInt(); // convert it to an integer (Thanks Phill)
if (screen==1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 7, 8);
myGLCD.print("SPD:", 7, 8);
myGLCD.printNumI(spdi, 30, 8);}
break;}
case 'd':{//Autopilot Heading
hdg = "";
hdg += getChar();
hdg += getChar();
hdg += getChar();
int hdgi = hdg.toInt();
if (screen==1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 7, 16);
myGLCD.print("HDG:", 7, 16);
myGLCD.printNumI(hdgi, 30, 16);}
break;}
case 'b':{//Autopilot Altitude
alt = "";
alt += getChar();
alt += getChar();
alt += getChar();
alt += getChar();
alt += getChar();
if (screen==1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 7, 24);
myGLCD.print("ALT:", 7, 24);
myGLCD.print(alt, 30, 24);}
break;}
case 'c':{//Autopilot Vertical Speed
vspd = "";
vspd += getChar();
vspd += getChar();
vspd += getChar();
vspd += getChar();
vspd += getChar();
int vspdi = vspd.toInt(); // convert it to an integer (Thanks Phill)
if (screen==1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 7, 32);
myGLCD.print("VSP:", 7, 32);
myGLCD.printNumI(vspdi, 30, 32);}
break;}
case 'a':{//Autopilot Active
ap = "";
ap += getChar();
int api = ap.toInt();
if (screen==1){
if (api == 0){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", LEFT, 40);
}
if (api == 1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", LEFT, 40);
myGLCD.invertText(true);
myGLCD.print("AP", LEFT, 40);}
myGLCD.invertText(false);}
break;}
case 'u':{//Autothrotle Active
ath = "";
ath += getChar();
int athi = ath.toInt();
if (screen==1){
if (athi == 0){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 40);
}
if (athi == 1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 40);
myGLCD.invertText(true);
myGLCD.print("ATH", RIGHT, 40);}
myGLCD.invertText(false);}
break;}
case 's':{//Airspeed Active
spda = "";
spda += getChar();
int spdai = spda.toInt();
if (screen==1){
if (spdai == 0){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 8);
}
if (spdai == 1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 8);
myGLCD.print("\\", RIGHT, 8);}}
break;}
case 'j':{//Autopilot Heading Lock Active
hdga = "";
hdga += getChar();
int hdgai = hdga.toInt();
if (screen==1){
if (hdgai == 0){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 16);
}
if (hdgai == 1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 16);
myGLCD.print("\\", RIGHT, 16);}}
break;}
case 'k':{//Autopilot Altitude lock Active
alta = "";
alta += getChar();
int altai = alta.toInt();
if (screen==1){
if (altai == 0){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 24);
}
if (altai == 1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 24);
myGLCD.print("\\", RIGHT, 24);}}
break;}
case 'l':{//GPS or NAV Display
gps = "";
gps += getChar();
int gpsi = gps.toInt();
if (screen==1){
if (gpsi == 1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 0);
myGLCD.print("GPS", RIGHT, 0);
}
if (gpsi == 0){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 0);
myGLCD.print("NAV", RIGHT, 0);}}
break;}
case 'o':{//NAV1 Lock Active
nav = "";
nav += getChar();
int navi = nav.toInt();
if (screen==1){
if (navi == 0){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 19, 40);
}
if (navi == 1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 19, 40);
myGLCD.invertText(true);
myGLCD.print("VOR", 19, 40);}
myGLCD.invertText(false);}
break;}
case 'm':{//Approach Hold Active
app = "";
app += getChar();
int appi = app.toInt();
if (screen==1){
if (appi == 0){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 45, 40);
}
if (appi == 1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 45, 40);
myGLCD.invertText(true);
myGLCD.print("APP", 45, 40);}
myGLCD.invertText(false);}
break;}
case 'n':{//BackCrouse Hold Active
bck = "";
bck += getChar();
int bcki = bck.toInt();
if (screen==1){
if (bcki == 0){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 32);
}
if (bcki == 1){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", RIGHT, 32);
myGLCD.invertText(true);
myGLCD.print("BC", RIGHT, 32);}
myGLCD.invertText(false);}
break;}
//Screen 2 RADIO1
case 'A':{//Com1 ACtive
String com1;
com1 = "";
com1 += getChar();
com1 += getChar();
com1 += getChar();
com1 += getChar();
com1 += getChar();
com1 += getChar();
getChar();
if (screen==2){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 0, 0);
myGLCD.print("COM1:", 0, 0);
myGLCD.print("s/b", 55, 0);
myGLCD.print(" ", 0, 8);
myGLCD.invertText(true);
myGLCD.print(com1, 0, 8);}
myGLCD.invertText(false);
break;}
case 'B':{//Com1 Stand by
String com1sb;
com1sb = "";
com1sb += getChar();
com1sb += getChar();
com1sb += getChar();
com1sb += getChar();
com1sb += getChar();
com1sb += getChar();
getChar();
if (screen==2){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 46, 8);
myGLCD.invertText(false);
myGLCD.print(com1sb, 46, 8);}
myGLCD.invertText(false);
break;}
case 'E':{//Nav1 ACtive
String nav1;
nav1 = "";
nav1 += getChar();
nav1 += getChar();
nav1 += getChar();
nav1 += getChar();
nav1 += getChar();
nav1 += getChar();
if (screen==2){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 0, 16);
myGLCD.print("NAV1:", 0, 16);
myGLCD.print("s/b", 55, 16);
myGLCD.print(" ", 0, 24);
myGLCD.print(" ", 0, 24);
myGLCD.invertText(true);
myGLCD.print(nav1, 0, 24);
myGLCD.invertText(false);}
break;}
case 'F':{//Nav1 Stand by
String nav1sb;
nav1sb = "";
nav1sb += getChar();
nav1sb += getChar();
nav1sb += getChar();
nav1sb += getChar();
nav1sb += getChar();
nav1sb += getChar();
if (screen==2){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 46, 24);
myGLCD.invertText(false);
myGLCD.print(nav1sb, 46, 24);
myGLCD.invertText(false);}
break;}
/*case 'K':{//DME1 Nm
String dme1;
dme1 = "";
getChar();
dme1 += getChar();
dme1 += getChar();
dme1 += getChar();
dme1 += getChar();
if (screen==2){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 0, 40);
myGLCD.print("DME1:" , LEFT, 40);
myGLCD.print(dme1, 31, 40);
myGLCD.print("Nm",55, 40);
}
break;} */
//SCREEN 3 RADIO2
case 'C':{//Com2 ACtive
String com2;
com2 = "";
com2 += getChar();
com2 += getChar();
com2 += getChar();
com2 += getChar();
com2 += getChar();
com2 += getChar();
getChar();
if (screen==3){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 0, 0);
myGLCD.print("COM2:", 0, 0);
myGLCD.print("s/b", 55, 0);
myGLCD.print(" ", 0, 8);
myGLCD.invertText(true);
myGLCD.print(com2, 0, 8);}
myGLCD.invertText(false);
break;}
case 'D':{//Com2 Stand by
String com2sb;
com2sb = "";
com2sb += getChar();
com2sb += getChar();
com2sb += getChar();
com2sb += getChar();
com2sb += getChar();
com2sb += getChar();
getChar();
if (screen==3){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 46, 8);
myGLCD.invertText(false);
myGLCD.print(com2sb, 46, 8);}
myGLCD.invertText(false);
break;}
case 'G':{//Nav2 ACtive
String nav2;
nav2 = "";
nav2 += getChar();
nav2 += getChar();
nav2 += getChar();
nav2 += getChar();
nav2 += getChar();
nav2 += getChar();
if (screen==3){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 0, 16);
myGLCD.print("NAV2:", 0, 16);
myGLCD.print("s/b", 55, 16);
myGLCD.print(" ", 0, 24);
myGLCD.print(" ", 0, 24);
myGLCD.invertText(true);
myGLCD.print(nav2, 0, 24);
myGLCD.invertText(false);}
break;}
case 'H':{//Nav2 Stand by
String nav2sb;
nav2sb = "";
nav2sb += getChar();
nav2sb += getChar();
nav2sb += getChar();
nav2sb += getChar();
nav2sb += getChar();
nav2sb += getChar();
if (screen==3){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 46, 24);
myGLCD.invertText(false);
myGLCD.print(nav2sb, 46, 24);
myGLCD.invertText(false);}
break;}
/*case 'L':{//DME2 Nm
String dme2;
dme2 = "";
dme2 += getChar();
dme2 += getChar();
dme2 += getChar();
dme2 += getChar();
dme2 += getChar();
dme2 += "N";
dme2 += "m";
if (screen==3){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 0, 32);
myGLCD.print("DME2:", LEFT, 32);
myGLCD.print(" ", RIGHT, 32);
myGLCD.invertText(false);
myGLCD.print(dme2, RIGHT, 32);
myGLCD.invertText(false);}
break;} */
}
}
/*void QUESTION(){ // The first identifier was "?"
CodeIn = getChar(); // Get another character
switch(CodeIn) {// Now lets find what to do with it
case 'k':{//kohlsman
String khm;
khm = "";
khm += "B";
khm += "a";
khm += "r";
khm += "o";
khm += ":";
khm += getChar();
khm += getChar();
khm += getChar();
khm += getChar();
khm += getChar();
khm += "Hg";
if (screen==2){
myGLCD.setFont(SmallFont);
myGLCD.print(" ", 7, 40);
myGLCD.print(khm, 7, 40);
break;
}
}
}
}
*/
void ENCODER(){
R3 =(quad1.position())/2; //full cycle encoder
if (R3 != Rold3) {
int buttonStaterot = digitalRead(buttonrot);
if (buttonStaterot ==HIGH){
if (screen == 1){
if (posset == 1){//CRS
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("A55"); // CRS down
if (Rdif3 == -1) Serial.println ("A56"); //CRS up
Rold3 = R3;
}
if (posset == 2){//SPD
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("B16"); // SPD down
if (Rdif3 == -1) Serial.println ("B15"); //SPD up
Rold3 = R3;
}
if (posset == 3){//HDG
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("A58"); // HDG down
if (Rdif3 == -1) Serial.println ("A57"); //HDG up
Rold3 = R3;
}
if (posset == 4){//ALT
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("B12"); // ALT down
if (Rdif3 == -1) Serial.println ("B11"); //ALT up
Rold3 = R3;
}
if (posset == 5){//VS
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("B14"); // VS down
if (Rdif3 == -1) Serial.println ("B13"); //VS up
Rold3 = R3;
}
}
if (screen == 2){
if (posset2 == 1){//COM1
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("A01"); // CRS down
if (Rdif3 == -1) Serial.println ("A02"); //CRS up
Rold3 = R3;
}
if (posset2 == 2){//NAV1
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("A13"); // SPD down
if (Rdif3 == -1) Serial.println ("A14"); //SPD up
Rold3 = R3;
}
if (posset2 == 3){//NAV1
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("C26"); // SPD down
if (Rdif3 == -1) Serial.println ("C25"); //SPD up
Rold3 = R3;
}
}
if (screen == 3){
if (posset2 == 1){//COM2
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("A07"); // CRS down
if (Rdif3 == -1) Serial.println ("A08"); //CRS up
Rold3 = R3;
}
if (posset2 == 2){//NAV2
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("A19"); // SPD down
if (Rdif3 == -1) Serial.println ("A20"); //SPD up
Rold3 = R3;
}
}
}
else{
if(screen==1){
(Rdif3 = (R3-Rold3));
if (posset>1){
if (Rdif3 == 1) posset-=1;
}
if(posset<5){
if (Rdif3 == -1) posset+=1;
Rold3 = R3;
}
if (posset==1){
myGLCD.print(" ", LEFT, 0);
myGLCD.print(" ", LEFT, 8);
myGLCD.print(" ", LEFT, 16);
myGLCD.print(" ", LEFT, 24);
myGLCD.print(" ", LEFT, 32);
myGLCD.print(">", LEFT, 0);
}
if (posset==2){
myGLCD.print(" ", LEFT, 0);
myGLCD.print(" ", LEFT, 8);
myGLCD.print(" ", LEFT, 16);
myGLCD.print(" ", LEFT, 24);
myGLCD.print(" ", LEFT, 32);
myGLCD.print(">", LEFT, 8);
}
if (posset==3){
myGLCD.print(" ", LEFT, 0);
myGLCD.print(" ", LEFT, 8);
myGLCD.print(" ", LEFT, 16);
myGLCD.print(" ", LEFT, 24);
myGLCD.print(" ", LEFT, 32);
myGLCD.print(">", LEFT, 16);
}
if (posset==4){
myGLCD.print(" ", LEFT, 0);
myGLCD.print(" ", LEFT, 8);
myGLCD.print(" ", LEFT, 16);
myGLCD.print(" ", LEFT, 24);
myGLCD.print(" ", LEFT, 32);
myGLCD.print(">", LEFT, 24);
}
if (posset==5){
myGLCD.print(" ", LEFT, 0);
myGLCD.print(" ", LEFT, 8);
myGLCD.print(" ", LEFT, 16);
myGLCD.print(" ", LEFT, 24);
myGLCD.print(" ", LEFT, 32);
myGLCD.print(">", LEFT, 32);
}
}
if(screen==2){
(Rdif3 = (R3-Rold3));
if (posset2==1){
if (Rdif3 == 1) Serial.println ("A03");
if (Rdif3 == -1) Serial.println ("A04");
Rold3 = R3;}
if (posset2==2){
if (Rdif3 == 1) Serial.println ("A15");
if (Rdif3 == -1) Serial.println ("A16");
Rold3 = R3;}
}
if(screen==3){
(Rdif3 = (R3-Rold3));
if (posset2==1){
if (Rdif3 == 1) Serial.println ("A09");
if (Rdif3 == -1) Serial.println ("A10");
Rold3 = R3;}
if (posset2==2){
if (Rdif3 == 1) Serial.println ("A21");
if (Rdif3 == -1) Serial.println ("A22");
Rold3 = R3;
}
}
}
}
}
void BUTTONS(){
int buttonState41 = digitalRead(button41);
int buttonState42 = digitalRead(button42);
int buttonState43 = digitalRead(button43);
int buttonState44 = digitalRead(button44);
int buttonState51 = digitalRead(button51);
int buttonState52 = digitalRead(button52);
int buttonState53 = digitalRead(button53);
int buttonState54 = digitalRead(button54);
int buttonState55 = digitalRead(button55);
int buttonStatescr = digitalRead(buttonscr);
if (screen==1){
if (buttonState41 ==LOW){Serial.println ("B01"); delay(250);}
if (buttonState43 ==LOW){Serial.println ("B10"); delay(250);}
if (buttonState44 ==LOW){Serial.println ("B08"); delay(250);}
if (buttonState42 ==LOW){Serial.println ("B34"); delay(250);}
if (buttonState51 ==LOW){Serial.println ("A54"); delay(250);}
if (buttonState52 ==LOW){Serial.println ("B26"); delay(25); Serial.print ("B22"); Serial.println (spd); delay(250);}
if (buttonState53 ==LOW){Serial.println ("B04"); delay(25); Serial.print ("A59"); Serial.println (hdg); delay(250);}
if (buttonState54 ==LOW){Serial.println ("B05"); delay(25); Serial.print ("B32"); Serial.println (alt); delay(250);}
if (buttonState55 ==LOW){Serial.println ("B09"); delay(250);}
}
if (screen==2){
if (buttonState41 ==LOW){Serial.println ("A45"); delay(250);}
if (buttonState42 ==LOW){Serial.println ("A46"); delay(250);}
if (buttonState43 ==LOW){Serial.println ("A48"); delay(250);}
if (buttonState44 ==LOW){Serial.println ("A49"); delay(250);}
if (buttonState52 ==LOW){Serial.println ("A06"); delay(250);}
if (buttonState51 ==LOW){posset2=1;
myGLCD.print(" ", 39, 8);
myGLCD.print(" ", 39, 24);
myGLCD.print(">",39, 8);
delay(250);}
if (buttonState54 ==LOW){Serial.println ("A18"); delay(250);}
if (buttonState53 ==LOW){posset2=2;
myGLCD.print(" ", 39, 8);
myGLCD.print(" ", 39, 24);
myGLCD.print(">",39, 24);
delay(250);}
if (buttonState55 ==LOW){}
}
if (screen==3){
if (buttonState41 ==LOW){Serial.println ("A45"); delay(250);}
if (buttonState42 ==LOW){Serial.println ("A46"); delay(250);}
if (buttonState43 ==LOW){Serial.println ("A48"); delay(250);}
if (buttonState44 ==LOW){Serial.println ("A49"); delay(250);}
if (buttonState52 ==LOW){Serial.println ("A12"); delay(250);}
if (buttonState51 ==LOW){
posset2=1;
myGLCD.print(" ", 39, 8);
myGLCD.print(" ", 39, 24);
myGLCD.print(">",39, 8);
delay(250);}
if (buttonState54 ==LOW){Serial.println ("A24"); delay(250);}
if (buttonState53
==LOW){posset2=2;
myGLCD.print(" ", 39, 8);
myGLCD.print(" ", 39, 24);
myGLCD.print(">",39, 24);
delay(250);}
if (buttonState55 ==LOW){}
}
if (buttonStatescr ==LOW){
screen+=1;
if (screen == 4){screen=1;}
myGLCD.clrScr();
delay(250);
}
}