BUTTON.ino 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/core/gray
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
  8. *
  9. * Describe: Button. 按键
  10. * Date: 2021/8/9
  11. *******************************************************************************
  12. Please connect to Port B(36),Read the button status of BUTTON Unit and display
  13. it on the screen 请连接端口B(36),读取按键的状态并在显示屏上显示 if you don't
  14. have M5GO BOTTOM, you need change the pinMode and the digitalRead to 22, But you
  15. will not be able to use any I2C operations. 如果你没有M5GO
  16. BOTTOM,你需要改变pinMode和digitalRead到22,但是你将不能使用任何I2C操作.
  17. */
  18. #include <M5Stack.h>
  19. int last_value = 0;
  20. int cur_value = 0;
  21. void setup() {
  22. M5.begin(); // Init M5Stack. 初始化M5Stack
  23. M5.Power.begin(); // Init power 初始化电源模块
  24. pinMode(36, INPUT); // set pin mode to input.设置引脚模式为输入模式
  25. M5.Lcd.setTextColor(
  26. YELLOW); // Set the font color to yellow. 设置字体颜色为黄色
  27. M5.Lcd.setTextSize(2); // Setting the Font size. 设置字号大小
  28. M5.Lcd.setCursor(
  29. 80, 0); // Set the cursor position to (80,0). 将光标位置设置为(80,0)
  30. M5.Lcd.println("Button example");
  31. M5.Lcd.setTextColor(WHITE);
  32. }
  33. void loop() {
  34. cur_value = digitalRead(36); // read the value of BUTTON. 读取36号引脚的值
  35. M5.Lcd.setCursor(80, 25);
  36. M5.Lcd.print("Button");
  37. M5.Lcd.setCursor(0, 45);
  38. M5.Lcd.print("Value: ");
  39. M5.Lcd.setCursor(0, 85);
  40. M5.Lcd.print("State: ");
  41. if (cur_value != last_value) {
  42. M5.Lcd.fillRect(85, 45, 75, 85,
  43. BLACK); // Draw a black rectangle 75 by 85 at (85,45).
  44. // 在(85,45)处绘制宽75,高85的黑色矩形
  45. if (cur_value == 0) {
  46. M5.Lcd.setCursor(95, 45);
  47. M5.Lcd.print("0"); // display the status
  48. M5.Lcd.setCursor(95, 85);
  49. M5.Lcd.print("pre");
  50. } else {
  51. M5.Lcd.setCursor(95, 45);
  52. M5.Lcd.print("1"); // display the status
  53. M5.Lcd.setCursor(95, 85);
  54. M5.Lcd.print("rel");
  55. }
  56. last_value = cur_value;
  57. }
  58. }