Advertisement
Guest User

Untitled

a guest
Apr 5th, 2016
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1. /*READ ME:
  2. This is an example sketch for the PMDG 737 NGX MCP. This sketch controls the Speed, Heading and altitude control of the MCP, as well as the C/O button. I may expand these functionalities in the future to include all buttons, 7 segment displays and encoders on the MCP.
  3.  
  4. BEFORE YOU START:
  5. This sketch only works with arduino Mega. To make it work with UNO, please adjust the pin on the ENCODERS section (just above void setup()), void Setup(), and void ButtonsAP()
  6. Please make sure that you installed Encoder library from:http://www.pjrc.com/teensy/td_libs_Encoder.html
  7. YOU MUST HAVE the latest version of FSUIPC INSTALLED!
  8. YOU MUST START FSX BEFORE YOU START LINK2FS for this to work properly
  9.  
  10. SETUP:
  11. This sketch will only work on Link2fs Experts. It will not function using the basic link2fs.
  12. Follow these steps to set up Link2fs. Note these offsets only work for PMDG 737 NGX. For use with other airplanes, please adjust offsets accordingly.
  13. You only need to do this once, provided that you save the profile (step 3)
  14. 1)Go to Expert tab, FSUIPC(1) tab
  15. Identifier Reminder Offsets Type Hex? Format card1
  16. #g MCP_IASMach 6524 Single 000.00 V
  17. then press reset
  18. 2) Go to Expert tab, Controls tab
  19. Ident Reminder Control Hex?
  20. Z04 MCP_IAS_SET 81434
  21. Z05 MCP_HDG_SET 81436
  22. Z06 MCP_ALT_SET 81437
  23. Z07 MCP_MACH_SET 81435
  24. 3)Press 'Save all settings for all cards and startup' box. (should be on the right, under the saved profile and profile in use)
  25.  
  26. After this, upload the sketch to your arduino.
  27.  
  28. WIRING:
  29. Encoders are wired as follows:
  30. IAS/Mach is wired to pin 2 and 4
  31. Heading is wired to pin 3 and 6
  32. Altitude is wired to pin 18 and 17
  33.  
  34. Buttons are wired as follows:
  35. C/O is wired to pin 52
  36.  
  37. NOTE FOR ENCODERS:
  38. if you want to rewire your encoder, please keep this in mind:
  39. 1) Best Performance: Both signals connect to interrupt pins.
  40. 2) Good Performance: First signal connects to an interrupt pin, second to a non-interrupt pin.
  41. 3) Low Performance: Both signals connect to non-interrupt pins
  42. Interrupt pins are on the following pins:
  43. Arduino Mega: 2, 3, 18, 19, 20, 21
  44. Arduino Uno: 2,3
  45. Arduino Leonardo: 0,1,2,3
  46.  
  47.  
  48. Final note:
  49. all improvement and constructive input to improve this sketch is highly welcomed. In no way is this sketch the most effective in doing its job, so I'm looking at ways to improve it. Please go to link2fs forum and PM me (barrizqi) :)
  50. */
  51.  
  52.  
  53. #include <Encoder.h> //include encoder library. Download from:http://www.pjrc.com/teensy/td_libs_Encoder.html
  54. //void serialCheck()
  55. int CodeIn;
  56. //IAS or Mach?
  57. String MCPIN_IASMachStr;
  58. float MCPIN_IASMach, MCPIN_IASMachOld;
  59.  
  60. //void EncoderAP()
  61. int MCP_IAS = 100, MCP_IASOld = 0, MCP_Mach=0.50, MCP_MachOld=0.49, MCP_IASEncOld, MCP_IASEnc, MCP_IASEncDiff; //IAS and Mach
  62. int MCP_Heading = 0, MCP_HeadingOld=359, MCP_HeadEncOld, MCP_HeadEnc, MCP_HeadEncDiff; //Heading
  63. int MCP_Altitude = 100, MCP_AltitudeOld=0, MCP_AltEncOld, MCP_AltEnc, MCP_AltEncDiff; //Altitude
  64. long MCP_AltLong;
  65. long encPreviousMillis = 0; //for Output delay
  66.  
  67.  
  68. //void ButtonsAP()
  69. int MCP_COBtn=0, MCP_COBtnOld;
  70.  
  71. //ENCODERS
  72. Encoder IASEnc(2,4); //IAS/Mach
  73. Encoder HeadingEnc(3,6); //Heading
  74. Encoder AltitudeEnc(18,17); //Altitude
  75.  
  76. void setup() {
  77. Serial.begin(115200);
  78.  
  79. //Input buttons
  80. pinMode(52,INPUT); //C/O
  81. }
  82.  
  83. void loop() {
  84. serialCheck(); // gather information from link2fs
  85. EncoderAP(); //Codes for Encode: IAS/Mach, Heading, Altitude
  86. ButtonsAP(); //Codes for Buttons: C/O
  87. }
  88.  
  89. char getChar()// Get a character from the serial buffer, Do not change!
  90. {
  91. while(Serial.available() == 0);
  92. return((char)Serial.read());
  93. }
  94.  
  95. void serialCheck(){
  96. if (Serial.available()) {// Check if serial data has arrived from PC
  97. CodeIn = getChar();
  98. if (CodeIn == '#') {Hashtag();} //the first identifier is "#"
  99. /* if (CodeIn == '=') {EQUALS();} // The first identifier is "="
  100. if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
  101. if (CodeIn == '?') {QUESTION();}// The first identifier is "?"
  102. if (CodeIn == '/') {SLASH();}// The first identifier is "/" (Annunciators) */
  103. }
  104. }
  105.  
  106. void Hashtag(){
  107. CodeIn = getChar();
  108. switch(CodeIn) {
  109. //IAS or Mach
  110. case 'g':
  111. MCPIN_IASMachStr = " ";
  112. MCPIN_IASMachStr += getChar();
  113. MCPIN_IASMachStr += getChar();
  114. MCPIN_IASMachStr += getChar();
  115. MCPIN_IASMachStr += getChar();
  116. MCPIN_IASMachStr += getChar();
  117. MCPIN_IASMachStr += getChar(); //string should be in form 000.00
  118. MCPIN_IASMach = MCPIN_IASMachStr.toFloat();
  119. //now check if there is a change in IAS or Mach mode. if there is a change, synchronise new value directly from link2fs
  120. if (MCPIN_IASMach <=99 && MCPIN_IASMachOld >=99){ //change from IAS to mach
  121. MCP_Mach = (MCPIN_IASMach*100);}
  122. if (MCPIN_IASMach >=99 && MCPIN_IASMachOld <=99){ //change from mach to IAS
  123. MCP_IAS = MCPIN_IASMach;}
  124. MCPIN_IASMachOld = MCPIN_IASMach;
  125. break;
  126. }
  127. }
  128.  
  129.  
  130.  
  131. void EncoderAP(){
  132. //IAS/Mach control
  133. MCP_IASEnc = IASEnc.read(); //read encoder
  134. MCP_IASEncDiff = (MCP_IASEnc - MCP_IASEncOld); //is the encoder being rotated?
  135. //Check if in IAS or Mach mode
  136. if (MCPIN_IASMach >=099.00){ //is in IAS mode?
  137. if (MCP_IASEncOld != MCP_IASEnc){ //is the encoder being rotated?
  138. if (MCP_IASEncDiff ==-1){ //rotated to the right
  139. (MCP_IAS=MCP_IAS+1);} //increase Value
  140. if (MCP_IASEncDiff == 1){ //rotated to the left
  141. (MCP_IAS=MCP_IAS-1);} //decrease value
  142. if (MCP_IAS < 100){ //set lower limit, in this case 100 for IAS in PMDG
  143. (MCP_IAS = 100);}
  144. if (MCP_IAS > 340){ //set upper limit, in this case 340 for IAS in PMDG
  145. (MCP_IAS = 340);}
  146. MCP_IASEncOld = MCP_IASEnc;} // store old encoder value
  147. }
  148. if (MCPIN_IASMach <=099.00) { //if not in IAS, then must be Mach
  149. if (MCP_IASEncOld != MCP_IASEnc){
  150. if (MCP_IASEncDiff ==-1){
  151. (MCP_Mach=MCP_Mach+1);}
  152. if (MCP_IASEncDiff == 1){
  153. (MCP_Mach=MCP_Mach-1);}
  154. if (MCP_Mach < 60){
  155. (MCP_Mach = 60);}
  156. if (MCP_Mach > 82){
  157. (MCP_Mach = 82);}
  158. MCP_IASEncOld = MCP_IASEnc;}
  159. }
  160.  
  161. //Heading control
  162. MCP_HeadEnc = HeadingEnc.read();
  163. MCP_HeadEncDiff = (MCP_HeadEnc - MCP_HeadEncOld);
  164. if (MCP_HeadEncOld != MCP_HeadEnc){
  165. if (MCP_HeadEncDiff ==-1){
  166. (MCP_Heading=MCP_Heading+1);}
  167. if (MCP_HeadEncDiff == 1){
  168. (MCP_Heading=MCP_Heading-1);}
  169. if (MCP_Heading > 359){
  170. (MCP_Heading = 000);}
  171. if (MCP_Heading < 0){
  172. (MCP_Heading = 359);}
  173. MCP_HeadEncOld = MCP_HeadEnc;
  174. }
  175.  
  176. //Altitude control
  177. MCP_AltEnc = AltitudeEnc.read();
  178. MCP_AltEncDiff = (MCP_AltEnc - MCP_AltEncOld);
  179. if (MCP_AltEncOld != MCP_AltEnc){
  180. if (MCP_AltEncDiff ==-1){
  181. (MCP_Altitude=MCP_Altitude+1);}
  182. if (MCP_AltEncDiff == 1){
  183. (MCP_Altitude=MCP_Altitude-1);}
  184. if (MCP_Altitude > 500){
  185. (MCP_Altitude = 500);}
  186. if (MCP_Altitude < 0){
  187. (MCP_Altitude = 00000);}
  188. MCP_AltLong = MCP_Altitude; //convert to long, because int only ranges from -32,768 to 32,767
  189. MCP_AltEncOld = MCP_AltEnc;
  190. }
  191.  
  192.  
  193.  
  194. //now for the output data to lin2fs
  195. unsigned long encCurrentMillis = millis(); // get processor running time in milli seconds
  196. if(encCurrentMillis - encPreviousMillis > 50) { // check 50miliseconds has passed sinced last check(fiddle the 50 to suit)
  197. encPreviousMillis = encCurrentMillis;
  198. //IAS/Mach Output
  199. //if in IAS mode
  200. if (MCP_IASOld != MCP_IAS){ //is there any change in IAS from last value?
  201. Serial.print("Z04");
  202. Serial.println(MCP_IAS);
  203. MCP_IASOld = MCP_IAS; //store old IAS value
  204. }
  205. //if in Mach mode
  206. if (MCP_MachOld != MCP_Mach){
  207. Serial.print("Z07");
  208. Serial.println(MCP_Mach);
  209. MCP_MachOld = MCP_Mach;
  210. }
  211.  
  212. //Heading Output
  213. if (MCP_HeadingOld != MCP_Heading){
  214. Serial.print("Z05");
  215. Serial.println(MCP_Heading);
  216. MCP_HeadingOld = MCP_Heading;
  217. }
  218. //Altitude Output
  219. if (MCP_AltitudeOld != MCP_Altitude){
  220. Serial.print("Z06");
  221. Serial.println(MCP_AltLong*100);
  222. MCP_AltitudeOld = MCP_Altitude;
  223. }
  224. }
  225. }
  226.  
  227. void ButtonsAP(){
  228. //C/O button - IAS or mach button
  229. MCP_COBtn = digitalRead(52);
  230. if ((MCP_COBtn != MCP_COBtnOld) && (MCP_COBtnOld !=1)){
  231. if (MCPIN_IASMach >=099.00){ //is changing from IAS to Mach
  232. Serial.println("Z031");}
  233. if (MCPIN_IASMach <=099.00){ //is changing from Mach to IAS
  234. Serial.println("Z030");}
  235. }
  236. MCP_COBtnOld = MCP_COBtn;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement