Overview
Finally, We integrate all the three sensors into a one working mini-wearable project.
Hardware Used
Software Used
Libraries Used
The libraries used will be the compilation of the previous libraries.
- SPI.h
- Wire.h
- PulseSensorPlayground
- Adafruit_GFX.h
- Adafruit_SSD1306.h
Application Description
To make this a mini-wearable project, we will switch from Uno R3 to a Nano. The components will work with 3.3V.
Set-up the Hardware
Code
#include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <PulseSensorPlayground.h> //PulseSensor #define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most accurate BPM math. //OLED Display #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); #if (SSD1306_LCDHEIGHT != 32) #error("Height incorrect, please fix Adafruit_SSD1306.h!"); #endif //Accelerometer #define DEVICE (0x53) //ADXL345 device address #define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis) #define offsetX -10.5 // OFFSET values #define offsetY -2.5 #define offsetZ -4.5 #define gainX 257.5 // GAIN factors #define gainY 254.5 #define gainZ 248.5 //Pulse Sensor Variables PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor" const int PulseWire = 0; // PulseSensor Analog WIRE connected to ANALOG PIN 0 int Threshold = 385; // Determine which Signal to "count as a beat" and which to ignore. // Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting. // Otherwise leave the default "550" value. //Accelerometer Variables byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device char str[512]; //string buffer to transform data before sending it to the serial port int x,y,z, stepsStepped; int xavg, yavg,zavg, steps=0, flag=0; int xval[15]={0}, yval[15]={0}, zval[15]={0}; int threshhold = 60.0; void setup() { Serial.begin(9600); Wire.begin(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) pulseSensor.analogInput(PulseWire); pulseSensor.setThreshold(Threshold); // init done //Turning on the ADXL345 writeTo(DEVICE, 0x2D, 0); writeTo(DEVICE, 0x2D, 16); writeTo(DEVICE, 0x2D, 8); if (pulseSensor.begin()) { Serial.println("Pulse Sensor Initialized..."); //This prints one time at Arduino power-up, or on Arduino reset. } // Show image buffer on the display hardware. // Since the buffer is intialized with an Adafruit splashscreen // internally, this will display the splashscreen. display.display(); delay(2000); // Clear the buffer. display.clearDisplay(); } void loop() { int regAddress = 0x32; //First axis-acceleration-data register on the ADXL345 readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345 int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int". // "myBPM" hold this BPM value now. //each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!! //thus we are converting both bytes in to one int x = (((int)buff[1]) << 8) | buff[0]; y = (((int)buff[3])<< 8) | buff[2]; z = (((int)buff[5]) << 8) | buff[4]; stepsStepped = ArduinoPedometer(); if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened". display.setTextSize(1.2); display.setTextColor(WHITE); display.setCursor(0,0); display.println("Your BPM is: " + String(myBPM) ); display.setCursor(0,10); display.println("You # Of steps are: " + String(stepsStepped)); display.display(); display.clearDisplay(); } delay(100); } //---------------- Functions //Writes val to address register on device void writeTo(int device, byte address, byte val) { Wire.beginTransmission(device); //start transmission to device Wire.write(address); // send register address Wire.write(val); // send value to write Wire.endTransmission(); //end transmission } //reads num bytes starting from address register on device in to buff array void readFrom(int device, byte address, int num, byte buff[]) { Wire.beginTransmission(device); //start transmission to device Wire.write(address); //sends address to read from Wire.endTransmission(); //end transmission Wire.beginTransmission(device); //start transmission to device Wire.requestFrom(device, num); // request 6 bytes from device int i = 0; while(Wire.available()) //device may send less than requested (abnormal) { buff[i] = Wire.read(); // receive a byte i++; } Wire.endTransmission(); //end transmission } //Get pedometer. int ArduinoPedometer(){ int acc=0; int totvect[15]={0}; int totave[15]={0}; int xaccl[15]={0}; int yaccl[15]={0}; int zaccl[15]={0}; for (int i=0;i<15;i++) { xaccl[i]= x; delay(1); yaccl[i]= y; delay(1); zaccl[i]= z; delay(1); totvect[i] = sqrt(((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)) + ((zval[i] - zavg)*(zval[i] - zavg))); totave[i] = (totvect[i] + totvect[i-1]) / 2 ; delay(150); //cal steps if (totave[i]>threshhold && flag==0) { steps=steps+1; flag=1; } else if (totave[i] > threshhold && flag==1) { //do nothing } if (totave[i] <threshhold && flag==1) { flag=0; } // Serial.print("steps="); // Serial.println(steps); return(steps); } delay(100); }
Conclusion
Reference
https://create.arduino.cc/projecthub?category=wearables-health-fitness&sort=updated
The post (4/4) BPM (Pulse Sensor) and Pedometer (ADXL345 Accelerometer) Wearable Device with OLED Display appeared first on CreateLabz.