Hi Rick
I forgot to mention that I sorted the code out.
Here's fully working code:
+++++++++++++++++++++
Code: Select all
#include "Wire.h" // Enable this line if using Arduino Uno, Mega, etc.
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "math.h"
#include "Quadrature.h"
Adafruit_7segment matrix1 = Adafruit_7segment();
Adafruit_7segment matrix2 = Adafruit_7segment();
Adafruit_7segment matrix3 = Adafruit_7segment();
Quadrature quad1(12, 11); // Course
Quadrature quad2(10, 9); // Speed
Quadrature quad3(8, 7); // Heading
Quadrature quad4(6, 5); // Altitude
int CodeIn;// used on all serial reads
int KpinNo;
int Koutpin;
int Count;
int AP_crs_set[3];
int AP_speed_kt_set[3];
int AP_hdg_set[3];
String Digit;
String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
String altit, altitold, vertsp, vertspold, hdgset, hdgsetold, flaps, flapsold, spd, spdold, crs, crsold;
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 R4;// 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 Rold4;// a second loop old reading
int Rdif4; // the second test difference
void setup() {
#ifndef __AVR_ATtiny85__
Serial.begin(115200);
#endif
matrix1.begin(0x71); //70 Course
matrix2.begin(0x71); //70 Speed
matrix3.begin(0x70); //70 Heading
for (int PinNo = 22; PinNo <= 70; PinNo++)// Get all the INPUT pins ready.
{
pinMode(PinNo, INPUT);
digitalWrite(PinNo, HIGH);
}
}
void loop() {
{KEYS();} //Check the "keys" section
{ENCODER();} //Check the Rotary Encoders
if (Serial.available()) {// Check if serial data has arrived from PC
CodeIn = getChar();
if (CodeIn == '=') {EQUALS();} // The first identifier is "="
if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<" Indications
if (CodeIn == '?') {QUESTION();}// The first identifier is "?" Systems2
if (CodeIn == '/') {SLASH();}// The first 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(){
CodeIn = getChar(); // Get the second identifier
switch(CodeIn) {// Now lets find what to do with it
case 'e': // AP course set
Count = 0;
while (Count < 3)
{
Digit = "";
Digit += getChar();
AP_crs_set[Count] = Digit.toInt();
Count++;
}
{
matrix1.writeDigitNum(1,AP_crs_set[0]);
matrix1.writeDigitNum(3,AP_crs_set[1]);
matrix1.writeDigitNum(4,AP_crs_set[2]);
}
matrix1.writeDisplay();
}
crsold = crs;
switch(CodeIn) {// Now lets find what to do with it
case 'f': // AP speed knots set kts
Count = 0;
while (Count < 3)
{
Digit = "";
Digit += getChar();
AP_speed_kt_set[Count] = Digit.toInt();
Count++;
}
{
matrix2.writeDigitNum(1,AP_speed_kt_set[0]);
matrix2.writeDigitNum(3,AP_speed_kt_set[1]);
matrix2.writeDigitNum(4,AP_speed_kt_set[2]);
}
matrix2.writeDisplay();
}
spdold = spd;
switch(CodeIn) {// Now lets find what to do with it
case 'd': // AP heading set
Count = 0;
while (Count < 3)
{
Digit = "";
Digit += getChar();
AP_hdg_set[Count] = Digit.toInt();
Count++;
}
{
matrix3.writeDigitNum(1,AP_hdg_set[0]);
matrix3.writeDigitNum(3,AP_hdg_set[1]);
matrix3.writeDigitNum(4,AP_hdg_set[2]);
}
matrix3.writeDisplay();
}
hdgsetold = hdgset;
}// end of void equals
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 'C':
//Do something
break;
//etc etc etc
}
}
void QUESTION(){ // 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;
//etc etc etc
}
}
void SLASH(){ // The first identifier was "/" (Annunciator)
//Do something
}
void KEYS()
{
Kstringnewstate = "";
for (int KpinNo = 22; KpinNo < 70; KpinNo++){//check for inputs on pins 8 to 70
KpinStateSTR = String(digitalRead(KpinNo)); //if you change the 8 in the line above ,, also change the 8 in the line below
KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 22));
if (KpinStateSTR != KoldpinStateSTR)//yes it's different
{
if (KpinNo != 13){ //pin 13 is no good as an input, it can "flutter" unless set up correctly
if (KpinNo == 22 && KpinStateSTR == "0"){Serial.println ("C01");} //Gear Up
if (KpinNo == 23 && KpinStateSTR == "0"){Serial.println ("C02");} //Gear Down
if (KpinNo == 50 && KpinStateSTR == "0"){Serial.println ("B341");} //Auto Throttle On
if (KpinNo == 51 && KpinStateSTR == "0"){Serial.println ("B340");} //Auto Throttle Off
if (KpinNo == 52 && KpinStateSTR == "0"){Serial.println ("B301");} //Flight Director On
if (KpinNo == 53 && KpinStateSTR == "0"){Serial.println ("B300");} //Flight Director Off
if (KpinNo > 9){//now the keys bit
Serial.print ("D");
if (KpinNo < 10) Serial.print ("0");
Serial.print (KpinNo);
Serial.println (KpinStateSTR);
}//end of keys bit
}//end of its not pin 13
}//end of yes its different
Kstringnewstate += KpinStateSTR;
}//end of 'For' loop
Kstringoldstate = Kstringnewstate;
}// end of "inputs" void
void ENCODER(){
R =(quad1.position()); //The /2 is to suit the encoder(See my website)
if (R != Rold) { // checks to see if it different
(Rdif = (R-Rold));// finds out the difference
if (Rdif == 1) Serial.println ("A56");// Course decrease
if (Rdif == -1) Serial.println ("A55"); // Course increase
Rold = R; // overwrites the old reading with the new one.
}
R2 =(quad2.position()/2);
if (R2 != Rold2) {
(Rdif2 = (R2-Rold2));
if (Rdif2 == 1) Serial.println ("B15"); // Speed decrease
if (Rdif2 == -1) Serial.println ("B16");// Speed increase
Rold2 = R2;
}
R3 =(quad3.position()/2);
if (R3 != Rold3) {
(Rdif3 = (R3-Rold3));
if (Rdif3 == 1) Serial.println ("A57"); // Heading decrease
if (Rdif3 == -1) Serial.println ("A58"); // Heading increase
Rold3 = R3;
}
R4 =(quad4.position());
if (R4 != Rold4) {
(Rdif4 = (R4-Rold4));
if (Rdif4 == 1) Serial.println ("B11"); // Altitude decrease
if (Rdif4 == -1) Serial.println ("B12"); // Altitude increase
Rold4 = R4;
}
}