AccelStepperI2C  v 0.1
I2C wrapper (and a bit more) for the AccelStepper Arduino library
Stepper_and_Servo_together.ino
Go to the documentation of this file.
1 /*
2  AccelStepperI2C Stepper and Servo demo
3  (c) juh 2022
4 
5  This is a combination of the stepper bounce and servo sweep examples to
6  demonstrate how steppers and servos can be used together.
7  Attach two servos and a stepstick (A4988 etc.) driven stepper to the slave
8  pins defined below.
9  Of course, the slave's firmware needs to be compiled with servo support enabled.
10 */
11 
12 #include <Wire.h>
13 #include <AccelStepperI2C.h>
14 #include <ServoI2C.h>
15 
16 uint8_t i2cAddress = 0x08;
17 const uint8_t stepPin = 6;
18 const uint8_t dirPin = 3;
19 const uint8_t enablePin = 8;
20 const uint8_t servo1Pin = 12;
21 const uint8_t servo2Pin = 13;
22 
23 I2Cwrapper wrapper(i2cAddress); // one wrapper to serve them all
25 ServoI2C servo1(&wrapper);
26 ServoI2C servo2(&wrapper);
27 
28 void setup()
29 {
30  Wire.begin();
31  // Wire.setClock(10000); // uncomment for ESP8266 slaves, to be on the safe side
32  // Serial.begin(115200); // uncomment for debugging output (needs DEBUG set in firmware/libraries)
33 
34  wrapper.reset(); // reset the slave device
35  delay(500); // and give it time to reboot
36 
37  servo1.attach(servo1Pin);
38  servo2.attach(servo2Pin);
39 
40  stepper.attach(AccelStepper::DRIVER, stepPin, dirPin);
41  if (stepper.myNum < 0) { // stepper could not be allocated (should not happen after a reset)
42  while (true) {}
43  }
44 
45  // adapt the following to your board/setup, this is for CNC v3 shields:
46  stepper.setEnablePin(enablePin);
47  stepper.setPinsInverted(false, false, true); // directionInvert, stepInvert, enableInvert
48  stepper.enableOutputs();
49 
50  // Change these to suit your stepper if you want
51  stepper.setMaxSpeed(200);
52  stepper.setAcceleration(100);
53  stepper.moveTo(5000); // set first target
54 
55 }
56 
57 int servo1pos = 90, servo2pos = 90;
58 int servo1speed = 11, servo2speed = 5;
59 
60 void loop()
61 {
62  stepper.runState(); // start the state machine with the set target and parameters
63  while (stepper.isRunning()) { // repeat until stepper target has been reached
64 
65  // sweep servo 1
66  servo1.write(servo1pos);
67  if (servo1pos + servo1speed > 180 or servo1pos + servo1speed < 0) {
68  servo1speed = -servo1speed; // turn around
69  }
71 
72  // sweep servo 2
73  servo2.write(servo2pos);
74  if (servo2pos + servo2speed > 180 or servo2pos + servo2speed < 0) {
75  servo2speed = -servo2speed; // turn around
76  }
78 
79  delay(50);
80 
81  }
82  stepper.moveTo(-stepper.currentPosition()); // turn around
83 }
I2Cwrapper
A helper class for the AccelStepperI2C (and ServoI2C) library.
Definition: I2Cwrapper.h:79
servo1speed
int servo1speed
Definition: Stepper_and_Servo_together.ino:58
i2cAddress
uint8_t i2cAddress
Definition: Stepper_and_Servo_together.ino:16
servo2Pin
const uint8_t servo2Pin
Definition: Stepper_and_Servo_together.ino:21
setup
void setup()
Definition: Stepper_and_Servo_together.ino:28
servo2speed
int servo2speed
Definition: Stepper_and_Servo_together.ino:58
servo1pos
int servo1pos
Definition: Stepper_and_Servo_together.ino:57
ServoI2C
An I2C wrapper class for the Arduino Servo library.
Definition: ServoI2C.h:65
AccelStepperI2C
An I2C wrapper class for the AccelStepper library.
Definition: AccelStepperI2C.h:163
AccelStepperI2C.h
ServoI2C.h
Arduino library for I2C-control of servo motors connected to another Arduino which runs the associate...
servo1Pin
const uint8_t servo1Pin
Definition: Stepper_and_Servo_together.ino:20
servo2pos
int servo2pos
Definition: Stepper_and_Servo_together.ino:57
stepPin
const uint8_t stepPin
Definition: Stepper_and_Servo_together.ino:17
loop
void loop()
Definition: Stepper_and_Servo_together.ino:60
dirPin
const uint8_t dirPin
Definition: Stepper_and_Servo_together.ino:18
enablePin
const uint8_t enablePin
Definition: Stepper_and_Servo_together.ino:19
wrapper
I2Cwrapper wrapper(i2cAddress)
Definition: Stepper_and_Servo_together.ino:24
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