-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_rc.ino
91 lines (81 loc) · 1.65 KB
/
bluetooth_rc.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
// Global Variables.
int state = 0, motorPins[] = {8, 9, 10 ,11};
/*
* This function moves the car forward.
*/
void forward()
{
digitalWrite(motorPins[0], HIGH);
digitalWrite(motorPins[1], LOW);
digitalWrite(motorPins[2], HIGH);
digitalWrite(motorPins[3], LOW);
}
/*
* The function move the car backward.
*/
void backward()
{
digitalWrite(motorPins[0], LOW);
digitalWrite(motorPins[1], HIGH);
digitalWrite(motorPins[2], LOW);
digitalWrite(motorPins[3], HIGH);
}
/*
* This function moves the car left.
*/
void left()
{
digitalWrite(motorPins[0], LOW);
digitalWrite(motorPins[1], LOW);
digitalWrite(motorPins[2], HIGH);
digitalWrite(motorPins[3], LOW);
}
/*
* This function moves the car right.
*/
void right()
{
digitalWrite(motorPins[0], HIGH);
digitalWrite(motorPins[1], LOW);
digitalWrite(motorPins[2], LOW);
digitalWrite(motorPins[3], LOW);
}
void setup()
{
// Setting up the serial monitor.
Serial.begin(9600);
// Setting the motor pins to output.
for(int i = 8; i <= 11; i++){
pinMode(i, OUTPUT);
}
}
void loop()
{
// Check if we have any data to process.
if(Serial.available() > 0)
{
// Reading the data.
state = Serial.read();
// Take action acording to the ASCII value recieved.
if(state == 'F'){
forward();
}
else if(state == 'B'){
backward();
}
else if(state == 'L'){
left();
}
else if(state == 'R'){
right();
}
// Stop all the motors if no input is given by the user.
if(state == 'S'){
Serial.println(state);
for(int i = 0; i < 4; i++)
{
digitalWrite(motorPins[i], LOW);
}
}
}
}