-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor_rd_menu.ino
194 lines (156 loc) · 3.98 KB
/
motor_rd_menu.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
int pwm_a = 3;
int pin_dir_a = 12;
int pwm_b = 11;
int pin_dir_b = 13;
int pin_collision_front = 2;
const int FORWARD = 1;
const int BACKWARD = 0;
const int collision_status = 7;
int direction_a = FORWARD;
int direction_b = FORWARD;
int global_speed =90;
void setup() {
Serial.begin(9600);
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(pin_dir_a, OUTPUT);
pinMode(pin_dir_b, OUTPUT);
pinMode(pin_collision_front, INPUT);
pinMode(collision_status, OUTPUT);
setMotorADirection(FORWARD);
setMotorBDirection(FORWARD);
stopMoving();
//blink col status LED to say "ready"
for(int i=0; i<5; i++){
digitalWrite (collision_status, HIGH);
delay(200);
digitalWrite (collision_status, LOW);
delay(200);
}
Serial.println("");
Serial.println("Ready");
attachInterrupt(0, collision_front, CHANGE);
// put your setup code here, to run once:
}
void collision_front() {
int r_state = digitalRead(pin_collision_front); // reads the status of the sensor
//coll_front_detected = r_state;
if(r_state == 0)
digitalWrite (collision_status, HIGH); // collision detectet, turn on the led
else
digitalWrite (collision_status, LOW); // turn off the led
//stopMoving();
// cli();
stopMoving();
}
void setMotorADirection(int dir) {
digitalWrite(pin_dir_a, dir);
direction_a = dir;
}
void setMotorBDirection(int dir) {
digitalWrite(pin_dir_b, dir);
direction_b = dir;
}
void switchDirection() {
direction_a = !direction_a;
direction_b = !direction_b;
setMotorADirection(direction_a);
setMotorBDirection(direction_b);
}
void setSpeedMotorA(unsigned int speed) {
digitalWrite(pwm_a,speed);
}
void setSpeedMotorB(unsigned int speed) {
digitalWrite(pwm_b,speed);
}
void stopMotorA(){
setSpeedMotorA(0);
}
void stopMotorB(){
setSpeedMotorB(0);
}
void moveForward() {
setMotorADirection(FORWARD);
setMotorBDirection(FORWARD);
setSpeedMotorA(global_speed);
setSpeedMotorB(global_speed);
}
void moveBackward() {
setMotorADirection(BACKWARD);
setMotorBDirection(BACKWARD);
setSpeedMotorA(global_speed);
setSpeedMotorB(global_speed);
}
void turnRight() {
//stopMotorA();
setMotorBDirection(FORWARD);
setMotorADirection(BACKWARD);
setSpeedMotorB(global_speed);
setSpeedMotorA(global_speed);
}
void turnLeft() {
//stopMotorB();
setMotorADirection(FORWARD);
setMotorBDirection(BACKWARD);
setSpeedMotorB(global_speed);
setSpeedMotorA(global_speed);
}
void stopMoving() {
stopMotorA();
stopMotorB();
}
void printMenu(){
Serial.println("(s)top (f)orward (b)ackward (l)eft (r)ight");
}
int command_found;
void loop() {
// put your main code here, to run repeatedly:
// while(Serial.available()){
// Serial.read();
//}
//wait till data is available on serial port
//Serial.println("Press a key to start me movin (again)...");
printMenu();
//wait for key
while( !Serial.available() >0) {};
// read next key
if (Serial.available() > 0) {
int inByte = Serial.read();
command_found = 1; //assume valid cmd as default;
// do something different depending on the character received.
// The switch statement expects single number values for each case;
// in this exmaple, though, you're using single quotes to tell
// the controller to get the ASCII value for the character. For
// example 'a' = 97, 'b' = 98, and so forth:
switch (inByte) {
case 'f':
Serial.println("Forward");
moveForward();
break;
case 'b':
Serial.println("Backward");
moveBackward();
break;
case 'l':
Serial.println("Left");
turnLeft();
break;
case 'r':
Serial.println("Right");
turnRight();
break;
case 's':
Serial.println("Stop");
stopMoving();
break;
default:
Serial.println("Unknown command");
command_found=0;
}
if(command_found) {
delay(1000);
stopMoving();
}
//Serial.println("Back to start");
}
}