central BLE - led strip & lamp
/*
* Haunted [Smart] House - RME Spring 2023 Final Project
* central BLE
* control led strip and lamp
*/
#include <ArduinoBLE.h>
#include <Adafruit_NeoPixel.h>
#include <Arduino_APDS9960.h>
#define PIXEL_PIN D2
#define PIXEL_COUNT 60
#define PIXEL_TYPE NEO_GRB + NEO_KHZ800
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
int proximity = -1;
int shock = -1;
int oldShockValue = -1;
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
for(int i=0; i < 255; i++){
setRedLight(i);
delay( 100 );
}
Serial.begin(9600);
while (!Serial)
;
if (!APDS.begin()) {
Serial.println("Error initializing APDS9960 sensor!");
}
// set the LEDs pins as outputs
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
// turn all the LEDs off
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
// Bluetooth Connection
Serial.begin(9600);
while (!Serial)
;
if (!APDS.begin()) {
Serial.println("* Error initializing APDS9960 sensor!");
}
APDS.setGestureSensitivity(80);
if (!BLE.begin()) {
Serial.println("* Starting Bluetooth® Low Energy module failed!");
while (1)
;
}
BLE.setLocalName("Nano 33 BLE (Central)");
BLE.advertise();
Serial.println("Arduino Nano 33 BLE Sense (Central Device)");
Serial.println(" ");
}
void loop() {
connectToPeripheral();
}
void setRedLight(int brightness) {
uint32_t c = strip.Color(brightness, 0, 0);
for(int i=0; i < strip.numPixels(); i++){
strip.setPixelColor(i, c); // set a color
}
strip.show();
}
void connectToPeripheral() {
BLEDevice peripheral;
Serial.println("- Discovering peripheral device...");
do {
BLE.scanForUuid(deviceServiceUuid);
peripheral = BLE.available();
} while (!peripheral);
if (peripheral) {
Serial.println("* Peripheral device found!");
Serial.print("* Device MAC address: ");
Serial.println(peripheral.address());
Serial.print("* Device name: ");
Serial.println(peripheral.localName());
Serial.print("* Advertised service UUID: ");
Serial.println(peripheral.advertisedServiceUuid());
Serial.println(" ");
BLE.stopScan();
controlPeripheral(peripheral);
}
}
void controlPeripheral(BLEDevice peripheral) {
Serial.println("- Connecting to peripheral device...");
if (peripheral.connect()) {
Serial.println("* Connected to peripheral device!");
Serial.println(" ");
} else {
Serial.println("* Connection to peripheral device failed!");
Serial.println(" ");
return;
}
Serial.println("- Discovering peripheral device attributes...");
if (peripheral.discoverAttributes()) {
Serial.println("* Peripheral device attributes discovered!");
Serial.println(" ");
} else {
Serial.println("* Peripheral device attributes discovery failed!");
Serial.println(" ");
peripheral.disconnect();
return;
}
BLECharacteristic shockCharacteristic = peripheral.characteristic(deviceServiceCharacteristicUuid);
if (!shockCharacteristic) {
Serial.println("* Peripheral device does not have shock_type characteristic!");
peripheral.disconnect();
return;
} else if (!shockCharacteristic.canWrite()) {
Serial.println("* Peripheral does not have a writable shock_type characteristic!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
int flicker = random(0, 256);
setRedLight(flicker);
delay(random(10, 50));
// check if a proximity reading is available
shock = 1;
if (oldShockValue != shock) {
oldShockValue = shock;
Serial.print("* Writing value to shock_type characteristic: ");
Serial.println(shock);
shockCharacteristic.writeValue((byte)shock);
Serial.println("* Writing value to shock_type characteristic done!");
Serial.println(" ");
}
}
Serial.println("- Peripheral device disconnected!");
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .