-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepper_Motor_Control.ino
More file actions
159 lines (145 loc) · 4.17 KB
/
Stepper_Motor_Control.ino
File metadata and controls
159 lines (145 loc) · 4.17 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Authors: Simeng Yang, Ronald Gayowski, Nathan Sheasby
Item: Skittle Sorting Machine Code
Date: June 16, 2016
*/
// Interrupt driven Servo library for Arduino
#include <Servo.h>
// STEPPER MOTOR
const int IN1 = 8;
const int IN2 = 9;
const int IN3 = 10;
const int IN4 = 11;
int steps = 0;
boolean Direction = true;
unsigned long last_time;
unsigned long currentMillis ;
long time;
// SERVO MOTOR
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
// STEPPER
Serial.begin(9600);
// `Set to stepper pins to outputs
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
// Rotates servo through rotation between positions specified by start and end parameters
// Change is the increment (or decrement); change > 1 results in choppy motion
void runServo(int start, int end, int change) {
myservo.attach(12); // attaches the servo on pin 12 to the servo object
for (pos = start; pos <= end; pos += change) {
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position 'end' from 'start'
delay(10); // waits 10ms for the servo to reach the position
}
delay(5);
}
// Rotates stepper required # of steps to empty a sector
// In our implementation, there were 6 sectors in the wheel, with each being 60 degrees
// Full 360 is 4096 steps. Required steps is 4096 / 6 or about 700, with some upperbound leeway.
void runStepper(int steps){
while(steps>0){
currentMillis = micros();
if(currentMillis-last_time>=1000){
stepper(1);
time=time+micros()-last_time;
last_time=micros();
steps--;
}
}
delay(2000);
// If you want to change direction, refer to ChangeDirection below
Direction=!Direction;
}
// Runs through servo + stepper rotation
// Note that the effective range of rotation (total) is 120,
// as constrained by set-up dimensions,
// although the servo has 180 rotation capability
void loop()
{
// There are 4 cups, so i < 5
for (int i = 0; i < 5; i++){
// Parameters are: start position, end position and increment size
runServo(30 + 24 * (i-1), 30 + 24 * i, 1);
}
// Parmameter is: # of steps (or angle of rotation)
// Goes through one sector
runStepper(700);
}
// Different output sequences to rotate the stepper motor
void stepper(int xw){
for (int x=0;x<xw;x++){
switch(steps){
case 0:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case 1:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
break;
case 2:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 3:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 4:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 5:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 6:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 7:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
}
// Sets direction (although we did not have to change direction here)
SetDirection();
}
}
// Function to change direction of stepper motor
void SetDirection(){
// If you want to change direction upon function call, change ++ to --
// for one of the conditions
if(Direction==1){ steps++;}
if(Direction==0){ steps++; }
if(steps>7){steps=0;}
if(steps<0){steps=7; }
}