JOYSTICK.ino 1005 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. Description: Read JOYSTICK Unit X, Y axis offset data and button status
  3. */
  4. #include <M5Stack.h>
  5. #include "Wire.h"
  6. #define JOY_ADDR 0x52
  7. void setup() {
  8. M5.begin();
  9. M5.Power.begin();
  10. M5.Lcd.clear();
  11. M5.Lcd.setTextFont(4);
  12. M5.Lcd.setCursor(70, 0, 4);
  13. M5.Lcd.println(("Joystick Test"));
  14. //disable the speak noise
  15. dacWrite(25, 0);
  16. Wire.begin(21, 22, 400000);
  17. }
  18. uint8_t x_data;
  19. uint8_t y_data;
  20. uint8_t button_data;
  21. char data[100];
  22. void loop() {
  23. // put your main code here, to run repeatedly:
  24. Wire.requestFrom(JOY_ADDR, 3);
  25. if (Wire.available()) {
  26. x_data = Wire.read();
  27. y_data = Wire.read();
  28. button_data = Wire.read();
  29. sprintf(data, "x:%d y:%d button:%d\n", x_data, y_data, button_data);
  30. Serial.print(data);
  31. M5.Lcd.setCursor(100, 50, 4);
  32. M5.Lcd.printf("X:%d ",x_data);
  33. M5.Lcd.setCursor(100, 80, 4);
  34. M5.Lcd.printf("Y:%d ",y_data);
  35. M5.Lcd.setCursor(100, 110, 4);
  36. M5.Lcd.printf("B:%d ",button_data);
  37. }
  38. delay(200);
  39. }