-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manual_mode_test.ino
82 lines (74 loc) · 2.24 KB
/
Manual_mode_test.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
// Import The redbot h file
#include <RedBot.h>
// Define a macro for speed to simplify changing
// the speed of the robot wihin the code
#define SPEED 200
// Define a macro for the value of the proximity sensor reading when
// it detects a white trace
#define WHITE 10
RedBotSensor Right_IR = RedBotSensor(A6);
RedBotSensor Left_IR = RedBotSensor(A7);
// intiate Motors
RedBotMotors motors;
void setup() {
// put your setup code here, to run once:
// Make sure that the robot is stopped when the code runs for the first time
motors.stop();
// Start a serial connection with baud rate 9600
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// this part check if there is a message on the serial connection
if (Serial.available() > 0) {
// Read the message till you find '\n'
String data = Serial.readStringUntil('\n');
// if the received message is "f" move forward
if (data == "f"){
forward();
// Print Forward on the monitor, this was used as a check to make sure that
// the forward command was successfully ran
Serial.write("Forward\n");
}
// if the received message is "b" move backwards
else if(data == "b"){
backward();
Serial.write("Backward\n");
}
// if the received message is "l" turn left
else if(data == "l"){
rotate_left();
Serial.write("Left\n");
}
// if the received message is "r" turn right
else if(data == "r"){
rotate_right();
Serial.write("Right\n");
}
// if the received message is "s" stop
else if(data == "s"){
motors.stop();
Serial.write("Stop\n");
}
}
}
// This function moves the robot forward
void forward(){
motors.rightMotor(SPEED);
motors.leftMotor(-SPEED);
}
// This function moves the robot backwards
void backward(){
motors.rightMotor(-SPEED);
motors.leftMotor(SPEED);
}
// This function uses the pivot command from the redbot library to
// rotate the robot to the left
void rotate_left(){
motors.pivot(-SPEED);
}
// This function uses the pivot command from the redbot library to
// rotate the robot to the right
void rotate_right(){
motors.pivot(SPEED);
}