#include <Servo.h>
Servo flapsServo;
Servo proparpmServo;
Servo egtbServo;
Servo ffaServo;
Servo ffbServo;
Servo adfServo;
int CodeIn; // used on all serial reads
int KpinNo;
int Koutpin;
String flaps;
String proparpm;
String egtb;
String ffa;
String ffb;
String adf;
int flapsangle;
int proparpmangle;
int egtbangle;
int ffaangle;
int ffbangle;
int adfiangle;
String KoldpinStateSTR, KpinStateSTR, Kstringnewstate, Kstringoldstate;
void setup()
{
Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
for (int KoutPin = 2; KoutPin < 20; KoutPin++) // Get all the pins ready for "Keys"
{
pinMode(KoutPin, INPUT);
digitalWrite(KoutPin, HIGH);
}
Serial.begin(115200);
pinMode(3, OUTPUT); // For the flap position servo.
flapsServo.attach(3);
pinMode(5, OUTPUT); // For the eng 1 rpm servo.
proparpmServo.attach(5);
pinMode(6, OUTPUT); // For the egt 2 servo.
egtbServo.attach(6);
pinMode(9, OUTPUT); // For fuel flow 1 servo.
ffaServo.attach(9);
pinMode(10, OUTPUT); // For fuel flow 2 servo.
ffbServo.attach(10);
pinMode(11, OUTPUT); // For adf servo.
adfServo.attach(11);
}
void loop() {
{KEYS();} //Check the "keys" section
if (Serial.available()) {
CodeIn = getChar();
if (CodeIn == '=') {EQUALS();} // The first identifier is "="
if (CodeIn == '<') {LESSTHAN();} // The first identifier is "<"
if (CodeIn == '?') {QUESTION();} // The first identifier is "?"
if (CodeIn == '/') {SLASH();} // The firt identifier is "/" (Annunciators)
}
}
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'A': // Found the second identifier
// Do something
break;
case'B':
// Do something
break;
case'C':
// Do something
break;
}
}
void LESSTHAN(){ // The first identifier was "<"
CodeIn = getChar(); // Get another character
switch(CodeIn) { // Now lets find what to do with it
case'A': // Found the second identifier
// Do something
break;
case'B':
// Do something
break;
case'G':; //("G" Flaps position)
flaps = "";
flaps += getChar();
flaps += getChar();
flaps += getChar();
int flapsi = flaps.toInt(); // Convert to an integer.
flapsangle = flapsi+flapsi*(4./5.); // Manual correction to suit servo 180 deg.
flapsServo.write(flapsangle); // Scaled output to servo.
break;
case'T':; //("T" Prop 1 rpm position)
proparpm = "";
proparpm += getChar();
proparpm += getChar();
proparpm += getChar();
proparpm += getChar();
proparpm += getChar();
int proparpmi = proparpm.toInt(); // Convert to an integer.
proparpmangle = proparpmi*1./200.; // Manual correction to suit servo 180 deg.
proparpmServo.write(proparpmangle); // Scaled output to servo.
break;
}
}
void QUESTION(){ // The first identifier was "?"
CodeIn = getChar(); // Get another character
switch(CodeIn) { // Now lets find what to do with it
case'K': // Found the second identifier ("N" Eng 2 Temp position)
egtb = "";
egtb += getChar();
egtb += getChar();
egtb += getChar();
int egtbi = egtb.toInt(); // convert it to an integer
egtbangle = egtbi*5; // Range calculation to suit servo 180deg.
egtbServo.write(egtbangle); // Scaled output to servo
;
case'V': // Found the second identifier ("V" Fuel Flow 1 position)
ffa = "";
ffa += getChar();
ffa += getChar();
ffa += getChar();
ffa += getChar();
ffa += getChar();
ffa += getChar();
int ffai = ffa.toInt(); // convert it to an integer
ffaangle = ffai+ffai*(1./2.); // Range calculation to suit servo 180deg.
ffaServo.write(ffaangle); // Scaled output to servo
break;
case'W': // Found the second identifier ("W" Fuel Flow 2 position)
ffb = "";
ffb += getChar();
ffb += getChar();
ffb += getChar();
ffb += getChar();
ffb += getChar();
ffb += getChar();
int ffbi = ffb.toInt(); // convert it to an integer (Thanks Phill)
ffbangle = ffbi+ffbi*(1./2.); // Range calculation to suit servo 180deg.
ffbServo.write(ffbangle); // Scaled output to servo
break;
case'n': // Found the second identifier ("n" ADF position)
adf = "";
adf += getChar();
adf += getChar();
adf += getChar();
adf += getChar();
adf += getChar();
int adfi = adf.toInt(); // Convert to an integer.
adfiangle = adfi/2; // Range calculation to suit servo 180deg.
adfServo.write(adfiangle); // Scaled output to servo.
break;
}
}
void SLASH(){ // The first identifier was "/" (Annunciator)
// Do something
}
void KEYS()
{
Kstringnewstate = "";
for (int KpinNo = 2; KpinNo < 20; KpinNo++){
KpinStateSTR = String(digitalRead(KpinNo));
KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));
if (KpinStateSTR != KoldpinStateSTR)
{
if (KpinNo != 13){
Serial.print ("D");
if (KpinNo < 10) Serial.print ("0");
Serial.print (KpinNo);
Serial.println (KpinStateSTR);
}
}
Kstringnewstate += KpinStateSTR;
}
Kstringoldstate = Kstringnewstate;
}