-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluetooth_RC_Car.ino
129 lines (95 loc) · 3.37 KB
/
Bluetooth_RC_Car.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
/*
* Program written by: Abdul Lutfi
* Email: abdul@lutfi-mechatronics.engineer
* lutfi-mechatronics.engineer
*
* Last updated: 7 September 2017
*/
//Define Direction & Speed: Results from Serial Reading from Bluetooth
//F,B,L,R, etc are commands sent out by the phone app to the arduino. Do not change!
#define forward 'F'
#define backward 'B'
#define left 'L'
#define right 'R'
#define forwardRight 'I'
#define forwardLeft 'G'
#define backwardLeft 'J'
#define backwardRight 'H'
#define Stop 'S'
//Define Pinout Motor Control: Deviations due to different arduino shields possible & motor connection
//To other Users, Change leftMotor and rightMotor accordingly
#define rightMotor 10
#define leftMotor 11
#define rightDirection 12 // Right Motor: HIGH = Backwards, LOW = Forward
#define leftDirection 13 // Left Motor : HIGH = Forward , LOW = Backwards
char data = 0;
void setup() {
// put your setup code here, to run once:
//Serial1.begin(9600);
Serial.begin(9600);
pinMode(rightDirection,OUTPUT); // Defining rightDirection's Pin as Output
pinMode(leftDirection,OUTPUT); // Defining leftDirection's Pin as Output
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()) // Check availability of serial input (BlueTooth)
{
data = Serial.read(); // Read values from serial interface of BlueTooth
Serial.println(data); // Print Values on Serial Monitor (Can run Headless)
Stopped();
//Below is the switch statements to execute commands accordingly.
if (data == forward)Forward();
if (data == backward)Backward();
if (data == left)Left();
if (data == right)Right();
if (data == forwardRight)ForwardRight();
if (data == forwardLeft)ForwardLeft();
if (data == backwardLeft)BackwardLeft();
if (data == backwardRight)BackwardRight();
if (data == Stop)Stopped();
}
}
void Forward()
{
digitalWrite(rightDirection,HIGH); digitalWrite(leftDirection,LOW);
analogWrite(leftMotor,255); analogWrite(rightMotor,255);
}
void Backward()
{
digitalWrite(rightDirection,LOW); digitalWrite(leftDirection,HIGH);
analogWrite(leftMotor,255); analogWrite(rightMotor,255);
}
void Left()
{
digitalWrite(rightDirection,LOW); digitalWrite(leftDirection,LOW);
analogWrite(leftMotor,255); analogWrite(rightMotor,255);
}
void Right()
{
digitalWrite(rightDirection,HIGH); digitalWrite(leftDirection,HIGH);
analogWrite(leftMotor,255); analogWrite(rightMotor,255);
}
void ForwardRight()
{
digitalWrite(rightDirection,HIGH); digitalWrite(leftDirection,LOW);
analogWrite(leftMotor,100); analogWrite(rightMotor,255);
}
void ForwardLeft()
{
digitalWrite(rightDirection,HIGH); digitalWrite(leftDirection,LOW);
analogWrite(leftMotor,255); analogWrite(rightMotor,100);
}
void BackwardLeft()
{
digitalWrite(rightDirection,LOW); digitalWrite(leftDirection,HIGH);
analogWrite(leftMotor,100); analogWrite(rightMotor,255);
}
void BackwardRight()
{
digitalWrite(rightDirection,LOW); digitalWrite(leftDirection,HIGH);
analogWrite(leftMotor,255); analogWrite(rightMotor,100);
}
void Stopped()
{
analogWrite(leftMotor,0);analogWrite(rightMotor,0);
}