STEPMOTOR.ino 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Description: STEP-MOTOR Module TEST Example,If Button A was pressed, stepmotor will rotate back and forth at a time.
  3. */
  4. #include <M5Stack.h>
  5. #include <Wire.h>
  6. /*
  7. * The I2C address of StepMotor Module is 0x70 by default.
  8. * But if you change this I2C address through burning this firmware
  9. * (https://github.com/m5stack/stepmotor_module/blob/master/Firmware%20for%20stepmotor%20module/GRBL-Arduino-Library/examples/GRBL_I2C/GRBL_I2C_0x71.hex),
  10. * you need to use I2C address `0x71` for correct communication.
  11. */
  12. #define STEPMOTOR_I2C_ADDR 0x70
  13. // #define STEPMOTOR_I2C_ADDR 0x71
  14. void setup() {
  15. // put your setup code here, to run once:
  16. M5.begin();
  17. M5.Power.begin();
  18. Wire.begin();
  19. Serial.begin(115200);
  20. m5.Lcd.setTextColor(WHITE, BLACK);
  21. m5.Lcd.setTextSize(2);
  22. m5.lcd.setBrightness(100);
  23. M5.Lcd.setCursor(4, 10);
  24. M5.Lcd.println("StepMotor Test: 0x70");
  25. M5.Lcd.setCursor(4, 30);
  26. M5.Lcd.println("Press A: 0x70");
  27. }
  28. void SendByte(byte addr, byte b) {
  29. Wire.beginTransmission(addr);
  30. Wire.write(b);
  31. Wire.endTransmission();
  32. }
  33. void SendCommand(byte addr, char *c) {
  34. Wire.beginTransmission(addr);
  35. while ((*c) != 0) {
  36. Wire.write(*c);
  37. c++;
  38. }
  39. Wire.write(0x0d);
  40. Wire.write(0x0a);
  41. Wire.endTransmission();
  42. }
  43. void loop() {
  44. /*
  45. If Button A was pressed,
  46. stepmotor will rotate back and forth at a time
  47. */
  48. if (digitalRead(39) == LOW) // A button
  49. {
  50. while (digitalRead(39) == LOW) delay(1);
  51. SendCommand(STEPMOTOR_I2C_ADDR, "G1 X20Y20Z20 F500");
  52. SendCommand(STEPMOTOR_I2C_ADDR, "G1 X0Y0Z0 F400");
  53. }
  54. if (digitalRead(37) == LOW) // C button
  55. {
  56. while (1) {
  57. SendCommand(STEPMOTOR_I2C_ADDR, "G1 X0Y0Z0 F500");
  58. delay(1000);
  59. M5.Lcd.print(".");
  60. delay(1000);
  61. SendCommand(STEPMOTOR_I2C_ADDR, "G1 X5Y5Z5 F500");
  62. delay(1000);
  63. M5.Lcd.print(".");
  64. delay(1000);
  65. }
  66. }
  67. // Get Data from Module.
  68. Wire.requestFrom(STEPMOTOR_I2C_ADDR, 1);
  69. if (Wire.available() > 0) {
  70. int u = Wire.read();
  71. if (u != 0) Serial.write(u);
  72. }
  73. delay(1);
  74. // Send Data to Module.
  75. while (Serial.available() > 0) {
  76. int inByte = Serial.read();
  77. SendByte(STEPMOTOR_I2C_ADDR, inByte);
  78. }
  79. }