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.

_6pack.ino 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include <HID-Project.h>
  2. #include <HID-Settings.h>
  3. #include <TimerOne.h>
  4. //debounce milliseconds
  5. const int debounce = 5;
  6. //Switch Pins
  7. const byte k[6] = { 4, 5, 6, 7, 8, 9 };
  8. //Switch status
  9. boolean s[6] = { 0, 0, 0, 0, 0, 0 };
  10. //M for Media Key, K for Keyboard
  11. const char codetype[6] = { 'M', 'K', 'M', 'K', 'K', 'K' };
  12. //Keycodes
  13. const ConsumerKeycode ccode[6] = { MEDIA_VOLUME_MUTE,
  14. MEDIA_VOLUME_MUTE,
  15. MEDIA_PLAY_PAUSE,
  16. MEDIA_PLAY_PAUSE,
  17. MEDIA_PLAY_PAUSE,
  18. MEDIA_PLAY_PAUSE
  19. };
  20. const KeyboardKeycode kcode[6] = { KEY_ESC,
  21. KEY_UP_ARROW,
  22. KEY_BACKSPACE,
  23. KEY_LEFT_ARROW,
  24. KEY_DOWN_ARROW,
  25. KEY_RIGHT_ARROW
  26. };
  27. byte col = 0;
  28. byte leds[3][2];
  29. const int keyfade = 40;
  30. const char curve[] = {
  31. 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 1
  32. };
  33. const int fadecount = 2;
  34. const unsigned long breathcount = 5000;
  35. int fadecounter = 0;
  36. unsigned long breathcounter = 0;
  37. byte pass = 1;
  38. // pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1)
  39. int pins[6] = {
  40. -1, 10, 18, 19, 20, 21
  41. };
  42. // col[xx] of leds = pin yy on led matrix
  43. int cols[3] = {
  44. pins[3], pins[2], pins[1]
  45. };
  46. // row[xx] of leds = pin yy on led matrix
  47. int rows[2] = {
  48. pins[5], pins[4]
  49. };
  50. void setup() {
  51. Keyboard.begin();
  52. Consumer.begin();
  53. setupLeds();
  54. onLeds();
  55. //setup inputs, turn on pullups
  56. for (int i = 0; i <= 5; i++) {
  57. pinMode(k[i], INPUT);
  58. digitalWrite(k[i], 1);
  59. }
  60. }
  61. void loop() {
  62. CheckKeys();
  63. delay(debounce);
  64. fadecounter++;
  65. if (fadecounter > fadecount) {
  66. fadecounter = 0;
  67. fade();
  68. }
  69. breathcounter++;
  70. if (breathcounter > breathcount) {
  71. breathcounter = 0;
  72. fadecounter = 0;
  73. breath();
  74. }
  75. }
  76. void CheckKeys() {
  77. for (int i = 0; i <= 5; i++) {
  78. if (codetype[i] == 'M') {
  79. if (digitalRead(k[i]) == 0) {
  80. if (s[i] == 0) {
  81. Consumer.press((ccode[i]));
  82. s[i] = 1;
  83. byte row = i / 3;
  84. byte col = i % 3;
  85. leds[col][row] = 60;
  86. breathcounter = 0;
  87. }
  88. }
  89. else {
  90. if (s[i] == 1) {
  91. s[i] = 0;
  92. Consumer.release((ccode[i]));
  93. byte row = i / 3;
  94. byte col = i % 3;
  95. leds[col][row] = 60;
  96. breathcounter = 0;
  97. }
  98. }
  99. }
  100. if (codetype[i] == 'K') {
  101. if (digitalRead(k[i]) == 0) {
  102. if (s[i] == 0) {
  103. Keyboard.press((kcode[i]));
  104. s[i] = 1;
  105. byte row = i / 3;
  106. byte col = i % 3;
  107. leds[col][row] = 60;
  108. breathcounter = 0;
  109. }
  110. }
  111. else {
  112. if (s[i] == 1) {
  113. s[i] = 0;
  114. Keyboard.release((kcode[i]));
  115. byte row = i / 3;
  116. byte col = i % 3;
  117. leds[col][row] = 60;
  118. breathcounter = 0;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. void setupLeds() {
  125. // sets the pins as output
  126. for (int i = 1; i <= 6; i++) {
  127. pinMode(pins[i], OUTPUT);
  128. }
  129. // set up cols and rows
  130. for (int i = 1; i <= 3; i++) {
  131. digitalWrite(cols[i - 1], HIGH);
  132. }
  133. for (int i = 1; i <= 2; i++) {
  134. digitalWrite(rows[i - 1], LOW);
  135. }
  136. clearLeds();
  137. // Set refresh rate (interrupt timeout period)
  138. Timer1.initialize(500);
  139. // Set interrupt routine to be called
  140. Timer1.attachInterrupt(display);
  141. }
  142. void clearLeds() {
  143. // Clear display array
  144. for (int i = 0; i < 3; i++) {
  145. for (int j = 0; j < 2; j++) {
  146. leds[i][j] = 0;
  147. }
  148. }
  149. }
  150. void onLeds() {
  151. for (int i = 0; i < 3; i++) {
  152. for (int j = 0; j < 2; j++) {
  153. leds[i][j] = keyfade;
  154. }
  155. }
  156. }
  157. void fade() {
  158. for (int i = 0; i < 3; i++) {
  159. for (int j = 0; j < 2; j++) {
  160. if (leds[i][j] > 0) {
  161. leds[i][j] = leds[i][j] - 1;
  162. }
  163. }
  164. }
  165. }
  166. void breath() {
  167. for (int i = 0; i < 3; i++) {
  168. for (int j = 0; j < 2; j++) {
  169. leds[i][j] = 60;
  170. }
  171. }
  172. }
  173. // Interrupt routine
  174. void display() {
  175. digitalWrite(cols[col], HIGH); // Turn whole previous column off
  176. col++;
  177. if (col == 3) {
  178. col = 0;
  179. pass++;
  180. if (pass > 8) {
  181. pass = 1;
  182. }
  183. }
  184. for (int row = 0; row < 2; row++) {
  185. if (curve[leds[col][row]] > pass) {
  186. digitalWrite(rows[row], HIGH); // Turn on this led
  187. }
  188. else {
  189. digitalWrite(rows[row], LOW); // Turn off this led
  190. }
  191. }
  192. digitalWrite(cols[col], LOW); // Turn whole column on at once (for equal lighting times)
  193. }