Advertisement
Eli_Beeblebrox

Untitled

Jul 29th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.92 KB | None | 0 0
  1. const int numReadingsZ = 5;
  2. const int numReadingsX = 5;
  3. const int numReadingsY = 5;
  4. const int numReadingsZrotate = 5;
  5. const int numReadingssliderLeft = 5;
  6. const int numReadingssliderRight = 5;
  7.  
  8. int readingsZ[numReadingsZ]; // the readings from the analog input
  9. int readIndexZ = 0; // the index of the current reading
  10. int totalZ = 0; // the running total
  11. int averageZ = 0; // the average
  12. int readingsX[numReadingsX];
  13. int readIndexX = 0;
  14. int totalX = 0;
  15. int averageX = 0;
  16. int readingsY[numReadingsY];
  17. int readIndexY = 0;
  18. int totalY = 0;
  19. int averageY = 0;
  20. int readingsZrotate[numReadingsZrotate];
  21. int readIndexZrotate = 0;
  22. int totalZrotate = 0;
  23. int averageZrotate = 0;
  24. int readingssliderLeft[numReadingssliderLeft];
  25. int readIndexsliderLeft = 0;
  26. int totalsliderLeft = 0;
  27. int averagesliderLeft = 0;
  28. int readingssliderRight[numReadingssliderRight];
  29. int readIndexsliderRight = 0;
  30. int totalsliderRight = 0;
  31. int averagesliderRight = 0;
  32.  
  33. int inputPinZ = A4;
  34. int inputPinX = A5;
  35. int inputPinY = A6;
  36. int inputPinZrotate = A8;
  37. int inputPinsliderLeft = A7;
  38. int inputPinsliderRight = A9;
  39.  
  40. void setup() {
  41.  
  42. Serial.begin(9600);
  43. for (int thisReadingZ = 0; thisReadingZ < numReadingsZ; thisReadingZ++) { // initialize serial communication with computer:
  44. readingsZ[thisReadingZ] = 0; // initialize all the readings to 0:
  45. }
  46. for (int thisReadingX = 0; thisReadingX < numReadingsX; thisReadingX++) {
  47. readingsX[thisReadingX] = 0;
  48. }
  49. for (int thisReadingY = 0; thisReadingY < numReadingsY; thisReadingY++) {
  50. readingsY[thisReadingY] = 0;
  51. }
  52. for (int thisReadingZrotate = 0; thisReadingZrotate < numReadingsZrotate; thisReadingZrotate++) {
  53. readingsZrotate[thisReadingZrotate] = 0;
  54. }
  55. for (int thisReadingsliderLeft = 0; thisReadingsliderLeft < numReadingssliderLeft; thisReadingsliderLeft++) {
  56. readingssliderLeft[thisReadingsliderLeft] = 0;
  57. }
  58. for (int thisReadingsliderRight = 0; thisReadingsliderRight < numReadingssliderRight; thisReadingsliderRight++) {
  59. readingssliderRight[thisReadingsliderRight] = 0;
  60. }
  61. pinMode(0, INPUT_PULLUP);
  62. pinMode(1, INPUT_PULLUP);
  63. pinMode(2, INPUT_PULLUP);
  64. pinMode(3, INPUT_PULLUP);
  65. pinMode(4, INPUT_PULLUP);
  66. pinMode(5, INPUT_PULLUP);
  67. pinMode(6, INPUT_PULLUP);
  68. pinMode(7, INPUT_PULLUP);
  69. pinMode(8, INPUT_PULLUP);
  70. pinMode(9, INPUT_PULLUP);
  71. pinMode(10, INPUT_PULLUP);
  72. pinMode(11, INPUT_PULLUP);
  73. pinMode(12, INPUT_PULLUP);
  74. pinMode(24, INPUT_PULLUP);
  75. pinMode(25, INPUT_PULLUP);
  76. pinMode(26, INPUT_PULLUP);
  77. pinMode(27, INPUT_PULLUP);
  78. pinMode(28, INPUT_PULLUP);
  79. pinMode(29, INPUT_PULLUP);
  80. pinMode(30, INPUT_PULLUP);
  81. pinMode(31, INPUT_PULLUP);
  82. pinMode(32, INPUT_PULLUP);
  83. pinMode(33, INPUT_PULLUP);
  84. pinMode(34, INPUT_PULLUP);
  85. pinMode(35, INPUT_PULLUP);
  86. pinMode(36, INPUT_PULLUP);
  87. pinMode(37, INPUT_PULLUP);
  88. pinMode(38, INPUT_PULLUP);
  89. pinMode(39, INPUT_PULLUP);
  90. pinMode(14, INPUT_PULLUP);
  91. pinMode(15, INPUT_PULLUP);
  92. pinMode(16, INPUT_PULLUP);
  93. pinMode(17, INPUT_PULLUP);
  94. }
  95.  
  96. void loop() {
  97.  
  98. totalZ = totalZ - readingsZ[readIndexZ]; // subtract the last reading:
  99. readingsZ[readIndexZ] = analogRead(inputPinZ); // read from the sensor:
  100. totalZ = totalZ + readingsZ[readIndexZ]; // add the reading to the total:
  101. readIndexZ = readIndexZ + 1; // advance to the next position in the array:
  102. if (readIndexZ >= numReadingsZ) { // if we're at the end of the array...
  103. readIndexZ = 0; // ...wrap around to the beginning:
  104. }
  105. averageZ = totalZ / numReadingsZ; // calculate the average:
  106.  
  107. // averageZ = map(averageZ, 695, 780, 0, 1023);
  108. // averageZ = constrain(averageZ, 0, 1023);
  109.  
  110. totalX = totalX - readingsX[readIndexX];
  111. readingsX[readIndexX] = analogRead(inputPinX);
  112. totalX = totalX + readingsX[readIndexX];
  113. readIndexX = readIndexX + 1;
  114. if (readIndexX >= numReadingsX) {
  115. readIndexX = 0;
  116. }
  117. averageX = totalX / numReadingsX;
  118. totalY = totalY - readingsY[readIndexY];
  119. readingsY[readIndexY] = analogRead(inputPinY);
  120. totalY = totalY + readingsY[readIndexY];
  121. readIndexY = readIndexY + 1;
  122. if (readIndexY >= numReadingsY) {
  123. readIndexY = 0;
  124. }
  125. averageY = totalY / numReadingsY;
  126. totalZrotate = totalZrotate - readingsZrotate[readIndexZrotate];
  127. readingsZrotate[readIndexZrotate] = 1023-analogRead(inputPinZrotate);
  128. totalZrotate = totalZrotate + readingsZrotate[readIndexZrotate];
  129. readIndexZrotate = readIndexZrotate + 1;
  130. if (readIndexZrotate >= numReadingsZrotate) {
  131. readIndexZrotate = 0;
  132. }
  133. averageZrotate = totalZrotate / numReadingsZrotate;
  134. totalsliderLeft = totalsliderLeft - readingssliderLeft[readIndexsliderLeft];
  135. readingssliderLeft[readIndexsliderLeft] = analogRead(inputPinsliderLeft);
  136. totalsliderLeft = totalsliderLeft + readingssliderLeft[readIndexsliderLeft];
  137. readIndexsliderLeft = readIndexsliderLeft + 1;
  138. if (readIndexsliderLeft >= numReadingssliderLeft) {
  139. readIndexsliderLeft = 0;
  140. }
  141. averagesliderLeft = totalsliderLeft / numReadingssliderLeft;
  142. totalsliderRight = totalsliderRight - readingssliderRight[readIndexsliderRight];
  143. readingssliderRight[readIndexsliderRight] = analogRead(inputPinsliderRight);
  144. totalsliderRight = totalsliderRight + readingssliderRight[readIndexsliderRight];
  145. readIndexsliderRight = readIndexsliderRight + 1;
  146. if (readIndexsliderRight >= numReadingssliderRight) {
  147. readIndexsliderRight = 0;
  148. }
  149. averagesliderRight = totalsliderRight / numReadingssliderRight;
  150.  
  151.  
  152.  
  153. Serial.println(averageZ); // send it to the computer as ASCII digits
  154.  
  155. Joystick.Z(averageZ);
  156. Joystick.X(averageX);
  157. Joystick.Y(averageY);
  158. Joystick.Zrotate(averageZrotate);
  159. Joystick.sliderLeft(averagesliderLeft);
  160. Joystick.sliderRight(averagesliderRight);
  161.  
  162. if (digitalRead(0) == LOW) {
  163. Joystick.button(1, 1);
  164. }
  165. else {
  166. Joystick.button(1, 0);
  167. }
  168. if (digitalRead(1) == LOW) {
  169. Joystick.button(2, 1);
  170. }
  171. else {
  172. Joystick.button(2, 0);
  173. }
  174. if (digitalRead(2) == LOW) {
  175. Joystick.button(3, 1);
  176. }
  177. else {
  178. Joystick.button(3, 0);
  179. }
  180. if (digitalRead(3) == LOW) {
  181. Joystick.button(4, 1);
  182. }
  183. else {
  184. Joystick.button(4, 0);
  185. }
  186. if (digitalRead(4) == LOW) {
  187. Joystick.button(5, 1);
  188. }
  189. else {
  190. Joystick.button(5, 0);
  191. }
  192. if (digitalRead(5) == LOW) {
  193. Joystick.button(6, 1);
  194. }
  195. else {
  196. Joystick.button(6, 0);
  197. }
  198. if (digitalRead(6) == LOW) {
  199. Joystick.button(7, 1);
  200. }
  201. else {
  202. Joystick.button(7, 0);
  203. }
  204. if (digitalRead(7) == LOW) {
  205. Joystick.button(8, 1);
  206. }
  207. else {
  208. Joystick.button(8, 0);
  209. }
  210. if (digitalRead(8) == LOW) {
  211. Joystick.button(9, 1);
  212. }
  213. else {
  214. Joystick.button(9, 0);
  215. }
  216. if (digitalRead(9) == LOW) {
  217. Joystick.button(10, 1);
  218. }
  219. else {
  220. Joystick.button(10, 0);
  221. }
  222. if (digitalRead(10) == LOW) {
  223. Joystick.button(11, 1);
  224. }
  225. else {
  226. Joystick.button(11, 0);
  227. }
  228. if (digitalRead(11) == LOW) {
  229. Joystick.button(12, 1);
  230. }
  231. else {
  232. Joystick.button(12, 0);
  233. }
  234. if (digitalRead(12) == LOW) {
  235. Joystick.button(13, 1);
  236. }
  237. else {
  238. Joystick.button(13, 0);
  239. }
  240. if (digitalRead(24) == LOW) {
  241. Joystick.button(14, 1);
  242. }
  243. else {
  244. Joystick.button(14, 0);
  245. }
  246. if (digitalRead(25) == LOW) {
  247. Joystick.button(15, 1);
  248. }
  249. else {
  250. Joystick.button(15, 0);
  251. }
  252. if (digitalRead(26) == LOW) {
  253. Joystick.button(16, 1);
  254. }
  255. else {
  256. Joystick.button(16, 0);
  257. }
  258. if (digitalRead(27) == LOW) {
  259. Joystick.button(17, 1);
  260. }
  261. else {
  262. Joystick.button(17, 0);
  263. }
  264. if (digitalRead(28) == LOW) {
  265. Joystick.button(18, 1);
  266. }
  267. else {
  268. Joystick.button(18, 0);
  269. }
  270. if (digitalRead(29) == LOW) {
  271. Joystick.button(19, 1);
  272. }
  273. else {
  274. Joystick.button(19, 0);
  275. }
  276. if (digitalRead(30) == LOW) {
  277. Joystick.button(20, 1);
  278. }
  279. else {
  280. Joystick.button(20, 0);
  281. }
  282. if (digitalRead(31) == LOW) {
  283. Joystick.button(21, 1);
  284. }
  285. else {
  286. Joystick.button(21, 0);
  287. }
  288. if (digitalRead(32) == LOW) {
  289. Joystick.button(22, 1);
  290. }
  291. else {
  292. Joystick.button(22, 0);
  293. }
  294. if (digitalRead(33) == LOW) {
  295. Joystick.button(23, 1);
  296. }
  297. else {
  298. Joystick.button(23, 0);
  299. }
  300. if (digitalRead(34) == LOW) {
  301. Joystick.button(24, 1);
  302. }
  303. else {
  304. Joystick.button(24, 0);
  305. }
  306. if (digitalRead(35) == LOW) {
  307. Joystick.button(25, 1);
  308. }
  309. else {
  310. Joystick.button(25, 0);
  311. }
  312. if (digitalRead(36) == LOW) {
  313. Joystick.button(26, 1);
  314. }
  315. else {
  316. Joystick.button(26, 0);
  317. }
  318. if (digitalRead(37) == LOW) {
  319. Joystick.button(27, 1);
  320. }
  321. else {
  322. Joystick.button(27, 0);
  323. }
  324. if (digitalRead(38) == LOW) {
  325. Joystick.button(28, 1);
  326. }
  327. else {
  328. Joystick.button(28, 0);
  329. }
  330. if (digitalRead(39) == LOW) {
  331. Joystick.button(29, 1);
  332. }
  333. else {
  334. Joystick.button(29, 0);
  335. }
  336. if (digitalRead(14) == LOW) {
  337. Joystick.button(30, 1);
  338. }
  339. else {
  340. Joystick.button(30, 0);
  341. }
  342. if (digitalRead(15) == LOW) {
  343. Joystick.button(31, 1);
  344. }
  345. else {
  346. Joystick.button(31, 0);
  347. }
  348. if (digitalRead(16) == LOW) {
  349. Joystick.button(32, 1);
  350. }
  351. else {
  352. Joystick.button(32, 0);
  353. }
  354. // if (digitalRead(17) == LOW) {
  355. // Joystick.button(X, 1);
  356. // }
  357. // else {
  358. // Joystick.button(X, 0);
  359. // }
  360.  
  361. // delay(1); // delay in between reads for stability
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement