16 December 2021
multiple buttons on a single pin

Using multiple buttons on a single pin is possible. There are modules for that. Also there is a great library called AceButton to make the button click easily readable. The library needs you to set resistance for each button, this is how you read your signal and add your corresponding levels to the configuration

Source code viewer
  1. static const uint8_t BUTTON_PIN = A5;
  2. static const uint8_t NUM_LEVELS = NUM_BUTTONS + 1;
  3. static const uint16_t LEVELS[NUM_LEVELS] = {
  4. // Replace these values with your corresponding values:
  5. 0 /* left */,
  6. 30 /* up */,
  7. 86 /* down */,
  8. 165 /* rigth */,
  9. 351 /* SW5 */,
  10. 1023 /* nothing pressed */,
  11. };
  12. static LadderButtonConfig buttonConfig(
  13. BUTTON_PIN, NUM_LEVELS, LEVELS, NUM_BUTTONS, BUTTONS
  14. );
  15.  
  16. void loop () {
  17. // Using this you can read the values while you hold down buttons from Tools > Serial monitor in Arduino IDE.
  18. Serial.println(analogRead(BUTTON_PIN));
  19. }
  20.  
Programming Language: C++