Back to Parent

peripheral BLE - alarm clock
/*
 * Haunted [Smart] House - RME Spring 2023 Final Project
 * peripheral BLE
 * control alarm clock
 */

#include <ArduinoBLE.h>
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#include "TM1637.h"
#include <TimeLib.h>

enum {
  NEUTRAL = 0,
  SHOCKED = 1,
};

const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

int shock = 0;

BLEService shockService(deviceServiceUuid);
BLEByteCharacteristic shockCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);

#define CLK A6  //pins definitions for TM1637 and can be changed to other ports
#define DIO A7
TM1637 tm1637(CLK, DIO);

void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);

  setTime(13, 0, 0, 3, 5, 23);  // set time to Saturday 8:29:00am Jan 1 2011
  tm1637.init();
  tm1637.set(2);

  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(" ");
}

void loop() {
  BLEDevice central = BLE.central();
  Serial.println("- Discovering central device...");
  delay(500);

  displayTime();

  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();
        writeShock(shock);
      }
    }

    shock = NEUTRAL;
    writeShock(shock);

    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() {
  tm1637.clearDisplay();
  Serial.println("in control");
  displayHelp();
}

void helpEffect() {
  tm1637.clearDisplay();
  int delayTime = random(6000, 30000);
  displayTime();
  delay(delayTime);
  Serial.println("help!!!!");
  displayHelp();
  delay(1500);
}

void displayHelp() {
  tm1637.display(1, 5);
  tm1637.point(0);
  tm1637.display(2, 0);
  tm1637.display(3, 5);
}

void displayTime() {
  tm1637.display(0, hour() / 10);
  tm1637.display(1, hour() % 10);
  tm1637.point(1);
  tm1637.display(2, minute() / 10);
  tm1637.display(3, minute() % 10);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0