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.

297 lines
6.4 KiB

5 years ago
  1. /*
  2. * Computer Switchboard
  3. *
  4. * Because interfacing with computers should be fun
  5. * and a keyboard is not enough.
  6. *
  7. * Let's turn a computer into an airplane (interface wise).
  8. *
  9. */
  10. //todo: debounce, see neotimer
  11. //makes serial slower so it can be read
  12. #define DEBUGMODE 0
  13. #include <avr/io.h>
  14. #include <avr/interrupt.h>
  15. /* SevenSegmentLEDdisplay102a.ino
  16. * 2017-02-20
  17. * Mel Lester Jr.
  18. * Simple example of using Shift Register with a
  19. * Single Digit Seven Segment LED Display
  20. */
  21. // Globals
  22. const int dataPin = 4; // blue wire to 74HC595 pin 14
  23. const int latchPin = 7; // green to 74HC595 pin 12
  24. const int clockPin = 8; // yellow to 74HC595 pin 11
  25. /* uncomment one of the following lines that describes your display
  26. * and comment out the line that does not describe your display */
  27. const char common = 'a'; // common anode
  28. //const char common = 'c'; // common cathode
  29. bool decPt = true; // decimal point display flag
  30. //switch
  31. // digital pin 9 has a pushbutton attached to it. Give it a name:
  32. int pushButton = 9;
  33. //rotary
  34. // -----
  35. // SimplePollRotator.ino - Example for the RotaryEncoder library.
  36. // This class is implemented for use with the Arduino environment.
  37. // Copyright (c) by Matthias Hertel, http://www.mathertel.de
  38. // This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
  39. // More information on: http://www.mathertel.de/Arduino
  40. // -----
  41. // 18.01.2014 created by Matthias Hertel
  42. // -----
  43. // This example checks the state of the rotary encoder in the loop() function.
  44. // The current position is printed on output when changed.
  45. // Hardware setup:
  46. // Attach a rotary encoder with output pins to A2 and A3.
  47. // The common contact should be attached to ground.
  48. #include <RotaryEncoder.h>
  49. // Setup a RoraryEncoder for pins A2 and A3:
  50. //RotaryEncoder encoder(A2, A1);
  51. RotaryEncoder encoder(A1, A2);
  52. int difference = 0;
  53. uint8_t segdisp = 0;
  54. // Example showing use of the MD_KeySwitch library
  55. //
  56. // Momentary switch
  57. //
  58. // Prints the switch value on the Serial Monitor
  59. // Allows setting of options to see theior effect (see setup())
  60. //
  61. #include <MD_KeySwitch.h>
  62. // This is just an average debounce -steak
  63. // what i need is a way to actually, not register if I don't pick up the depressed button
  64. // but good enough for now
  65. const uint8_t SWITCH_PIN = 9; // switch connected to this pin
  66. const uint8_t SWITCH_ACTIVE = LOW; // digital signal when switch is pressed 'on'
  67. MD_KeySwitch S(SWITCH_PIN, SWITCH_ACTIVE);
  68. void setup() {
  69. // initialize I/O pins
  70. pinMode(dataPin, OUTPUT);
  71. pinMode(latchPin, OUTPUT);
  72. pinMode(clockPin, OUTPUT);
  73. Serial.begin(9600);
  74. pinMode(pushButton, INPUT_PULLUP);
  75. S.begin();
  76. //S.enableDoublePress(true);
  77. S.enableLongPress(true);
  78. /*S.enableRepeat(true);
  79. S.enableRepeatResult(true);*/
  80. }
  81. void loop() {
  82. //decPt = !decPt; // display decimal point every other pass through loop
  83. // generate characters to display for hexidecimal numbers 0 to F
  84. /* for (int i = 0; i <= 15; i++) {
  85. byte bits = myfnNumToBits(i) ;
  86. if (decPt) {
  87. bits = bits | B00000001; // add decimal point if needed
  88. }
  89. myfnUpdateDisplay(bits); // display alphanumeric digit
  90. delay(500); // pause for 1/2 second
  91. }*/
  92. byte bits = myfnNumToBits(segdisp);
  93. if (decPt) {
  94. bits = bits | B00000001; // add decimal point if switch high
  95. }
  96. myfnUpdateDisplay(bits);
  97. //ROTARY
  98. static uint8_t pos = 0;
  99. encoder.tick();
  100. uint8_t newPos = encoder.getPosition();
  101. if (pos != newPos) {
  102. difference = pos - newPos;
  103. //Serial.print(newPos);
  104. //Serial.println();
  105. //Serial.println(difference);
  106. pos = newPos;
  107. segdisp = (segdisp + difference);
  108. segdisp = segdisp & B00000111;//only give me the last three bits
  109. //if there is anything there, so and.
  110. //gives me 0-7
  111. //Serial.println(segdisp);
  112. }
  113. //BUTTONS
  114. /* int resultb = 0;
  115. resultb = PINB;
  116. delay(100);
  117. if (resultb == 0 ){
  118. decPt = 1;
  119. Serial.print("User Pressed button: ");
  120. Serial.println(segdisp);
  121. }
  122. else{
  123. decPt = 0;
  124. }
  125. */
  126. // too fast, print serial when checking switch only
  127. // Serial.println(resultb,BIN); // noisy, but reads 0 when low.
  128. switch(S.read())
  129. {
  130. case MD_KeySwitch::KS_NULL: /* Serial.println("NULL"); */ break;
  131. case MD_KeySwitch::KS_PRESS:
  132. //Serial.println("\nSINGLE PRESS");
  133. Serial.print("User Pressed button: ");
  134. Serial.println(segdisp);
  135. break;
  136. /*
  137. case MD_KeySwitch::KS_DPRESS: Serial.print("\nDOUBLE PRESS");
  138. break;*/
  139. //case MD_KeySwitch::KS_LONGPRESS: Serial.print("\nLONG PRESS");
  140. //break; //this doesn't seem to work at all. - steak
  141. /*
  142. case MD_KeySwitch::KS_RPTPRESS: Serial.print("\nREPEAT PRESS");
  143. break;
  144. */
  145. default: Serial.print("\nUNKNOWN");
  146. break;
  147. }
  148. }
  149. void myfnUpdateDisplay(byte eightBits) {
  150. if (common == 'a') { // using a common anonde display?
  151. eightBits = eightBits ^ B11111111; // then flip all bits using XOR
  152. }
  153. digitalWrite(latchPin, LOW); // prepare shift register for data
  154. shiftOut(dataPin, clockPin, LSBFIRST, eightBits); // send data
  155. digitalWrite(latchPin, HIGH); // update display
  156. }
  157. byte myfnNumToBits(int someNumber) {
  158. switch (someNumber) {
  159. case 0:
  160. return B11111100;
  161. break;
  162. case 1:
  163. return B01100000;
  164. break;
  165. case 2:
  166. return B11011010;
  167. break;
  168. case 3:
  169. return B11110010;
  170. break;
  171. case 4:
  172. return B01100110;
  173. break;
  174. case 5:
  175. return B10110110;
  176. break;
  177. case 6:
  178. return B10111110;
  179. break;
  180. case 7:
  181. return B11100000;
  182. break;
  183. case 8:
  184. return B11111110;
  185. break;
  186. case 9:
  187. return B11110110;
  188. break;
  189. case 10:
  190. return B11101110; // Hexidecimal A
  191. break;
  192. case 11:
  193. return B00111110; // Hexidecimal B
  194. break;
  195. case 12:
  196. return B10011100; // Hexidecimal C or use for Centigrade
  197. break;
  198. case 13:
  199. return B01111010; // Hexidecimal D
  200. break;
  201. case 14:
  202. return B10011110; // Hexidecimal E
  203. break;
  204. case 15:
  205. return B10001110; // Hexidecimal F or use for Fahrenheit
  206. break;
  207. default:
  208. return B10010010; // Error condition, displays three vertical bars
  209. break;
  210. }
  211. }