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