]> git.piffa.net Git - sketchbook_andrea/blob - motors/stepper/stepper_acceleration/stepper_acceleration.ino
9b22155aa7ebb738293e8da6e1fbf32fa1653624
[sketchbook_andrea] / motors / stepper / stepper_acceleration / stepper_acceleration.ino
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
11
12  - V1.01 11/30/2013
13    Questions: terry@yourduino.com */
14
15 /*-----( Import needed libraries )-----*/
16 #include <AccelStepper.h>
17 /*-----( Declare Constants and Pin Numbers )-----*/
18 #define FULLSTEP 4
19 #define HALFSTEP 8
20 // motor pins
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)
26                         
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);
36
37 /*-----( Declare Variables )-----*/
38 //none
39
40 void setup()   /****** SETUP: RUNS ONCE ******/
41 {
42   stepper1.setMaxSpeed(1000.0);
43   stepper1.setAcceleration(50.0);
44   stepper1.setSpeed(200);
45   stepper1.moveTo(2048);  // 1 revolution 
46   
47   stepper2.setMaxSpeed(1000.0);
48   stepper2.setAcceleration(50.0);
49   stepper2.setSpeed(200);
50   stepper2.moveTo(-2048);  // 1 revolution 
51
52 }//--(end setup )---
53
54
55 void loop()   /****** LOOP: RUNS CONSTANTLY ******/
56 {
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());
62   
63   stepper1.run();
64   stepper2.run();
65
66 }//--(end main loop )---
67
68 /*-----( Declare User-written Functions )-----*/
69 //none
70 //*********( THE END )***********