peripheral BLE - lamp
/*
* Haunted [Smart] House - RME Spring 2023 Final Project
* peripheral BLE
* control lamp
*/
#include <ArduinoBLE.h>
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
enum {
NEUTRAL = 0,
SHOCKED = 1,
};
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1111";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1111";
int shock = 0;
BLEService shockService(deviceServiceUuid);
BLEByteCharacteristic shockCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
int relayPin = D2;
int controlFlag = 0;
int helpFlag = 0;
void setup() {
Serial1.begin(9600);
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
if (!BLE.begin()) {
Serial.println("- Starting Bluetooth® Low Energy module failed!");
while (1)
;
}
BLE.setLocalName("Arduino Nano 33 BLE (Peripheral)");
BLE.setAdvertisedService(shockService);
shockService.addCharacteristic(shockCharacteristic);
BLE.addService(shockService);
shockCharacteristic.writeValue(-1);
BLE.advertise();
Serial.println("Nano 33 BLE (Peripheral Device)");
Serial.println(" ");
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(Serial1)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true) {
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(10); //Set volume value. From 0 to 30
}
void loop() {
BLEDevice central = BLE.central();
// Serial.println("- Discovering central device...");
delay(500);
if (central) {
Serial.println("* Connected to central device!");
Serial.print("* Device MAC address: ");
Serial.println(central.address());
Serial.println(" ");
controlEffect();
while (central.connected()) {
if (shockCharacteristic.written()) {
shock = shockCharacteristic.value();
Serial.println("in the loop");
Serial.println(shock);
writeShock(shock);
}
}
shock = NEUTRAL;
writeShock(shock);
thanksEffect();
Serial.println("* Disconnected to central device!");
}
}
void writeShock(int shock) {
Serial.println("- Characteristic <shock_type> has changed!");
switch (shock) {
case NEUTRAL:
Serial.println("* Actual value: NEUTRAL (red LED on)");
Serial.println(" ");
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
break;
case SHOCKED:
Serial.println("* Actual value: SHOCK (green LED on)");
Serial.println(" ");
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
helpEffect();
break;
default:
Serial.println("* Actual value: NEUTRAL (red LED on)");
Serial.println(" ");
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
break;
}
}
void controlEffect() {
Serial.println("control false"); // Switch Relay Off (NC)
digitalWrite(relayPin, LOW);
delay(500);
Serial.println("control true"); // Switch Relay On (NO)
digitalWrite(relayPin, HIGH);
int delayTime = delayTime = random(6000, 30000);
myDFPlayer.play(1); //Play the first mp3
delay(delayTime);
}
void helpEffect() {
int helpFlag = random(0, 2);
int delayTime = 0;
if (helpFlag == 1) {
int randomTime = random(1, 3);
for (int i = 0; i < randomTime; randomTime++) {
Serial.println("help false"); // Switch Relay Off (NC)
delayTime = random(50, 500);
digitalWrite(relayPin, LOW);
delay(delayTime);
delayTime = random(50, 500);
Serial.println("help true"); // Switch Relay On (NO)
digitalWrite(relayPin, HIGH);
delay(delayTime);
}
myDFPlayer.next(); //Play the first mp3
delayTime = random(6000, 15000);
delay(delayTime);
}
}
void thanksEffect() {
myDFPlayer.play(2); //Play the first mp3
}
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. .