-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbase_ard_code.ino
More file actions
164 lines (143 loc) · 3.58 KB
/
Copy pathbase_ard_code.ino
File metadata and controls
164 lines (143 loc) · 3.58 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
160
161
162
GIT REPO LINK:
https://github.com/jawjay/Guitar_Tuning
Arduino stepper code
/*
BYJ48 Stepper motor code
Connect :
IN1 >> D8
IN2 >> D9
IN3 >> D10
IN4 >> D11
VCC ... 5V Prefer to use external 5V Source
Gndc
Adapted from code found at:
http://www.instructables.com/member/Mohannad+Rawashdeh/
Written by Mark Jajeh
*/
// set digital input pins for motor
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
int Steps = 0; // Records which wiring orientatation motor is currently at
boolean Direction = true;// gre
unsigned long last_time; // records time of motor step, used to insure signal is not sent to motor too fast
unsigned long currentMillis ;
int steps_left = 4095; // use this variable to iterate over some number of steps
long time;
void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// delay(1000);
}
void loop()
{
basic_rotate();
}
void basic_rotate(){
move_x_steps(steps_left);
Serial.println(time);
Serial.println("Wait...!");
delay(2000);
Direction = !Direction;
steps_left = 4095;
}
void right_write(){
// function to move motor one step
// handles all the different motor orientations as well as recycling the direction after orientation 8
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;
}
SetDirection();// updates Steps variable
}
void stepper(int number_of_steps) {
// function to step motor number of steps
// right_write() handles all the stepper specifics
for (int x = 0; x < number_of_steps; x++) {
right_write();
}
}
void move_x_steps(int x_steps) {
while (x_steps > 0) {
currentMillis = micros();
if (currentMillis - last_time >= 1000) { // making sure motor has enough time to move and settle down
stepper(1); //
time = time + micros() - last_time;
last_time = micros();
x_steps--;
}
}
}
void SetDirection() {
// function to update steps variable after a step has been sent to motor
// Steps variable is updated based on current direction
if (Direction == 1) {
Steps++;
}
if (Direction == 0) {
Steps--;
}
if (Steps > 7) {
Steps = 0;
}
if (Steps < 0) {
Steps = 7;
}
}