Skip to content

Instantly share code, notes, and snippets.

@dmelancon
Created February 21, 2015 20:52
Show Gist options
  • Save dmelancon/20cec5725943082d50cc to your computer and use it in GitHub Desktop.
Save dmelancon/20cec5725943082d50cc to your computer and use it in GitHub Desktop.
// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEPeripheral.h>
#include <TimerOne.h>
// define pins (varies per shield/board)
#define BLE_REQ 6
#define BLE_RDY 7
#define BLE_RST 4
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
BLEService potService = BLEService("fff0");
BLECharCharacteristic potCharacteristic = BLECharCharacteristic("fff1", BLERead | BLENotify);
BLEDescriptor potDescriptor = BLEDescriptor("2901", "pot");
BLEService buttonService = BLEService("fff3");
BLECharCharacteristic buttonCharacteristic = BLECharCharacteristic("fff4", BLEWrite );
BLEDescriptor buttonDescriptor = BLEDescriptor("2901", "btn");
volatile bool readFromSensor = false;
float lastReading;
const int buttonPin = 5;
int buttonState = 0;
void setup() {
Serial.begin(115200);
#if defined (__AVR_ATmega32U4__)
delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board
#endif
blePeripheral.setLocalName("Dan's BLE"); // optional
blePeripheral.setAdvertisedServiceUuid(potService.uuid()); // optional
blePeripheral.addAttribute(potService);
blePeripheral.addAttribute(potCharacteristic);
blePeripheral.addAttribute(potDescriptor);
blePeripheral.setAdvertisedServiceUuid(buttonService.uuid()); // optional
blePeripheral.addAttribute(buttonService);
blePeripheral.addAttribute(buttonCharacteristic);
blePeripheral.addAttribute(buttonDescriptor);
//eventHander for connect/disconnect events
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
buttonCharacteristic.setEventHandler(BLEWritten, buttonCharacteristicWritten);
// begin initialization
blePeripheral.begin();
Timer1.initialize(2 * 10000); // in milliseconds
Timer1.attachInterrupt(timerHandler);
pinMode(buttonPin, INPUT);
Serial.println(bleP
}
void loop() {
buttonState = digitalRead(buttonPin);
blePeripheral.poll();
if (readFromSensor) {
setPotCharacteristicValue();
readFromSensor = false;
}
//buttonCharacteristicValue(readFromSensor);
}
void timerHandler() {
readFromSensor = true;
}
void setPotCharacteristicValue() {
int reading = analogRead(A0);
if (!isnan(reading) && significantChange(lastReading, reading, 50.0)) {
potCharacteristic.setValue(reading/4);
//Serial.println(potCharacteristic.value());
Serial.print(F("Pot: ")); Serial.println(reading/4);
lastReading = reading;
}
}
boolean significantChange(float val1, float val2, float threshold) {
return (abs(val1 - val2) >= threshold);
}
void blePeripheralConnectHandler(BLECentral& central) {
Serial.print(F("Connected event, central: "));
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLECentral& central) {
Serial.print(F("Disconnected event, central: "));
Serial.println(central.address());
}
void buttonCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
// central wrote new value to characteristic, update LED
//Serial.println( sizeof(setCharacteristic.value()));
Serial.print(F("Characteristic event, writen: "));
Serial.println( buttonCharacteristic.value(), DEC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment