-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepperMotor.ino
123 lines (117 loc) · 2.8 KB
/
stepperMotor.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
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
#define STEPPER_PIN_1 0
#define STEPPER_PIN_2 1
#define STEPPER_PIN_3 2
#define STEPPER_PIN_4 4
int step_number = 0;
boolean dir = false;// false=clockwise,true=counter clockwise
int start_motor;
int counter = 0;
void pciSetup(byte pin)
{
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}
ISR (PCINT0_vect){
if(digitalRead(13)){
start_motor = 1;
}
if(digitalRead(10)){
start_motor = 0;
}
}
void motor(){
if(counter >=500){
counter=0;
dir= !dir;
}
if (start_motor == 1 && counter <=500){
counter ++;
switch(step_number){
case 0:
digitalWrite(STEPPER_PIN_1,LOW);
digitalWrite(STEPPER_PIN_2,LOW);
digitalWrite(STEPPER_PIN_3,LOW);
digitalWrite(STEPPER_PIN_4,HIGH);
break;
case 1:
digitalWrite(STEPPER_PIN_1,LOW);
digitalWrite(STEPPER_PIN_2,LOW);
digitalWrite(STEPPER_PIN_3,HIGH);
digitalWrite(STEPPER_PIN_4,HIGH);
break;
case 2:
digitalWrite(STEPPER_PIN_1,LOW);
digitalWrite(STEPPER_PIN_2,LOW);
digitalWrite(STEPPER_PIN_3,HIGH);
digitalWrite(STEPPER_PIN_4,LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1,LOW);
digitalWrite(STEPPER_PIN_2,HIGH);
digitalWrite(STEPPER_PIN_3,HIGH);
digitalWrite(STEPPER_PIN_4,LOW);
break;
case 4:
digitalWrite(STEPPER_PIN_1,LOW);
digitalWrite(STEPPER_PIN_2,HIGH);
digitalWrite(STEPPER_PIN_3,LOW);
digitalWrite(STEPPER_PIN_4,LOW);
break;
case 5:
digitalWrite(STEPPER_PIN_1,HIGH);
digitalWrite(STEPPER_PIN_2,HIGH);
digitalWrite(STEPPER_PIN_3,LOW);
digitalWrite(STEPPER_PIN_4,LOW);
break;
case 6:
digitalWrite(STEPPER_PIN_1,HIGH);
digitalWrite(STEPPER_PIN_2,LOW);
digitalWrite(STEPPER_PIN_3,LOW);
digitalWrite(STEPPER_PIN_4,LOW);
break;
case 7:
digitalWrite(STEPPER_PIN_1,HIGH);
digitalWrite(STEPPER_PIN_2,LOW);
digitalWrite(STEPPER_PIN_3,LOW);
digitalWrite(STEPPER_PIN_4,HIGH);
break;
default:
digitalWrite(STEPPER_PIN_1,LOW);
digitalWrite(STEPPER_PIN_2,LOW);
digitalWrite(STEPPER_PIN_3,LOW);
digitalWrite(STEPPER_PIN_4,LOW);
break;
}
if(dir){
step_number++;
}
else{
step_number--;
}
if(step_number>7){
step_number=0;
}
if(step_number<0){
step_number=7;
}
delayMicroseconds(1500);
}
else if (start_motor == 0){
digitalWrite(STEPPER_PIN_1,LOW);
digitalWrite(STEPPER_PIN_2,LOW);
digitalWrite(STEPPER_PIN_3,LOW);
digitalWrite(STEPPER_PIN_4,LOW);
}
}
void setup() {
pinMode(STEPPER_PIN_1,OUTPUT);
pinMode(STEPPER_PIN_2,OUTPUT);
pinMode(STEPPER_PIN_3,OUTPUT);
pinMode(STEPPER_PIN_4,OUTPUT);
pciSetup(13);
pciSetup(10);
}
void loop() {
motor();
}