1 /* YourDuinoStarter Example: 2 Stepper Motors
2 - WHAT IT DOES: Runs 2 28BYJ-48 stepper motors with AccelStepper Library
3 - Motors accelerate and decelerate simultaneously in opposite rotations
4 - SEE the comments after "//" on each line below
5 - Derived from example code by Mike McCauley
6 - modified by Celem for single stepper
7 - modified by lowres for two steppers
8 NOTE: This may not run 2 motors from USB.
9 May need separate +5 Supply for motors
10 - CONNECTIONS: See Pin definitions below
13 Questions: terry@yourduino.com */
15 /*-----( Import needed libraries )-----*/
16 #include <AccelStepper.h>
17 /*-----( Declare Constants and Pin Numbers )-----*/
21 #define motorPin1 4 // Blue - 28BYJ48 pin 1
22 #define motorPin2 5 // Pink - 28BYJ48 pin 2
23 #define motorPin3 6 // Yellow - 28BYJ48 pin 3
24 #define motorPin4 7 // Orange - 28BYJ48 pin 4
25 // Red - 28BYJ48 pin 5 (VCC)
27 #define motorPin5 8 // Blue - 28BYJ48 pin 1
28 #define motorPin6 9 // Pink - 28BYJ48 pin 2
29 #define motorPin7 10 // Yellow - 28BYJ48 pin 3
30 #define motorPin8 11 // Orange - 28BYJ48 pin 4
31 // Red - 28BYJ48 pin 5 (VCC)
32 /*-----( Declare objects )-----*/
33 // NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
34 AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
35 AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
37 /*-----( Declare Variables )-----*/
40 void setup() /****** SETUP: RUNS ONCE ******/
42 stepper1.setMaxSpeed(1000.0);
43 stepper1.setAcceleration(50.0);
44 stepper1.setSpeed(200);
45 stepper1.moveTo(2048); // 1 revolution
47 stepper2.setMaxSpeed(1000.0);
48 stepper2.setAcceleration(50.0);
49 stepper2.setSpeed(200);
50 stepper2.moveTo(-2048); // 1 revolution
55 void loop() /****** LOOP: RUNS CONSTANTLY ******/
57 //Change direction at the limits
58 if (stepper1.distanceToGo() == 0)
59 stepper1.moveTo(-stepper1.currentPosition());
60 if (stepper2.distanceToGo() == 0)
61 stepper2.moveTo(-stepper2.currentPosition());
66 }//--(end main loop )---
68 /*-----( Declare User-written Functions )-----*/
70 //*********( THE END )***********