Hello all, I am a ECE student trying to make an arduino project. Currently I am having trouble connecting with my device as it is stuck at connecting then disconnects after one cycle of the loop. I need help in verifying on what errors are found in my sketch.
/*
Remote Heartrate and Temperature Monitoring
Arduino Nano 33 BLE peripheral with a temperature and heartrate service that can be viewed
on a mobile phone.
*/
#include <ArduinoBLE.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h> //Temp Sensor
#include <Adafruit_GFX.h> // LCD Display
#include <Adafruit_SSD1306.h> // LCD Display
#include "MAX30105.h" //Heart Rate
#include "heartRate.h" //Heart Rate
MAX30105 particleSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
float temp_amb;
float temp_obj;
#define SERVICE_UUID "f97c2bec-e69e-4ddf-8e0d-b4e5d49ddf3e"
#define CHAR_UUIDtemp "7f3e5cc9-2056-4e57-8571-0ea17f8499ce"
#define CHAR_UUIDhr "935325f6-6179-4fa2-8d7f-8ac047345527"
static const char* greeting = "Health is wealth!";
BLEService customService(SERVICE_UUID); // User defined service
BLEStringCharacteristic tempCharacteristic(CHAR_UUIDtemp, BLERead | BLENotify, 20);
BLEStringCharacteristic hrCharacteristic(CHAR_UUIDhr, BLERead | BLENotify, 20);
float temp;
float heartrate;
void setup() {
Serial.begin(9600); // initialize serial communication
mlx.begin(); //Initialize MLX90614
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
Serial.println("Temperature Sensor MLX90614");
DisplayLCD(25, 5, 1, " Thermometer", 25, 35, 1 , "Initializing");
delay(1000);
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30102 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
//while (!Serial);
pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin
if (!BLE.begin()) { // initialize BLE
Serial.println("starting BLE failed!");
while (1);
}
BLE.setLocalName("Smart Bracelet"); // Set name for connection
BLE.setAdvertisedService(customService); // Advertise service
customService.addCharacteristic(tempCharacteristic); // Add characteristic to service
customService.addCharacteristic(hrCharacteristic); // Add characteristic to service
BLE.addService(customService); // Add service
tempCharacteristic.setValue(greeting); // Set greeting string
hrCharacteristic.setValue(greeting); // Set greeting string
BLE.advertise(); // Start advertising
Serial.print("Peripheral device MAC: ");
Serial.println(BLE.address());
Serial.println("Waiting for connections...");
}
void loop() {
BLEDevice central = BLE.central(); // Wait for a BLE central to connect
// if a central is connected to the peripheral:
if (central) {
Serial.print("Connected to central MAC: ");
Serial.println(central.address()); // print the central's BT address:
while (central.connected()){
digitalWrite(LED_BUILTIN, HIGH); // turn on the LED to indicate the connection:
temp = tempSensor(); //Read tempsensor data
heartrate = heartBPM(15000); //Read average bpm data
// Print a message when sending notifications
Serial.println("Sending notifications");
//tempCharacteristic.writeValue("Temp=" + String(temp) + "C, " + "BPM=" + String(heartrate));
tempCharacteristic.writeValue(String(temp));
hrCharacteristic.writeValue(String(heartrate));
} // keep looping while connected
// when the central disconnects, turn off the LED:
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central MAC: ");
Serial.println(central.address());
DisplayLCD(5, 5, 1, "HeartTempMonitor", 25, 35, 1, "Disconnected");
// Reinitialize BLE when disconnected
BLE.stopAdvertise();
BLE.begin();
BLE.advertise();
}
}
float tempSensor(){
//Reading room temperature and object temp
//for reading Fahrenheit values, use
//mlx.readAmbientTempF() , mlx.readObjectTempF() )
temp_amb = mlx.readAmbientTempC();
temp_obj = mlx.readObjectTempC();
//Serial Monitor
//Serial.print("Room Temp = ");
//Serial.println(temp_amb);
Serial.print("Temp = ");
Serial.println(temp_obj);
DisplayLCD(25, 5, 1, " Thermometer", 25, 35, 2, String(temp_obj) + String((char)247) + "C");
return temp_obj;
}
void DisplayLCD(int top1, int left1, int textsize1, String mesg1, int top2, int left2, int textsize2, String mesg2 ){
display.clearDisplay();
display.setCursor(top1,left1); //left = 25, top = 15
display.setTextSize(textsize1);
display.setTextColor(WHITE);
display.println(mesg1);
display.setCursor(top2,left2); //left = 25, top = 35
display.setTextSize(textsize2);
display.print(mesg2);
display.display();
}
float heartBPM(int delayT){
unsigned long prMillis = millis();
Serial.print("Measuring Heart Beat");
while(millis() - prMillis < delayT)
{
long irValue = particleSensor.getIR();
Serial.print(".");
if (checkForBeat(irValue) == true) {
//We sensed a beat!
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20) {
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}
}
Serial.println();
Serial.print("Avg BPM = ");
Serial.println(beatAvg);
DisplayLCD(25, 5, 1, "Avg BPM", 25, 35, 2, String(beatAvg) + "bpm");
delay(1500);
return beatAvg;
}
This is the current state of my arduino code blocks:
I havent done the other functions since I am having trouble connecting with the device.