Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. #include <LedControl.h>
  2. #include <Quadrature.h>
  3. #include "math.h"
  4.  
  5.  
  6.  
  7. Quadrature quad1(22, 23);
  8. Quadrature quad2(25, 26);
  9. // ****** LedControl Library is required to drive the MAX7219 7 segment LED displays. *******
  10. // led_Display_1 is the variable name of my set of displays chained
  11. LedControl led_Display_1 = LedControl(12,10,11,8); // together running off of pins 12,11,10.
  12. // Pin 12 is the DataOut
  13. // Pin 11 is Load or CS
  14. // Pin 10 is clock.
  15. // The 8 is for how many displays you have to chain.
  16. // I have 4 but put 8 incase of expansion. It cost no memory.
  17. // You can run any pins you want in the instruction,
  18. // just plug the module in correctly.
  19. // LedControl(Data,Clock,Load,Displays)
  20. // I used 12,10,11 so I could have a straight ribbon connection
  21. // from the module without crossing leads.
  22.  
  23.  
  24. int CodeIn; // used on all serial reads from Jim's code
  25. String Digit; // Variable as a string to take from getChar()
  26. int Count; // This variable used in loops in the EQUALS() function
  27. int Com1Active[5]; // AP_alt_set[5] is an array of the 5 digits that are the Auto Pilot AltitudeSet
  28. int Com1Stby[5]; // AP_alt_set[5] is an array of the 5 digits that are the Auto Pilot AltitudeSet
  29. int Nav1Active[5];
  30. int Nav1Stby[5];
  31. long Xold1;// the first encoder "old" reading
  32. long Xold2;// the second encoder "old" reading
  33. int KpinNo;
  34. int Koutpin;
  35.  
  36. String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
  37.  
  38.  
  39. //**********************************************************
  40. //*************( START of void setup() )********************
  41.  
  42. void setup()
  43. {
  44. Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
  45.  
  46. for (int KoutPin = 22; KoutPin <=70; KoutPin++)// Get all the pins ready for simconnect codes and "Keys"(all inputs)
  47. {
  48. pinMode(KoutPin, INPUT);
  49. digitalWrite(KoutPin, HIGH);
  50.  
  51. //The MAX72XX is in power-saving mode on startup, we have to do a wakeup call
  52. delay (500);
  53. led_Display_1.shutdown(0,false); // I have 4 displays, these start them up
  54. delay (500); // I put the delay in so they all dont start drawing
  55. led_Display_1.shutdown(1,false); // current at the same time.
  56. delay (500); // I had an issue with running on only USB power and getting a display glitch.
  57. led_Display_1.shutdown(2,false); // The delay seems to have fixed this issue.
  58. // delay (500);
  59. // led_Display_1.shutdown(3,false);
  60.  
  61. // Set the brightness to a lower than medium values
  62. led_Display_1.setIntensity(0,8);
  63. led_Display_1.setIntensity(1,8);
  64. led_Display_1.setIntensity(2,8);
  65. //led_Display_1.setIntensity(3,8);
  66.  
  67. // and clear the display
  68. led_Display_1.clearDisplay(0);
  69. led_Display_1.clearDisplay(1);
  70. led_Display_1.clearDisplay(2);
  71. //led_Display_1.clearDisplay(3);
  72.  
  73. Serial.begin(115200);
  74. }
  75. }
  76.  
  77. void loop() {
  78.  
  79. {INPUTS();} //Check the Simconnect and "keys" section for any input pins
  80.  
  81. long X1 =(quad1.position())/2; // for full cycle encoders
  82. if (X1 != Xold1) {
  83. if (digitalRead(24) != 0) {
  84. if (X1 > Xold1) Serial.println("A02"); else Serial.println("A01");} //Nav1 MHz
  85. else {
  86. if (X1 > Xold1) Serial.println("A04"); else Serial.println("A03");} //Nav1 KHz
  87. // end of "check for HOLD-DOWN"
  88. Xold1 = X1; // overwrites the old reading with the new one.
  89. }//end of quad1 read
  90.  
  91. long X2 =(quad2.position())/2; // for full cycle encoders
  92. if (X2 != Xold2) {
  93. if (digitalRead(27) != 0) {
  94. if (X2 > Xold2) Serial.println("A14"); else Serial.println("A13");} //Comm1 MHz
  95. else {
  96. if (X2 > Xold2) Serial.println("A16"); else Serial.println("A15");} //Comm1 Khz
  97. // end of "check for HOLD-DOWN"
  98. Xold2 = X2; // overwrites the old reading with the new one.
  99. }//end of quad2 read
  100.  
  101. if (Serial.available())
  102. {
  103. CodeIn = getChar();
  104. if (CodeIn == '=')
  105. {
  106. EQUALS(); // The first identifier is "="
  107. }
  108. }
  109. }
  110. char getChar()// Get a character from the serial buffer
  111. {
  112. while(Serial.available() == 0); // wait for data
  113. return((char)Serial.read()); // Thanks Doug
  114. }
  115.  
  116.  
  117. void EQUALS() // The first identifier was "="
  118. {
  119. CodeIn = getChar(); // Get another character
  120. switch(CodeIn) // Now lets find what to do with it
  121. {
  122. case 'A': // Comm1 Active
  123. Count = 0; // clear the Count
  124. while (Count < 5) // we will count to 5 from 0 to 4
  125. {
  126. Digit = ""; // clears the string variable Digit
  127. Digit += getChar(); // Makes the string Digit what ever getChar() is holding
  128. if (Digit ==".") // ****** This looks for the "." *************
  129. {
  130. Digit = ""; // If we find the "." we clear it and get the next digit
  131. Digit += getChar(); // Because we know where the "." always goes.
  132. }
  133. Com1Active[Count] = Digit.toInt(); // Turns the string Digit into an Integer and
  134. // then stores it in the Array AP_alt_set[] 0,1,2,3,4
  135. Count++;
  136. }
  137.  
  138. led_Display_1.setDigit(0,7,Com1Active[0],false); // First digit of Alt Set is displayed
  139. led_Display_1.setDigit(0,6,Com1Active[1],false); // Second digit of Alt Set is displayed
  140. led_Display_1.setDigit(0,5,Com1Active[2],true); // Third digit of Alt Set is displayed
  141. led_Display_1.setDigit(0,4,Com1Active[3],false); // Fourth digit of Alt Set is displayed
  142. led_Display_1.setDigit(0,3,Com1Active[4],false); // Fifth digit of Alt Set is displayed
  143. // false denotes NO decimal point
  144. break;
  145.  
  146.  
  147. case 'B': // Comm1 Stby
  148. Count = 0; // clear the Count
  149. while (Count < 5) // we will count to 5 from 0 to 4
  150. {
  151. Digit = ""; // clears the string variable Digit
  152. Digit += getChar(); // Makes the string Digit what ever getChar() is holding
  153. if (Digit ==".") // ****** This looks for the "." *************
  154. {
  155. Digit = ""; // If we find the "." we clear it and get the next digit
  156. Digit += getChar(); // Because we know where the "." always goes.
  157. }
  158. Com1Stby[Count] = Digit.toInt(); // Turns the string Digit into an Integer and
  159. // then stores it in the Array AP_alt_set[] 0,1,2,3,4
  160. Count++;
  161. }
  162.  
  163. led_Display_1.setDigit(0,1,Com1Stby[0],false); // First digit of Alt Set is displayed
  164. led_Display_1.setDigit(0,0,Com1Stby[1],false); // Second digit of Alt Set is displayed
  165. led_Display_1.setDigit(1,7,Com1Stby[2],true); // Third digit of Alt Set is displayed
  166. led_Display_1.setDigit(1,6,Com1Stby[3],false); // Fourth digit of Alt Set is displayed
  167. led_Display_1.setDigit(1,5,Com1Stby[4],false); // Fifth digit of Alt Set is displayed
  168. // false denotes NO decimal point
  169. break;
  170.  
  171.  
  172. case 'E': // Nav1Active
  173. Count = 0; // clear the Count
  174. while (Count < 5) // we will count to 5 from 0 to 4
  175. {
  176. Digit = ""; // clears the string variable Digit
  177. Digit += getChar(); // Makes the string Digit what ever getChar() is holding
  178. if (Digit ==".") // ****** This looks for the "." *************
  179. {
  180. Digit = ""; // If we find the "." we clear it and get the next digit
  181. Digit += getChar(); // Because we know where the "." always goes.
  182. }
  183. Nav1Active[Count] = Digit.toInt(); // Turns the string Digit into an Integer and
  184. // then stores it in the Array AP_alt_set[] 0,1,2,3,4
  185. Count++;
  186. }
  187.  
  188. led_Display_1.setDigit(1,2,Nav1Active[0],false); // First digit of Alt Set is displayed
  189. led_Display_1.setDigit(1,1,Nav1Active[1],false); // Second digit of Alt Set is displayed
  190. led_Display_1.setDigit(1,0,Nav1Active[2],true); // Third digit of Alt Set is displayed
  191. led_Display_1.setDigit(2,7,Nav1Active[3],false); // Fourth digit of Alt Set is displayed
  192. led_Display_1.setDigit(2,6,Nav1Active[4],false); // Fifth digit of Alt Set is displayed
  193. // false denotes NO decimal point
  194.  
  195. break;
  196. case 'F': // Nav1Stby
  197. Count = 0; // clear the Count
  198. while (Count < 5) // we will count to 5 from 0 to 4
  199. {
  200. Digit = ""; // clears the string variable Digit
  201. Digit += getChar(); // Makes the string Digit what ever getChar() is holding
  202. if (Digit ==".") // ****** This looks for the "." *************
  203. {
  204. Digit = ""; // If we find the "." we clear it and get the next digit
  205. Digit += getChar(); // Because we know where the "." always goes.
  206. }
  207. Nav1Stby[Count] = Digit.toInt(); // Turns the string Digit into an Integer and
  208. // then stores it in the Array AP_alt_set[] 0,1,2,3,4
  209. Count++;
  210. }
  211.  
  212. led_Display_1.setDigit(2,4,Nav1Stby[0],false); // First digit of Alt Set is displayed
  213. led_Display_1.setDigit(2,3,Nav1Stby[1],false); // Second digit of Alt Set is displayed
  214. led_Display_1.setDigit(2,2,Nav1Stby[2],true); // Third digit of Alt Set is displayed
  215. led_Display_1.setDigit(2,1,Nav1Stby[3],false); // Fourth digit of Alt Set is displayed
  216. led_Display_1.setDigit(2,0,Nav1Stby[4],false); // Fifth digit of Alt Set is displayed
  217. // false denotes NO decimal point
  218. }
  219. }
  220.  
  221.  
  222. void INPUTS(){ // Simconnect codes and "Keys" section
  223. Kstringnewstate = "";
  224. for (int KpinNo = 22; KpinNo <=70; KpinNo++){ //set this to the input pins. (pins 10 to 19)
  225. KpinStateSTR = String(digitalRead(KpinNo)); //read the state of the pin
  226. KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 22));// gets the 'old' state of the pin from string
  227. if (KpinStateSTR != KoldpinStateSTR){// checks if it's different to the last reading of that pinNo
  228. if (KpinNo != 13){ // avoid using pin 13 as an input unless you know the tricks.
  229. if (KpinNo == 28 && KpinStateSTR == "0"){Serial.println ("A06");} //sets gear handle up
  230. if (KpinNo == 29 && KpinStateSTR == "0"){Serial.println ("A18");} //sets gear handle up
  231.  
  232. // Add more here but remember to also change the figure in the next line down. (the 14)
  233. }//end of 'its not pin 13'
  234. }//end of 'its different'
  235. Kstringnewstate += KpinStateSTR;
  236. }//end of 'for' loop (read the pins)
  237. Kstringoldstate = Kstringnewstate;
  238. }//end of INPUTS void
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement