PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

122 lines
3.3KB

  1. // Edge Test For use with the DANGER SHIELD
  2. // This code turns a LED on and incraments number displayed on the 7-segment display
  3. // whenever either PB1 or PB2 is pressed. PB1 used the debounce library and PB2 does
  4. // not. PB1 will cleanly increment the displayed value. PB2 will not alway increment
  5. // the display because the switch is not debounced.
  6. //
  7. // The bounce library also detects the rising or falling edge of the input.
  8. // This is often called a one-shot and is usefull if you want something to only
  9. // happen once when a button is pressed or released. The risingEdge method is true
  10. // for only one scan when the input goes from off-to-on. The fallingEdge method is
  11. // true for only one scan when the input goes from on-to-off. Even if you are not
  12. // using the debounce feature the the library you need to call the update method
  13. // every scan for the edge detection to work.
  14. //
  15. // Notes:
  16. // The buttons on the Danger Shield pull the input to ground when they are pressed,
  17. // so they act like normally closed switches. If order to make them work like one
  18. // would expect, he tests for the button states are reversed. This also reversesd
  19. // the rising edge test on button1 to a falling edge test.
  20. //
  21. // Since PB2 is not debounced, the edge detection is done directly in the sketch.
  22. //
  23. #include <Bounce.h>
  24. // This code turns a led on/off through a debounced button
  25. // Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button
  26. #define BUTTON1 10
  27. #define BUTTON2 11
  28. #define BUTTON3 12
  29. #define BUZZER 3
  30. #define LED1 5
  31. #define LED2 6
  32. #define LATCH 7
  33. #define CLOCK 8
  34. #define DATA 4
  35. // Shift register bit values to display 0-9 on the seven-segment display
  36. const byte ledCharSet[16] = {
  37. B00111111, B00000110, B01011011, B01001111, B01100110,
  38. B01101101, B01111101, B00000111, B01111111, B01101111,
  39. B01110111, // a
  40. B01111100, // b
  41. B00111001, // c
  42. B01011110, // d
  43. B01111001, // e
  44. B01110001 // f
  45. };
  46. byte pressCount= 0;
  47. bool lastButton2;
  48. // Instantiate a Bounce object with a 5 millisecond debounce time
  49. Bounce bouncer1 = Bounce(BUTTON1, 5);
  50. void setup() {
  51. pinMode(BUTTON1, INPUT);
  52. pinMode(LED1, OUTPUT);
  53. pinMode(BUTTON2, INPUT);
  54. pinMode(LED2, OUTPUT);
  55. pinMode(BUTTON3, INPUT);
  56. pinMode(BUZZER, OUTPUT);
  57. pinMode(LATCH, OUTPUT);
  58. pinMode(CLOCK, OUTPUT);
  59. pinMode(DATA, OUTPUT);
  60. lastButton2 = digitalRead(BUTTON2);
  61. displayDigit(255, true);
  62. }
  63. void loop() {
  64. // Update debouncer
  65. boolean button1Changed = bouncer1.update();
  66. // Turn on or off the LED
  67. if ( !bouncer1.read()) {
  68. digitalWrite(LED1, HIGH);
  69. } else {
  70. digitalWrite(LED1, LOW);
  71. }
  72. int buttonValue2 = digitalRead(BUTTON2);
  73. if (buttonValue2 == LOW) {
  74. digitalWrite(LED2, HIGH);
  75. } else {
  76. digitalWrite(LED2, LOW);
  77. }
  78. if ( bouncer1.fallingEdge() || // rising edge of button 1
  79. (lastButton2 && !buttonValue2)) { // rising edge of button 2
  80. displayDigit(pressCount, false);
  81. pressCount++;
  82. if (pressCount > 15) { pressCount = 0; }
  83. }
  84. lastButton2 = digitalRead(BUTTON2);
  85. }
  86. void displayDigit(byte value, boolean dp) {
  87. byte shiftData = 0;
  88. if (value <= 15) { shiftData = ledCharSet[value]; }
  89. if (dp) { shiftData |= B10000000; }
  90. digitalWrite(LATCH,LOW);
  91. shiftOut(DATA, CLOCK, MSBFIRST, ~shiftData);
  92. digitalWrite(LATCH,HIGH);
  93. }