DUAL_BUTTON.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Description: Read the button status of DUAL BUTTON Unit and display it on the screen.
  3. */
  4. #include <M5Stack.h>
  5. int last_value_red = 0;
  6. int cur_value_red = 0;
  7. int last_value_blue = 0;
  8. int cur_value_blue = 0;
  9. void setup() {
  10. // init lcd
  11. M5.begin();
  12. M5.Power.begin();
  13. pinMode(26, INPUT);
  14. pinMode(36, INPUT);
  15. M5.Lcd.clear(BLACK);
  16. M5.Lcd.setTextColor(YELLOW);
  17. M5.Lcd.setTextSize(2);
  18. M5.Lcd.setCursor(40, 0);
  19. M5.Lcd.println("Dual Button example");
  20. M5.Lcd.setTextColor(WHITE);
  21. }
  22. void loop() {
  23. cur_value_red = digitalRead(26);
  24. cur_value_blue = digitalRead(36);
  25. M5.Lcd.setCursor(0,25); M5.Lcd.print("Blue Status: ");
  26. M5.Lcd.setCursor(0,45); M5.Lcd.print("Blue Value: ");
  27. M5.Lcd.setCursor(0,65); M5.Lcd.print(" Red Status: ");
  28. M5.Lcd.setCursor(0,85); M5.Lcd.print(" Red Value: ");
  29. if(cur_value_blue != last_value_blue){
  30. M5.Lcd.fillRect(160,25,100,25,BLACK);
  31. M5.Lcd.fillRect(160,45,100,25,BLACK);
  32. if(cur_value_blue==0){
  33. M5.Lcd.setCursor(160,25); M5.Lcd.print("pressed");
  34. M5.Lcd.setCursor(160,45); M5.Lcd.print("0");
  35. Serial.println("Button Status: blue pressed");
  36. Serial.println(" value: 0");
  37. }
  38. else{
  39. M5.Lcd.setCursor(160,25); M5.Lcd.print("released");
  40. M5.Lcd.setCursor(160,45); M5.Lcd.print("1");
  41. Serial.println("Button Status: blue released");
  42. Serial.println(" value: 1");
  43. }
  44. last_value_blue = cur_value_blue;
  45. }
  46. if(cur_value_red != last_value_red){
  47. M5.Lcd.fillRect(160,65,100,25,BLACK);
  48. M5.Lcd.fillRect(160,85,100,25,BLACK);
  49. if(cur_value_red==0){
  50. M5.Lcd.setCursor(160,65); M5.Lcd.print("pressed");
  51. M5.Lcd.setCursor(160,85); M5.Lcd.print("0");
  52. Serial.println("Button Status: red pressed");
  53. Serial.println(" value: 0");
  54. }
  55. else{
  56. M5.Lcd.setCursor(160,65); M5.Lcd.print("released");
  57. M5.Lcd.setCursor(160,85); M5.Lcd.print("1");
  58. Serial.println("Button Status: red released");
  59. Serial.println(" value: 1");
  60. }
  61. last_value_red = cur_value_red;
  62. }
  63. M5.update();
  64. }