GoPLUS.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Description: Use GoPLUS Module for four-channel servo test and three-channel
  3. ADC test.
  4. */
  5. #include <M5Stack.h>
  6. #include <Wire.h>
  7. #include "GoPlus.h"
  8. GoPlus goPlus;
  9. #define X_LOCAL 40
  10. #define Y_LOCAL 30
  11. #define X_OFFSET 160
  12. #define Y_OFFSET 23
  13. // Print the header for a display screen
  14. void header(const char *string, uint16_t color) {
  15. M5.Lcd.fillScreen(color);
  16. M5.Lcd.setTextSize(1);
  17. M5.Lcd.setTextColor(TFT_MAGENTA, TFT_BLUE);
  18. M5.Lcd.fillRect(0, 0, 320, 30, TFT_BLUE);
  19. M5.Lcd.setTextDatum(TC_DATUM);
  20. M5.Lcd.drawString(string, 160, 3, 4);
  21. }
  22. void Motor(void) {
  23. goPlus.Motor_write_speed(MOTOR_NUM0, (uint8_t)0x80, 250);
  24. goPlus.Motor_write_speed(MOTOR_NUM1, (uint8_t)0x80, 250);
  25. delay(500);
  26. goPlus.Motor_write_speed(MOTOR_NUM0, (uint8_t)0x80, 0);
  27. goPlus.Motor_write_speed(MOTOR_NUM1, (uint8_t)0x00, 0);
  28. delay(500);
  29. goPlus.Motor_write_speed(MOTOR_NUM1, (uint8_t)0x00, 250);
  30. goPlus.Motor_write_speed(MOTOR_NUM0, (uint8_t)0x00, 250);
  31. delay(500);
  32. goPlus.Motor_write_speed(MOTOR_NUM0, (uint8_t)0x80, 0);
  33. goPlus.Motor_write_speed(MOTOR_NUM1, (uint8_t)0x00, 0);
  34. delay(500);
  35. }
  36. void setup() {
  37. // put your setup code here, to run once:
  38. M5.begin();
  39. M5.Power.begin();
  40. goPlus.begin();
  41. header("G O P L U S", TFT_BLACK);
  42. M5.Lcd.setTextColor(TFT_MAGENTA, TFT_BLACK);
  43. M5.Lcd.setCursor(X_LOCAL, Y_LOCAL, 2);
  44. M5.Lcd.printf("Read analog\n");
  45. M5.Lcd.setCursor(X_LOCAL + X_OFFSET, Y_LOCAL, 2);
  46. M5.Lcd.printf("Test Servo\n");
  47. M5.Lcd.setCursor(X_OFFSET - 60, Y_LOCAL + Y_OFFSET * 5, 2);
  48. M5.Lcd.printf("Test MoTor\n");
  49. M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
  50. pinMode(13, OUTPUT);
  51. }
  52. uint8_t a = 20;
  53. uint16_t adInValue[6] = {0};
  54. int led = 0;
  55. void loop() {
  56. // put your main code here, to run repeatedly:
  57. digitalWrite(13, led);
  58. goPlus.Servo_write_angle(SERVO_NUM0, a);
  59. delay(100);
  60. goPlus.Servo_write_angle(SERVO_NUM1, a);
  61. delay(100);
  62. goPlus.Servo_write_angle(SERVO_NUM2, a);
  63. delay(100);
  64. goPlus.Servo_write_angle(SERVO_NUM3, a);
  65. delay(100);
  66. if (a > 180) {
  67. a = 10;
  68. }
  69. a = a + 170;
  70. M5.Lcd.fillRect(X_LOCAL + X_OFFSET, Y_LOCAL + Y_OFFSET, 100, 23, TFT_BLACK);
  71. M5.Lcd.setCursor(X_LOCAL + X_OFFSET, Y_LOCAL + Y_OFFSET, 2);
  72. M5.Lcd.printf("Angle:%d", a);
  73. adInValue[0] = goPlus.hub1_d_read_value(HUB_NUM0);
  74. delay(50);
  75. M5.Lcd.fillRect(X_LOCAL, Y_LOCAL + Y_OFFSET * 1, 100, 23, TFT_BLACK);
  76. M5.Lcd.setCursor(X_LOCAL, Y_LOCAL + Y_OFFSET * 1, 2);
  77. M5.Lcd.printf("A0:%d", adInValue[0]);
  78. adInValue[1] = goPlus.hub2_d_read_value(HUB_NUM0);
  79. delay(50);
  80. M5.Lcd.fillRect(X_LOCAL, Y_LOCAL + Y_OFFSET * 2, 100, 23, TFT_BLACK);
  81. M5.Lcd.setCursor(X_LOCAL, Y_LOCAL + Y_OFFSET * 2, 2);
  82. M5.Lcd.printf("A1:%d", adInValue[1]);
  83. adInValue[2] = goPlus.hub3_d_read_value(HUB_NUM0);
  84. delay(50);
  85. M5.Lcd.fillRect(X_LOCAL, Y_LOCAL + Y_OFFSET * 3, 100, 23, TFT_BLACK);
  86. M5.Lcd.setCursor(X_LOCAL, Y_LOCAL + Y_OFFSET * 3, 2);
  87. M5.Lcd.printf("A2:%d", adInValue[2]);
  88. led = !led;
  89. Motor();
  90. }