KEY-DEMO1.ino 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/unit/key
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/key
  8. *
  9. * Describe: KEY. 按键.
  10. * Date: 2022/6/1
  11. *******************************************************************************
  12. Please follow the steps below to add FastLED library:
  13. - Arduino menu --> Manage Libraries... --> FastLED --> install
  14. 在烧录前请按以下步骤添加 FastLED 库:
  15. - Arduino menu --> Manage Libraries... --> FastLED --> install
  16. */
  17. #include <FastLED.h>
  18. #include <M5Stack.h>
  19. uint8_t ledColor = 0;
  20. #define KEY_PIN 36
  21. #define DATA_PIN 26
  22. CRGB LED[1];
  23. void setup() {
  24. M5.begin();
  25. M5.Lcd.setTextSize(3);
  26. M5.Lcd.print("\n UNIT-KEY Example\n\n Key State:");
  27. /* Init key pin */
  28. pinMode(KEY_PIN, INPUT_PULLUP);
  29. /* Init RGB led */
  30. FastLED.addLeds<SK6812, DATA_PIN, GRB>(LED, 1);
  31. LED[0] = CRGB::Blue;
  32. FastLED.setBrightness(0);
  33. }
  34. void loop() {
  35. /* If Key was pressed */
  36. if (!digitalRead(KEY_PIN)) {
  37. M5.Lcd.setCursor(75, 130);
  38. M5.Lcd.print((" Pressed "));
  39. FastLED.setBrightness(255);
  40. FastLED.show();
  41. /* Hold until the key released */
  42. while (!digitalRead(KEY_PIN))
  43. ;
  44. } else {
  45. M5.Lcd.setCursor(75, 130);
  46. M5.Lcd.println(("Released"));
  47. FastLED.setBrightness(0);
  48. FastLED.show();
  49. }
  50. delay(100);
  51. }