Skip to content

Instantly share code, notes, and snippets.

@marc21121980
Last active December 28, 2018 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marc21121980/1a8684134bf8b7bd8a6085ce89a5ed27 to your computer and use it in GitHub Desktop.
Save marc21121980/1a8684134bf8b7bd8a6085ce89a5ed27 to your computer and use it in GitHub Desktop.
Radio Code
#include "LedControl.h"
#include "math.h"
#include "Quadrature.h"
Quadrature quad1(22, 23);
Quadrature quad2(24, 25);
// ****** LedControl Library is required to drive the MAX7219 7 segment LED displays. *******
// led_Display_1 is the variable name of my set of displays chained
LedControl led_Display_1 = LedControl(2,3,4,8); // together running off of pins 12,11,10.
// Pin 12 is the DataOut
// Pin 11 is Clock
// Pin 10 CS
// The 8 is for how many displays you have to chain.
// I have 4 but put 8 incase of expansion. It cost no memory.
// You can run any pins you want in the instruction,
// just plug the module in correctly.
// LedControl(Data,Clock,Load,Displays)
// I used 12,10,11 so I could have a straight ribbon connection
// from the module without crossing leads.
int CodeIn; // used on all serial reads from Jim's code
String Digit; // Variable as a string to take from getChar()
int Count; // This variable used in loops in the EQUALS() function
int Com2Active[5]; // Com2Active[5] is an array of the 5 digits that are the Com 2 Radio digits
int Com2Stby[5];
int Nav2Active[5];
int Nav2Stby[5];
int X1;
int X2;
int Xold1;// the first encoder "old" reading
int Xold2;// the second encoder "old" reading
int Xdif;
String avm, avmOld;
int avmi;
int KpinNo;
int Koutpin;
String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
void setup()
{
Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
for (int KoutPin = 22; KoutPin <=70; KoutPin++)// Get all the pins ready for simconnect codes and "Keys"(all inputs)
{
pinMode(KoutPin, INPUT_PULLUP);
digitalWrite(KoutPin, HIGH);
//The MAX72XX is in power-saving mode on startup, we have to do a wakeup call
delay (500);
led_Display_1.shutdown(0,false); // I have 4 displays, these start them up
delay (500); // I put the delay in so they all dont start drawing
led_Display_1.shutdown(1,false); // current at the same time.
delay (500); // I had an issue with running on only USB power and getting a display glitch.
led_Display_1.shutdown(2,false); // The delay seems to have fixed this issue.
delay (500);
led_Display_1.shutdown(3,false);
// Set the brightness to a lower than medium values
led_Display_1.setIntensity(0,8);
led_Display_1.setIntensity(1,8);
led_Display_1.setIntensity(2,8);
led_Display_1.setIntensity(3,8);
// and clear the display
led_Display_1.clearDisplay(0);
led_Display_1.clearDisplay(1);
led_Display_1.clearDisplay(2);
led_Display_1.clearDisplay(3);
Serial.begin(115200);
}
}
void loop() {
{INPUTS();} //Check the Simconnect and "keys" section for any input pins
{ROTARYS();} // Check Rotary Encoder
if (Serial.available())
{
CodeIn = getChar();
if (CodeIn == '=') {EQUALS();}// The first identifier is "="
if (CodeIn == '?') {QUESTION();}// 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 'C': // Comm2 Active
Count = 0; // clear the Count
while (Count < 5) // we will count to 5 from 0 to 4
{
Digit = ""; // clears the string variable Digit
Digit += getChar(); // Makes the string Digit what ever getChar() is holding
if (Digit ==".") // ****** This looks for the "." *************
{
Digit = ""; // If we find the "." we clear it and get the next digit
Digit += getChar(); // Because we know where the "." always goes.
}
Com2Active[Count] = Digit.toInt(); // Turns the string Digit into an Integer and
// then stores it in the Array AP_alt_set[] 0,1,2,3,4
Count++;
}
led_Display_1.setDigit(0,7,Com2Active[0],false); // First digit of Com2 Active is displayed
led_Display_1.setDigit(0,6,Com2Active[1],false); // Second digit of Com2 Active is displayed
led_Display_1.setDigit(0,5,Com2Active[2],true); // Third digit of Com2 Active is displayed
led_Display_1.setDigit(0,4,Com2Active[3],false); // Fourth digit of Com2 Active is displayed
led_Display_1.setDigit(0,3,Com2Active[4],false); // Fifth digit of Com2 Active is displayed
// false denotes NO decimal point
break;
case 'D': // Comm2 Stby
Count = 0; // clear the Count
while (Count < 5) // we will count to 5 from 0 to 4
{
Digit = ""; // clears the string variable Digit
Digit += getChar(); // Makes the string Digit what ever getChar() is holding
if (Digit ==".") // ****** This looks for the "." *************
{
Digit = ""; // If we find the "." we clear it and get the next digit
Digit += getChar(); // Because we know where the "." always goes.
}
Com2Stby[Count] = Digit.toInt(); // Turns the string Digit into an Integer and
// then stores it in the Array AP_alt_set[] 0,1,2,3,4
Count++;
}
led_Display_1.setDigit(0,1,Com2Stby[0],false); // First digit of Comm2 Stby is displayed
led_Display_1.setDigit(0,0,Com2Stby[1],false); // Second digit of Comm2 Stby is displayed
led_Display_1.setDigit(1,7,Com2Stby[2],true); // Third digit of Comm2 Stby is displayed
led_Display_1.setDigit(1,6,Com2Stby[3],false); // Fourth digit of Comm2 Stby is displayed
led_Display_1.setDigit(1,5,Com2Stby[4],false); // Fifth digit of Comm2 Stby is displayed
// false denotes NO decimal point
break;
case 'G': // Nav2 Active
Count = 0; // clear the Count
while (Count < 5) // we will count to 5 from 0 to 4
{
Digit = ""; // clears the string variable Digit
Digit += getChar(); // Makes the string Digit what ever getChar() is holding
if (Digit ==".") // ****** This looks for the "." *************
{
Digit = ""; // If we find the "." we clear it and get the next digit
Digit += getChar(); // Because we know where the "." always goes.
}
Nav2Active[Count] = Digit.toInt(); // Turns the string Digit into an Integer and
// then stores it in the Array AP_alt_set[] 0,1,2,3,4
Count++;
}
led_Display_1.setDigit(1,2,Nav2Active[0],false); // First digit of Nav2 Active is displayed
led_Display_1.setDigit(1,1,Nav2Active[1],false); // Second digit of Nav2 Active is displayed
led_Display_1.setDigit(1,0,Nav2Active[2],true); // Third digit of Nav2 Active is displayed
led_Display_1.setDigit(2,7,Nav2Active[3],false); // Fourth digit of Nav2 Active is displayed
led_Display_1.setDigit(2,6,Nav2Active[4],false); // Fifth digit of Nav2 Active is displayed
// false denotes NO decimal point
break;
case 'H': // Nav2 Stby
Count = 0; // clear the Count
while (Count < 5) // we will count to 5 from 0 to 4
{
Digit = ""; // clears the string variable Digit
Digit += getChar(); // Makes the string Digit what ever getChar() is holding
if (Digit ==".") // ****** This looks for the "." *************
{
Digit = ""; // If we find the "." we clear it and get the next digit
Digit += getChar(); // Because we know where the "." always goes.
}
Nav2Stby[Count] = Digit.toInt(); // Turns the string Digit into an Integer and
// then stores it in the Array AP_alt_set[] 0,1,2,3,4
Count++;
}
led_Display_1.setDigit(2,4,Nav2Stby[0],false); // First digit of Nav2 Stby is displayed
led_Display_1.setDigit(2,3,Nav2Stby[1],false); // Second digit of Nav2 Stby is displayed
led_Display_1.setDigit(2,2,Nav2Stby[2],true); // Third digit of Nav2 Stby is displayed
led_Display_1.setDigit(2,1,Nav2Stby[3],false); // Fourth digit of Nav2 Stby is displayed
led_Display_1.setDigit(2,0,Nav2Stby[4],false); // Fifth digit of Nav2 Stby is displayed
// false denotes NO decimal point
} // *** End of Switch Case ***
}
//***************( End of EQUALS() Function )**************
//*********************************************************
void QUESTION() // The first identifier was "?"
{
CodeIn = getChar(); // Get another character
switch(CodeIn) // Now lets find what to do with it
{
case 'U': // When the avionics bus voltage is 0 (when avionics master switch is selected
avm = ""; // All displays will blank
avm += getChar();
avmi = avm.toInt();
if (avmi != 0)
{
led_Display_1.setDigit(0,7,Com2Active[0],false); // First digit of Com2Active is displayed
led_Display_1.setDigit(0,6,Com2Active[1],false); // Second digit of Com2Active is displayed
led_Display_1.setDigit(0,5,Com2Active[2],true); // Third digit of Com2Active is displayed
led_Display_1.setDigit(0,4,Com2Active[3],false); // Fourth digit of Com2Active is displayed
led_Display_1.setDigit(0,3,Com2Active[4],false); // Fith digit of Com2 Active is displayed
led_Display_1.setDigit(0,1,Com2Stby[0],false); // First digit of Com2 Stby is displayed
led_Display_1.setDigit(0,0,Com2Stby[1],false); // Second digit of Com2 Stby is displayed
led_Display_1.setDigit(1,7,Com2Stby[2],true); // Third digit of Com2 Stby displayed
led_Display_1.setDigit(1,6,Com2Stby[3],false); // Fourth digit of Com2 Stby is displayed
led_Display_1.setDigit(1,5,Com2Stby[4],false); // Fith digit of Com2 Stby is displayed
led_Display_1.setDigit(1,2,Nav2Active[0],false); // First digit of Nav2 Avtive is displayed
led_Display_1.setDigit(1,1,Nav2Active[1],false); // Second digit of Nav2 Avtive is displayed
led_Display_1.setDigit(1,0,Nav2Active[2],true); // Third digit of Nav2 Avtive is displayed
led_Display_1.setDigit(2,7,Nav2Active[3],false); // Fourth digit of Nav2 Avtive is displayed
led_Display_1.setDigit(2,6,Nav2Active[4],false); // Fith digit of Nav2 Avtive is displayed
led_Display_1.setDigit(2,4,Nav2Stby[0],false); // First digit of Nav2 Stby is displayed
led_Display_1.setDigit(2,3,Nav2Stby[1],false); // Second digit of Nav2 Stby is displayed
led_Display_1.setDigit(2,2,Nav2Stby[2],true); // Third digit of Nav2 Stby is displayed
led_Display_1.setDigit(2,1,Nav2Stby[3],false); // Fourth digit of Nav2 Stby is displayed
led_Display_1.setDigit(2,0,Nav2Stby[4],false); // Fith digit of Nav2 Stby is displayed
}
else
{
led_Display_1.setChar(0,7, ' ',false); //**
led_Display_1.setChar(0,6, ' ',false); // *
led_Display_1.setChar(0,5, ' ',false); // *
led_Display_1.setChar(0,4, ' ',false); // *
led_Display_1.setChar(0,3, ' ',false); // *
led_Display_1.setChar(0,1, ' ',false); // *
led_Display_1.setChar(0,0, ' ',false); // *
led_Display_1.setChar(1,7, ' ',false); // *
led_Display_1.setChar(1,6, ' ',false); // *
led_Display_1.setChar(1,5, ' ',false); // * > All displays blank when avionis master is selected off
led_Display_1.setChar(1,2, ' ',false); // *
led_Display_1.setChar(1,1, ' ',false); // *
led_Display_1.setChar(1,0, ' ',false); // *
led_Display_1.setChar(2,7, ' ',false); // *
led_Display_1.setChar(2,6, ' ',false); // *
led_Display_1.setChar(2,4, ' ',false); // *
led_Display_1.setChar(2,3, ' ',false); // *
led_Display_1.setChar(2,2, ' ',false); // *
led_Display_1.setChar(2,1, ' ',false); // *
led_Display_1.setChar(2,0, ' ',false); //**
}
}
}
void ROTARYS(){
// ******Comm 2 Encoder******
X1 =(quad1.position()/2); // for full cycle encoders
if (X1 != Xold1) { // checks to see if it is different
(Xdif = (X1-Xold1)); // finds out the difference
if (Xdif == 1) {
if (digitalRead(26) == 0){Serial.println("A07");} else Serial.println("A09");}
if (Xdif == -1){
if (digitalRead(26) == 0) {Serial.println("A08");} else Serial.println("A10");}
// end of "check for HOLD-DOWN"
Xold1 = X1; // overwrites the old reading with the new one.
}//end of quad1 read
// ******Nav 2 Encoder******
X2 =(quad2.position()/2); // for full cycle encoders
if (X2 != Xold2) { // checks to see if it is different
(Xdif = (X2-Xold2)); // finds out the difference
if (Xdif == 1) {
if (digitalRead(27) == 0){Serial.println("A19");} else Serial.println("A21");}
if (Xdif == -1){
if (digitalRead(27) == 0) {Serial.println("A20");} else Serial.println("A22");}
// end of "check for HOLD-DOWN"
Xold2 = X2; // overwrites the old reading with the new one.
}//end of quad2 read
}
void INPUTS(){ // Simconnect codes and "Keys" section
Kstringnewstate = "";
for (int KpinNo = 22; KpinNo <=70; KpinNo++){ //set this to the input pins. (pins 10 to 19)
KpinStateSTR = String(digitalRead(KpinNo)); //read the state of the pin
KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 22));// gets the 'old' state of the pin from string
if (KpinStateSTR != KoldpinStateSTR){// checks if it's different to the last reading of that pinNo
if (KpinNo != 13){ // avoid using pin 13 as an input unless you know the tricks.
if (KpinNo == 28 && KpinStateSTR == "0"){Serial.println ("A12");} //Swaps Comm 2 with Standby
if (KpinNo == 29 && KpinStateSTR == "0"){Serial.println ("A24");} //Swaps Nav 2 with Standby
if ((KpinNo == 30 && KpinStateSTR == "0") && (avmi != 0))
{
led_Display_1.setChar(0,7, ' ',false); //**
led_Display_1.setChar(0,6, ' ',false); // *
led_Display_1.setChar(0,5, ' ',false); // *
led_Display_1.setChar(0,4, ' ',false); // *
led_Display_1.setChar(0,3, ' ',false); // *
led_Display_1.setChar(0,1, ' ',false); // *
led_Display_1.setChar(0,0, ' ',false); // *
led_Display_1.setChar(1,7, ' ',false); // *
led_Display_1.setChar(1,6, ' ',false); // *
led_Display_1.setChar(1,5, ' ',false); // * > Sets all displays blank when pin 30 is HIGH
led_Display_1.setChar(1,2, ' ',false); // * For Radio OFF Switch
led_Display_1.setChar(1,1, ' ',false); // *
led_Display_1.setChar(1,0, ' ',false); // *
led_Display_1.setChar(2,7, ' ',false); // *
led_Display_1.setChar(2,6, ' ',false); // *
led_Display_1.setChar(2,4, ' ',false); // *
led_Display_1.setChar(2,3, ' ',false); // *
led_Display_1.setChar(2,2, ' ',false); // *
led_Display_1.setChar(2,1, ' ',false); // *
led_Display_1.setChar(2,0, ' ',false); //**
}
if ((KpinNo == 31 && KpinStateSTR == "0") && (avmi != 0))
{
led_Display_1.setDigit(0,7,Com2Active[0],false); // **
led_Display_1.setDigit(0,6,Com2Active[1],false); // *
led_Display_1.setDigit(0,5,Com2Active[2],true); // *
led_Display_1.setDigit(0,4,Com2Active[3],false); // *
led_Display_1.setDigit(0,3,Com2Active[4],false); // *
led_Display_1.setDigit(0,1,Com2Stby[0],false); // *
led_Display_1.setDigit(0,0,Com2Stby[1],false); // *
led_Display_1.setDigit(1,7,Com2Stby[2],true); // *
led_Display_1.setDigit(1,6,Com2Stby[3],false); // *
led_Display_1.setDigit(1,5,Com2Stby[4],false); // * > Sets all displays active when pin 31 is HIGH
led_Display_1.setDigit(1,2,Nav2Active[0],false); // * For Radio ON Switch
led_Display_1.setDigit(1,1,Nav2Active[1],false); // *
led_Display_1.setDigit(1,0,Nav2Active[2],true); // *
led_Display_1.setDigit(2,7,Nav2Active[3],false); // *
led_Display_1.setDigit(2,6,Nav2Active[4],false); // *
led_Display_1.setDigit(2,4,Nav2Stby[0],false); // *
led_Display_1.setDigit(2,3,Nav2Stby[1],false); // *
led_Display_1.setDigit(2,2,Nav2Stby[2],true); // *
led_Display_1.setDigit(2,1,Nav2Stby[3],false); // *
led_Display_1.setDigit(2,0,Nav2Stby[4],false); // **
}
}//end of 'its not pin 13'
}//end of 'its different'
Kstringnewstate += KpinStateSTR;
}//end of 'for' loop (read the pins)
Kstringoldstate = Kstringnewstate;
}//end of INPUTS void
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment