CigarroPuro

Untitled

Mar 4th, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. /**
  2. * @brief This example demonstrates the use of push buttons and LEDs and how
  3. * to use shift registers and analog multiplexers to save pins.
  4. *
  5. * Connections:
  6. *
  7. * - 10: 74HC595 ST_CP
  8. * - 12: 74HC595 DS
  9. * - 13: 74HC595 SH_CP
  10. * - 18: 74HC4067 A (COM OUT/IN)
  11. * - 19: 74HC4067 S0
  12. * - 20: 74HC4067 S1
  13. * - 21: 74HC4067 S2
  14. * - 22: 74HC4067 S3
  15. *
  16. * Connect 16 momentary push buttons between the input pins of the
  17. * multiplexer and ground.
  18. * The internal pull-up resistor for the buttons will be enabled automatically,
  19. * so no external resistors are necessary.
  20. *
  21. * Connect 16 LEDs (and series resistors) between the eight outputs of the
  22. * two shift registers and ground.
  23. *
  24. * Remember to connect the enable pins of both the multiplexer and the shift
  25. * register to ground in order to enable them.
  26. * Connect the serial data output of the first shift register (QH' or Q7S) to
  27. * the serial input of the second shift register.
  28. *
  29. * Pressing the first button will turn on the first LED. Pressing it again will
  30. * turn it off again.
  31. * Pressing the second button will turn on the second LED. Pressing it again will
  32. * turn it off again, and so on.
  33. *
  34. * Written by PieterP, 2018-08-28
  35. * https://github.com/tttapa/Control-Surface
  36. */
  37.  
  38. #include <Control_Surface.h> // Include the Control Surface library.
  39.  
  40. /* Instantiate a multiplexer with three address lines. */
  41. CD74HC4067 mux = { 8, {2, 3, 4, 5} };
  42.  
  43. /* The number of buttons and LEDs */
  44. constexpr size_t n = 16;
  45.  
  46. const pin_t latchPin = 10; // Pin connected to ST_CP of 74HC595
  47. const pin_t dataPin = 11; // Pin connected to DS of 74HC595
  48. const pin_t clockPin = 12; // Pin connected to SH_CP of 74HC595
  49.  
  50. /* Instantiate a shift registers on the correct pins, most significant bit
  51. * first, and a total of 16 outputs (two eight-bit registers). */
  52. ShiftRegisterOut<n> sreg = { dataPin, clockPin, latchPin, MSBFIRST };
  53.  
  54. /* Instantiate the momentary push buttons.
  55. * It generates an array of Buttons on pins:
  56. * { mux.pin(0), mux.pin(1) ... mux.pin(15) } */
  57. auto buttons = generateIncrementalArray<Button, n>(mux.pin(0));
  58.  
  59. void setup() { /* Initialize everything. */
  60. mux.begin();
  61. sreg.begin();
  62. for (Button &button : buttons)
  63. button.begin();
  64. }
  65.  
  66. void loop() { /* Check if a button is pressed, if so toggle the LED. */
  67. for (uint8_t i = 0; i < n; ++i)
  68. if (buttons[i].getState() == Button::Falling)
  69. sreg.digitalWrite(i, !sreg.digitalRead(i));
  70. }
Advertisement
Add Comment
Please, Sign In to add comment