TRACE.ino 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/trace
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/trace
  8. *
  9. * Describe: TRACE.
  10. * Date: 2021/9/1
  11. *******************************************************************************
  12. Please connect to PORT-A, Read the status of the four line-tracking sensors of
  13. TRACE Unit and output them through UART. 请连接端口A,读取TRACE
  14. Unit四个线跟踪传感器的状态,并通过UART输出。
  15. */
  16. #include <M5Stack.h>
  17. // #define VALUE_SPLIT
  18. uint8_t value;
  19. int SensorArray[4] = {0};
  20. void SensorStatus(void) {
  21. Wire.beginTransmission(
  22. 0x5a); // The data transfer to 0x5A begins. 开始向0x5a传输数据
  23. Wire.write(0x00);
  24. Wire.endTransmission(); // End the data transmission.结束数据传输
  25. Wire.requestFrom(0x5a, 1); // Request a byte from 0x5a. 向0x5a请求一个字节
  26. while (Wire.available()) { // If data is received. 如果数据被接收到
  27. value = Wire.read();
  28. }
  29. M5.Lcd.print(" value = ");
  30. M5.Lcd.println(value, HEX);
  31. #ifdef VALUE_SPLIT
  32. SensorArray[3] = (value & 0x08) >> 3;
  33. SensorArray[2] = (value & 0x04) >> 2;
  34. SensorArray[1] = (value & 0x02) >> 1;
  35. SensorArray[0] = (value & 0x01) >> 0;
  36. M5.Lcd.println(" After splitting... ");
  37. M5.Lcd.print(" SensorArray[0] = ");
  38. M5.Lcd.println(SensorArray[0]);
  39. M5.Lcd.print(" SensorArray[1] = ");
  40. M5.Lcd.println(SensorArray[1]);
  41. M5.Lcd.print(" SensorArray[2] = ");
  42. M5.Lcd.println(SensorArray[2]);
  43. M5.Lcd.print(" SensorArray[3] = ");
  44. M5.Lcd.println(SensorArray[3]);
  45. #endif
  46. }
  47. void setup() {
  48. M5.begin();
  49. M5.Power.begin();
  50. Wire.begin();
  51. M5.Lcd.setTextColor(YELLOW);
  52. M5.Lcd.setTextSize(2);
  53. M5.Lcd.setCursor(80, 0);
  54. M5.Lcd.println("TRACE example");
  55. M5.Lcd.setTextColor(WHITE);
  56. }
  57. void loop() {
  58. M5.Lcd.fillRect(0, 20, 320, 180, BLACK);
  59. M5.Lcd.setCursor(100, 70);
  60. SensorStatus();
  61. delay(100);
  62. }