Misc files
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

analogread.ino 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #define HWSERIAL Serial1
  2. void setup() {
  3. Serial.begin(9600);
  4. HWSERIAL.begin(9600);
  5. }
  6. const unsigned long sleeptime = 2500;
  7. unsigned long sleep = 0;
  8. int aread = 0;
  9. boolean bounce = 0;
  10. // array to hold analog values for each column
  11. int col[12];
  12. // array to hold high and low values for each range
  13. int range[16][2] = {
  14. {1009, 1024},
  15. {204, 215},
  16. {343, 352},
  17. {146, 152},
  18. {507, 518},
  19. {170, 176},
  20. {255, 260},
  21. {126, 134},
  22. {686, 701},
  23. {186, 192},
  24. {295, 302},
  25. {137, 142},
  26. {409, 416},
  27. {157, 162},
  28. {227, 238},
  29. {118, 125}
  30. };
  31. // convert to switch positions
  32. boolean bin[16][4] = {
  33. {0, 0, 0, 0},
  34. {1, 0, 0, 0},
  35. {0, 1, 0, 0},
  36. {1, 1, 0, 0},
  37. {0, 0, 1, 0},
  38. {1, 0, 1, 0},
  39. {0, 1, 1, 0},
  40. {1, 1, 1, 0},
  41. {0, 0, 0, 1},
  42. {1, 0, 0, 1},
  43. {0, 1, 0, 1},
  44. {1, 1, 0, 1},
  45. {0, 0, 1, 1},
  46. {1, 0, 1, 1},
  47. {0, 1, 1, 1},
  48. {1, 1, 1, 1}
  49. };
  50. // array to hold key positions
  51. boolean keya[12][4] = {
  52. {0, 0, 0, 0},
  53. {0, 0, 0, 0},
  54. {0, 0, 0, 0},
  55. {0, 0, 0, 0},
  56. {0, 0, 0, 0},
  57. {0, 0, 0, 0},
  58. {0, 0, 0, 0},
  59. {0, 0, 0, 0},
  60. {0, 0, 0, 0},
  61. {0, 0, 0, 0},
  62. {0, 0, 0, 0},
  63. {0, 0, 0, 0}
  64. };
  65. boolean keyb[12][4] = {
  66. {0, 0, 0, 0},
  67. {0, 0, 0, 0},
  68. {0, 0, 0, 0},
  69. {0, 0, 0, 0},
  70. {0, 0, 0, 0},
  71. {0, 0, 0, 0},
  72. {0, 0, 0, 0},
  73. {0, 0, 0, 0},
  74. {0, 0, 0, 0},
  75. {0, 0, 0, 0},
  76. {0, 0, 0, 0},
  77. {0, 0, 0, 0}
  78. };
  79. void loop()
  80. {
  81. LEDsleep();
  82. readcolumns();
  83. assignkeysA();
  84. delay(5);
  85. readcolumns();
  86. assignkeysB();
  87. compkeys();
  88. if (bounce == 0)
  89. {
  90. LEDupdate();
  91. SERIALupdate();
  92. }
  93. }
  94. void readcolumns()
  95. {
  96. byte pass = 0;
  97. int comp = 0;
  98. for (int x = 0; x < 12; x++)
  99. {
  100. aread = 0;;
  101. comp = 0;
  102. pass = 0;
  103. while (pass < 3)
  104. {
  105. aread = analogRead(x + 14);
  106. if (aread != comp)
  107. {
  108. pass = 0;
  109. }
  110. comp = aread;
  111. pass++;
  112. }
  113. col[x] = aread;
  114. }
  115. }
  116. void assignkeysA()
  117. {
  118. for (int c = 0; c < 12; c++)
  119. {
  120. for (int v = 0; v < 16; v++)
  121. {
  122. if ((col[c] >= range[v][0]) && (col[c] <= range[v][1]))
  123. {
  124. for (byte x = 0; x < 4; x++)
  125. {
  126. keya[c][x] = bin[v][x];
  127. }
  128. }
  129. }
  130. }
  131. }
  132. void assignkeysB()
  133. {
  134. for (int c = 0; c < 12; c++)
  135. {
  136. for (int v = 0; v < 16; v++)
  137. {
  138. if ((col[c] >= range[v][0]) && (col[c] <= range[v][1]))
  139. {
  140. for (byte x = 0; x < 4; x++)
  141. {
  142. keyb[c][x] = bin[v][x];
  143. }
  144. }
  145. }
  146. }
  147. }
  148. void compkeys()
  149. {
  150. for (int c = 0; c < 12; c++)
  151. {
  152. for (int v = 0; v < 16; v++)
  153. {
  154. bounce = 0;
  155. for (byte x = 0; x < 4; x++)
  156. {
  157. if (keya[c][x] != keyb[c][x])
  158. {
  159. bounce = 1;
  160. }
  161. }
  162. }
  163. }
  164. }
  165. void LEDupdate()
  166. {
  167. for (int c = 0; c < 12; c++)
  168. {
  169. for (byte r = 0; r < 4; r++)
  170. {
  171. if (keya[c][r] == 1)
  172. {
  173. sleep = 0;
  174. HWSERIAL.write((r * 16) + c);
  175. Serial.println((r * 16) + c);
  176. }
  177. }
  178. }
  179. }
  180. void SERIALupdate()
  181. {
  182. for (int y = 0; y < 12; y++)
  183. {
  184. Serial.print(col[y]);
  185. Serial.print(", ");
  186. }
  187. Serial.println();
  188. }
  189. void LEDsleep()
  190. {
  191. if (sleep < sleeptime)
  192. {
  193. sleep++;
  194. }
  195. if (sleep == sleeptime)
  196. {
  197. HWSERIAL.write(random(0, 63));
  198. delay(5);
  199. }
  200. }