BUTTON.ino 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. Description: Read the button status of BUTTON Unit and display it on the screen
  3. */
  4. #include <M5Stack.h>
  5. int last_value = 0;
  6. int cur_value = 0;
  7. void setup() {
  8. // init lcd
  9. M5.begin();
  10. M5.Power.begin();
  11. Serial.begin(115200);
  12. pinMode(36, INPUT);
  13. M5.Lcd.clear(BLACK);
  14. M5.Lcd.setTextColor(YELLOW);
  15. M5.Lcd.setTextSize(2);
  16. M5.Lcd.setCursor(80, 0); M5.Lcd.println("Button example");
  17. Serial.println("Button example: ");
  18. M5.Lcd.setTextColor(WHITE);
  19. }
  20. void loop() {
  21. cur_value = digitalRead(36);// read the value of BUTTON
  22. M5.Lcd.setCursor(0,25); M5.Lcd.print("Status: ");
  23. M5.Lcd.setCursor(0,45); M5.Lcd.print("Value: ");
  24. if(cur_value != last_value){
  25. M5.Lcd.fillRect(95,25,100,25,BLACK);
  26. M5.Lcd.fillRect(95,45,100,25,BLACK);
  27. if(cur_value==0){
  28. M5.Lcd.setCursor(95,25); M5.Lcd.print("pressed");// display the status
  29. M5.Lcd.setCursor(95,45); M5.Lcd.print("0");
  30. Serial.println("Button Status: pressed");
  31. Serial.println(" value: 0");
  32. }
  33. else{
  34. M5.Lcd.setCursor(95,25); M5.Lcd.print("released");// display the status
  35. M5.Lcd.setCursor(95,45); M5.Lcd.print("1");
  36. Serial.println("Button Status: released");
  37. Serial.println(" value: 1");
  38. }
  39. last_value = cur_value;
  40. }
  41. M5.update();
  42. }