-
Notifications
You must be signed in to change notification settings - Fork 2
/
bluetooth.ino
133 lines (104 loc) · 2.5 KB
/
bluetooth.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
# Auto detect text files and perform LF normalization
* text=auto
//bluetooth connection code
String str;// raw string received from android to arduino
int blueToothVal;// stores the last value sent over via bluetooth
#include <Servo.h>
Servo myservo;
float pos = 156.5;
int ena = 5;
int enb = 6;
int in1 = 8;
int in2 = 9;
int in3 = 11;
int in4 = 10;
void setup()
{
Serial.begin(9600);// Serial 1 is for Bluetooth communication - DO NOT MODIFY
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
myservo.attach(3);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
analogWrite(ena, 150);
analogWrite(enb, 150);
delay(1000);
}
void loop()
{
bluetooth(); // Run the Bluetooth procedure to see if there is any data being sent via BT
}
void bluetooth()
{
while (Serial.available()) // Read bluetooth commands over Serial1 // Warning: If an error with Serial1 occurs, make sure Arduino Mega 2560 is Selected
{
{
str = Serial.readStringUntil('\n'); // str is the temporary variable for storing the last sring sent over bluetooth from Android devic
}
blueToothVal = (str.toInt()); // convert the string 'str' into an integer and assign it to blueToothVal
switch (blueToothVal)
{
case 1:
Serial.println("Forward");
Forward();
break;
case 2:
Serial.println("Reverse");
Reverse();
break;
case 3:
Serial.println("Left");
LeftTurn();
Stop();
break;
case 4:
Serial.println("Right");
RightTurn();
Stop();
break;
case 5:
Serial.println("Stop");
Stop();
break;
} // end of switch case
} // end of while loop Serial1 read
// if no data from Bluetooth
if (Serial.available() < 0)
{
Serial.println("No Bluetooth Data ");
}
}
void Forward()
{
myservo.write(pos);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(15);
}
void Reverse()
{
myservo.write(pos);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(15);
}
void LeftTurn()
{
for (pos; pos >= 100; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
void RightTurn(){
for (pos; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
}
void Stop()
{
myservo.write(156.5);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(100);
}