AccelStepperI2C  v 0.1
I2C wrapper (and a bit more) for the AccelStepper Arduino library
Stepper_Bounce_CNCShieldv3.ino
Go to the documentation of this file.
1 /*
2  AccelStepperI2C Bounce demo
3  (c) juh 2022
4 
5  This is a equivalent of the AccelStepper Bounce.pde example adapted for a CNCv3 shield
6  https://www.airspayce.com/mikem/arduino/AccelStepper/Bounce_8pde-example.html
7 
8 */
9 
10 #include <Wire.h>
11 #include <AccelStepperI2C.h>
12 
13 
14 uint8_t i2cAddress = 0x08;
15 
16 I2Cwrapper wrapper(i2cAddress); // each slave device is represented by a wrapper...
17 AccelStepperI2C stepper(&wrapper); // ...that the stepper uses to communicate with the slave
18 
19 void setup()
20 {
21  Serial.begin(115200);
22  Wire.begin();
23  // Wire.setClock(10000); // uncomment for ESP8266 slaves, to be on the safe side
24 
25  wrapper.reset(); // reset the slave device
26  delay(500); // and give it time to reboot
27 
28  stepper.attach(AccelStepper::DRIVER, /* step */ 6, /* dir */ 3); // wemos d1 mini D7 and D6
29 
30  if (stepper.myNum < 0) { // stepper could not be allocated (should not happen after a reset)
31  log("mynum<0");
32  while (true) {}
33  }
34 
35  stepper.setEnablePin(8);
36  stepper.setPinsInverted(false, false, true); // directionInvert, stepInvert, enableInvert
37  stepper.enableOutputs();
38 
39  // Change these to suit your stepper if you want
40  stepper.setMaxSpeed(2000);
41  stepper.setAcceleration(50);
42  stepper.moveTo(30000);
43 
44 }
45 
46 /* This is the recommended AccelStepperI2C implementation using the state machine.
47  * Note that the polling frequency is not critical, as the state machine will stop
48  * on its own. So even if stepper.distanceToGo() causes some I2C traffic, it will be
49  * substantially less traffic than sending each stepper step seperately (see below).
50  * If you want to cut down I2C polling completely, you can use the interrupt mechanism
51  * (see interrupt example).
52  */
53 void loop()
54 {
55  stepper.runState(); // start the state machine with the set target and parameters
56  while (stepper.distanceToGo() != 0) { // wait until target has been reached
57  delay(250); // just to demonstrate that polling frequency is not critical
58  }
59  stepper.moveTo(-stepper.currentPosition());
60 }
61 
62 /* This is the "classic" implementation which uses the original polling functions.
63  * It will work, at least in low performance situations, but will clog the I2C bus as
64  * each (attempted) single stepper step needs to be sent via I2C.
65  */
67 {
68  if (stepper.distanceToGo() == 0)
69  { stepper.moveTo(-stepper.currentPosition()); }
70  stepper.run(); // frequency is critical, each call will cause I2C traffic
71 
72 }
i2cAddress
uint8_t i2cAddress
Definition: Stepper_Bounce_CNCShieldv3.ino:14
I2Cwrapper
A helper class for the AccelStepperI2C (and ServoI2C) library.
Definition: I2Cwrapper.h:79
AccelStepperI2C
An I2C wrapper class for the AccelStepper library.
Definition: AccelStepperI2C.h:163
AccelStepperI2C.h
wrapper
I2Cwrapper wrapper(i2cAddress)
Definition: Stepper_Bounce_CNCShieldv3.ino:17
setup
void setup()
Definition: Stepper_Bounce_CNCShieldv3.ino:19
loop
void loop()
Definition: Stepper_Bounce_CNCShieldv3.ino:53
loopClassic
void loopClassic()
Definition: Stepper_Bounce_CNCShieldv3.ino:66
log
#define log(...)
Definition: firmware.ino:94
I2Cwrapper::reset
void reset()
Tells the slave to reset to it's default state. It is recommended to reset the slave every time the m...
Definition: I2Cwrapper.cpp:107