-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
81 lines (69 loc) · 1.47 KB
/
main.cpp
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
#include <Arduino.h>
#include "BluetoothSerial.h"
#include <ESP32Servo.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
static const int servoPin = 13;
Servo servo1;
void setup()
{
Serial.begin(115200);
servo1.attach(servoPin);
SerialBT.begin("ESP32test"); // Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void rotateForward()
{
for (int posDegrees = 0; posDegrees <= 90; posDegrees++)
{
servo1.write(posDegrees);
Serial.println(posDegrees);
delay(20);
}
}
void rotateBack()
{
for (int posDegrees = 90; posDegrees >= 0; posDegrees--)
{
servo1.write(posDegrees);
Serial.println(posDegrees);
delay(20);
}
}
void handleCommand(int command)
{
Serial.print("Handling command: ");
Serial.println(command);
switch (command)
{
case 49: // ASCII "1"
rotateForward();
break;
case 48: // ASCII "0"
rotateBack();
break;
default:
break;
}
}
void loop()
{
if (Serial.available())
{
int serialRead = Serial.read();
Serial.print("UNO ");
Serial.println(serialRead);
SerialBT.write(serialRead);
}
if (SerialBT.available())
{
int serialBTRead = SerialBT.read();
// Serial.print("DOS ");
// Serial.println(serialBTRead);
//Serial.write(serialBTRead);
handleCommand(serialBTRead);
}
delay(20);
}