-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathButton.ino
58 lines (45 loc) · 1.52 KB
/
Button.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
#include <CurieBLE.h>
// create peripheral instance
BLEPeripheral blePeripheral;
// create service
BLEService buttonService = BLEService("FFE0");
// create characteristic
BLECharCharacteristic buttonCharacteristic = BLECharCharacteristic("FFE1", BLENotify);
BLEDescriptor buttonDescriptor = BLEDescriptor("2901", "Button State");
#define BUTTON_PIN 7
unsigned long lastReadTime = 0;
unsigned char readInterval = 100;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT);
// set advertised local name and service UUID
blePeripheral.setLocalName("Button");
blePeripheral.setDeviceName("Button");
// Advertise AA80 so this looks like a TI CC2650 Sensor Tag
blePeripheral.setAdvertisedServiceUuid("AA80");
// add service and characteristics
blePeripheral.addAttribute(buttonService);
blePeripheral.addAttribute(buttonCharacteristic);
blePeripheral.addAttribute(buttonDescriptor);
// begin initialization
blePeripheral.begin();
Serial.println(F("Bluetooth Button"));
}
void loop() {
// Tell the bluetooth radio to do whatever it should be working on
blePeripheral.poll();
// limit how often we read the button
if (millis() - lastReadTime > readInterval) {
readButton();
lastReadTime = millis();
}
}
void readButton() {
char buttonValue = digitalRead(BUTTON_PIN);
// has the value changed since the last read?
if (buttonCharacteristic.value() != buttonValue) {
Serial.print("Button ");
Serial.println(buttonValue, HEX);
buttonCharacteristic.setValue(buttonValue);
}
}