AccelStepperI2C  v 0.1
I2C wrapper (and a bit more) for the AccelStepper Arduino library
Stepper_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 
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  if (!wrapper.ping()) {
26  Serial.println("Slave not found! Check connections and restart.");
27  while (true) {}
28  }
29 
30  wrapper.reset(); // reset the slave device
31  delay(500); // and give it time to reboot
32 
33  stepper.attach(); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
34  // attach() replaces the AccelStepper constructor, so it could also be called like this:
35  // stepper.attach(AccelStepper::DRIVER, /* step */ 6, /* dir */ 3); // wemos d1 mini D7 and D6
36  // (important: "D7" and "D6" might mean different pins for the master, so
37  // it's safer to use the integers they replace)
38 
39  if (stepper.myNum < 0) { // should not happen after a reset
40  Serial.println("Error: stepper could not be allocated");
41  while (true) {}
42  }
43 
44  // Change these to suit your stepper if you want
45  stepper.setMaxSpeed(2000);
46  stepper.setAcceleration(50);
47  stepper.moveTo(30000);
48 
49 }
50 
51 /* This is the recommended AccelStepperI2C implementation using the state machine.
52  * Note that the polling frequency is not critical, as the state machine will stop
53  * on its own. So even if stepper.distanceToGo() causes some I2C traffic, it will be
54  * substantially less traffic than sending each stepper step seperately (see below).
55  * If you want to cut down I2C polling completely, you can use the interrupt mechanism
56  * (see interrupt example).
57  */
58 void loop()
59 {
60  stepper.runState(); // start the state machine with the set target and parameters
61  while (stepper.distanceToGo() != 0) { // wait until target has been reached
62  delay(250); // just to demonstrate that polling frequency is not critical
63  }
64  stepper.moveTo(-stepper.currentPosition());
65 }
66 
67 /* This is the "classic" implementation which uses the original polling functions.
68  * It will work, at least in low performance situations, but will clog the I2C bus as
69  * each single (attempted) stepper step needs to be sent via I2C.
70  */
72 {
73  if (stepper.distanceToGo() == 0)
74  { stepper.moveTo(-stepper.currentPosition()); }
75  stepper.run(); // frequency is critical, but each call will cause I2C traffic...
76 }
I2Cwrapper
A helper class for the AccelStepperI2C (and ServoI2C) library.
Definition: I2Cwrapper.h:79
i2cAddress
uint8_t i2cAddress
Definition: Stepper_Bounce.ino:14
setup
void setup()
Definition: Stepper_Bounce.ino:19
wrapper
I2Cwrapper wrapper(i2cAddress)
Definition: Stepper_Bounce.ino:17
loop
void loop()
Definition: Stepper_Bounce.ino:58
AccelStepperI2C
An I2C wrapper class for the AccelStepper library.
Definition: AccelStepperI2C.h:163
AccelStepperI2C.h
loopClassic
void loopClassic()
Definition: Stepper_Bounce.ino:71
I2Cwrapper::ping
bool ping()
Test if slave is listening.
Definition: I2Cwrapper.cpp:102
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:108