PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

74 lines
1.9KB

  1. /* @file EventSerialKeypad.pde
  2. || @version 1.0
  3. || @author Alexander Brevig
  4. || @contact alexanderbrevig@gmail.com
  5. ||
  6. || @description
  7. || | Demonstrates using the KeypadEvent.
  8. || #
  9. */
  10. #include <Keypad.h>
  11. const byte ROWS = 4; //four rows
  12. const byte COLS = 3; //three columns
  13. char keys[ROWS][COLS] = {
  14. {'1','2','3'},
  15. {'4','5','6'},
  16. {'7','8','9'},
  17. {'*','0','#'}
  18. };
  19. byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
  20. byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
  21. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  22. byte ledPin = 13;
  23. boolean blink = false;
  24. boolean ledPin_state;
  25. void setup(){
  26. Serial.begin(9600);
  27. pinMode(ledPin, OUTPUT); // Sets the digital pin as output.
  28. digitalWrite(ledPin, HIGH); // Turn the LED on.
  29. ledPin_state = digitalRead(ledPin); // Store initial LED state. HIGH when LED is on.
  30. keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
  31. }
  32. void loop(){
  33. char key = keypad.getKey();
  34. if (key) {
  35. Serial.println(key);
  36. }
  37. if (blink){
  38. digitalWrite(ledPin,!digitalRead(ledPin)); // Change the ledPin from Hi2Lo or Lo2Hi.
  39. delay(100);
  40. }
  41. }
  42. // Taking care of some special events.
  43. void keypadEvent(KeypadEvent key){
  44. switch (keypad.getState()){
  45. case PRESSED:
  46. if (key == '#') {
  47. digitalWrite(ledPin,!digitalRead(ledPin));
  48. ledPin_state = digitalRead(ledPin); // Remember LED state, lit or unlit.
  49. }
  50. break;
  51. case RELEASED:
  52. if (key == '*') {
  53. digitalWrite(ledPin,ledPin_state); // Restore LED state from before it started blinking.
  54. blink = false;
  55. }
  56. break;
  57. case HOLD:
  58. if (key == '*') {
  59. blink = true; // Blink the LED when holding the * key.
  60. }
  61. break;
  62. }
  63. }