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.

4pack.ino 4.5KB

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