-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepper_clockwise_counterclockwise.ino
56 lines (47 loc) · 1.8 KB
/
stepper_clockwise_counterclockwise.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Stepper Motor Control -- Simple Reliability Test
Motor will rotate a certain number of revolutions in one direction and back to orignal position
*/
#include <Stepper.h>
#define SPEED 60 //motor speed
#define STEP 200 //number of steps in one revolution
#define MAXSTEPCOUNT 400 //used to count number of revolutions in each direction in one cycle, number of revolution * STEP
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
int stepCount = 0; // counter for the number of steps the motor has taken
bool CW = false; //change this to true if want to start with clockwise
// initialize the stepper library on pins 4 through 7 (Arduino Motor Shield):
Stepper myStepper(stepsPerRevolution, 4, 6, 5, 7);
void setup() {
myStepper.setSpeed(SPEED); //set the speed of the motor
Serial.begin(9600); // initialize the serial port
}
void loop(){
if (CW == false){ //for counterclockwise
counterclockwiseStep();
stepCount = stepCount+STEP; //increments the counter for number of steps
//delay(500); //uncomment for the motor to pause every revolution
if(stepCount >= MAXSTEPCOUNT){ //once the number of revolutions has reached the setpoint
CW = true; //change direction
stepCount = 0; //reset counter to 0 so it will turn same number of revolutions back to position
}
}
else if (CW == true){ //for clockwise
clockwiseStep();
stepCount = stepCount+STEP;
//delay(500);
if(stepCount >= MAXSTEPCOUNT){
CW = false;
stepCount = 0;
}
}
}
void clockwiseStep(){ //motor turns clockwise in the defined number of steps
myStepper.step(STEP);
Serial.print("steps:");
Serial.println(stepCount);
}
void counterclockwiseStep(){
myStepper.step(-STEP);
Serial.print("steps:");
Serial.println(stepCount);
}