-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32-ble-buzzer-script.ino
72 lines (53 loc) · 1.44 KB
/
esp32-ble-buzzer-script.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
#include <BleKeyboard.h>
#define LED_POWER_PIN 13
#define LED_BLE_PIN 12
#define LED_TRIGGER_PIN 14
#define BUTTON_PIN 27
BleKeyboard bleKeyboard;
int button_state;
int last_button_state;
boolean connected = false;
boolean connectedLed = false;
void setup() {
Serial.begin(115200);
pinMode(LED_POWER_PIN, OUTPUT);
digitalWrite(LED_POWER_PIN, HIGH);
pinMode(LED_TRIGGER_PIN, OUTPUT);
digitalWrite(LED_TRIGGER_PIN, LOW);
pinMode(LED_BLE_PIN, OUTPUT);
digitalWrite(LED_BLE_PIN, LOW);
pinMode(BUTTON_PIN, INPUT_PULLUP);
button_state = digitalRead(BUTTON_PIN);
bleKeyboard.setName("Buzzer");
bleKeyboard.begin();
}
void controlConnectionLED() {
if(!connected && bleKeyboard.isConnected()) {
digitalWrite(LED_BLE_PIN, HIGH);
connectedLed = true;
}
}
void loop() {
controlConnectionLED();
connected = bleKeyboard.isConnected();
if(connected) {
last_button_state = button_state;
button_state = digitalRead(BUTTON_PIN);
delay(10);
if (last_button_state == HIGH && button_state == LOW) {
Serial.println("The button is pressed");
bleKeyboard.write(KEY_RETURN);
digitalWrite(LED_TRIGGER_PIN, HIGH);
delay(1000);
digitalWrite(LED_TRIGGER_PIN, LOW);
}
} else {
if(connectedLed) {
digitalWrite(LED_BLE_PIN, LOW);
} else {
digitalWrite(LED_BLE_PIN, HIGH);
}
connectedLed = !connectedLed;
delay(500);
}
}