Advertisement
ghowse

Teensy

Apr 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. const int numReadings = 10;
  2.  
  3. int readings[numReadings]; // the readings from the analog input
  4. int readIndex = 0; // the index of the current reading
  5. int total = 0; // the running total
  6. int average = 0; // the average
  7. int inputPin = 38;
  8.  
  9.  
  10. int throttleNow;
  11. int throttleThisSession;
  12. int throttledeviation;
  13. int throttleVal;
  14.  
  15. int brakeNow;
  16. int brakeThisSession;
  17. int brakedeviation;
  18. int brakeVal;
  19.  
  20. void setup() {
  21. // Analog wheel
  22.  
  23. // initialize serial communication with computer:
  24. Serial.begin(9600);
  25. // initialize all the readings to 0:
  26. for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  27. readings[thisReading] = 0;
  28. }
  29.  
  30. // Analog throttle pedal
  31. pinMode(39, INPUT);
  32. throttleThisSession = analogRead(39);
  33. throttledeviation = throttleThisSession;
  34.  
  35. // Analog brake pedal
  36. pinMode(40, INPUT);
  37. brakeThisSession = analogRead(40);
  38. brakedeviation = brakeThisSession;
  39.  
  40. //Left buttons
  41. pinMode(27, INPUT_PULLUP);
  42. pinMode(0, INPUT_PULLUP);
  43. pinMode(1, INPUT_PULLUP);
  44. pinMode(2, INPUT_PULLUP);
  45.  
  46. //Right buttons
  47. pinMode(3, INPUT_PULLUP);
  48. pinMode(4, INPUT_PULLUP);
  49. pinMode(5, INPUT_PULLUP);
  50. pinMode(10, INPUT_PULLUP);
  51.  
  52. //Hat
  53. pinMode(11, INPUT_PULLUP);
  54. pinMode(12, INPUT_PULLUP);
  55. pinMode(13, INPUT_PULLUP);
  56. pinMode(14, INPUT_PULLUP);
  57.  
  58. //Centre button
  59. pinMode(15, INPUT_PULLUP);
  60.  
  61. //Mode button
  62. pinMode(16, INPUT_PULLUP);
  63. }
  64.  
  65. void loop() {
  66. // put your main code here, to run repeatedly:
  67. // Analog wheel
  68. // subtract the last reading:
  69. total = total - readings[readIndex];
  70. // read from the sensor:
  71. readings[readIndex] = analogRead(inputPin);
  72. // add the reading to the total:
  73. total = total + readings[readIndex];
  74. // advance to the next position in the array:
  75. readIndex = readIndex + 1;
  76.  
  77. // if we're at the end of the array...
  78. if (readIndex >= numReadings) {
  79. // ...wrap around to the beginning:
  80. readIndex = 0;
  81. }
  82.  
  83. // calculate the average:
  84. average = total / numReadings;
  85. // send it to the computer as ASCII digits
  86. Serial.println(average);
  87. delay(1); // delay in between reads for stability
  88. Joystick.X(average);
  89.  
  90.  
  91. //Throttle pedal
  92. throttleNow = analogRead(39)- 20;
  93. throttleVal = (throttledeviation - throttleNow);
  94. map(throttleVal, 0, 1, 0, 1023);
  95. Joystick.Z(throttleVal);
  96.  
  97. //Brake pedal
  98. brakeNow = (0.99 * analogRead(40));
  99. brakeVal = (brakedeviation - brakeNow);
  100. map(brakeVal, 0, 1, 0, 1023);
  101. Joystick.slider(brakeVal);
  102.  
  103.  
  104. // All buttons________
  105. if (digitalRead(27) == LOW) { Joystick.button(1, 1); }
  106. else
  107. { Joystick.button(1, 0);}
  108.  
  109. // ________
  110. if (digitalRead(0) == LOW) { Joystick.button(2, 1); }
  111. else
  112. { Joystick.button(2, 0);}
  113.  
  114. // ________
  115. if (digitalRead(1) == LOW) { Joystick.button(3, 1); }
  116. else
  117. { Joystick.button(3, 0);}
  118.  
  119. // ________
  120. if (digitalRead(2) == LOW) { Joystick.button(4, 1); }
  121. else
  122. { Joystick.button(4, 0);}
  123.  
  124. // ________
  125. if (digitalRead(3) == LOW) { Joystick.button(5, 1); }
  126. else
  127. { Joystick.button(5, 0);}
  128.  
  129. // ________
  130. if (digitalRead(4) == LOW) { Joystick.button(6, 1); }
  131. else
  132. { Joystick.button(6, 0);}
  133.  
  134. // ________
  135. if (digitalRead(5) == LOW) { Joystick.button(7, 1); }
  136. else
  137. { Joystick.button(7, 0);}
  138.  
  139. // ________
  140. if (digitalRead(10) == LOW) { Joystick.button(8, 1); }
  141. else
  142. { Joystick.button(8, 0);}
  143.  
  144.  
  145. // ________
  146. if (digitalRead(15) == LOW) { Joystick.button(13, 1); }
  147. else
  148. { Joystick.button(13, 0);}
  149.  
  150. // ________
  151. if (digitalRead(16) == LOW) { Joystick.button(14, 1); }
  152. else
  153. { Joystick.button(14, 0);}
  154.  
  155.  
  156. //Hat buttons
  157. // Hat up________
  158. if (digitalRead(11) == LOW) { Joystick.hat(0); }
  159.  
  160. // Hat 45________
  161. if ((digitalRead(11) == LOW)
  162. && (digitalRead(13) ==LOW)) { Joystick.hat(45); }
  163.  
  164. // Hat right________
  165. if (digitalRead(13) == LOW) { Joystick.hat(90); }
  166.  
  167. // Hat 135________
  168. if ((digitalRead(13) == LOW)
  169. && (digitalRead(12) ==LOW)) { Joystick.hat(135); }
  170.  
  171. // Hat down________
  172. if (digitalRead(12) == LOW) { Joystick.hat(180); }
  173.  
  174. // Hat 225________
  175. if ((digitalRead(14) == LOW)
  176. && (digitalRead(12) ==LOW)) { Joystick.hat(225); }
  177.  
  178. // Hat left________
  179. if (digitalRead(14) == LOW) { Joystick.hat(270); }
  180.  
  181. // Hat 315________
  182. if ((digitalRead(14) == LOW)
  183. && (digitalRead(11) ==LOW)) { Joystick.hat(315); }
  184.  
  185. // Hat centred
  186. if ((digitalRead(11) == HIGH)
  187. && (digitalRead(12) == HIGH)
  188. && (digitalRead(13) == HIGH)
  189. && (digitalRead(14) == HIGH)) {Joystick.hat(-1); }
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement