Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed PulseSensor2 SCP1000 mbed-rtos 4DGL-uLCD-SE LSM9DS1_Library_cal PinDetect FatFileSystemCpp GP-20U7
Diff: main.cpp
- Revision:
- 24:841ceaf2cf69
- Parent:
- 23:1971e5cb6e3f
- Child:
- 25:41ec16a87ebd
--- a/main.cpp Fri Apr 17 23:01:59 2020 +0000
+++ b/main.cpp Sat Apr 18 17:54:15 2020 +0000
@@ -6,46 +6,54 @@
#include "PulseSensor.h"
#include "PinDetect.h"
#include "uLCD_4DGL.h"
-
+#include "LL.h"
+
SCP1000 scp1000(p5,p6,p7,p8);
LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
-PulseSensor PPG(p20);
-uLCD_4DGL uLCD(p28,p27,p21);
+PulseSensor PPG(p17);
+uLCD_4DGL uLCD(p28,p27,p29);
Serial pc(USBTX, USBRX);
-DigitalOut myled = LED1;
-DigitalOut led2(LED2);
-AnalogIn pot(p19);
-PinDetect pb(p29);
-
+DigitalOut one = LED1;
+DigitalOut two = LED2;
+DigitalOut three = LED3;
+DigitalOut four = LED4;
+AnalogIn pot(p20);
+PinDetect pb(p21);
+
#define FSNAME "msc"
MSCFileSystem msc(FSNAME);
-
+
volatile int bpm;
volatile int steps = 0;
volatile int flights = 0;
volatile float distance = 0.0;
volatile int calories = 0;
-//char stat_string[3];
-
+volatile unsigned long pressure;
+
volatile int mode = 1;
volatile int oldMode = 1;
-
-Mutex mtx;
+
Thread thread1;
Thread thread2;
Thread thread3;
Thread thread4;
Thread thread5;
-
+
bool run = true;
-
+Mutex mtx;
+
// when the pushbotton is pressed the run flag is set to false and the main
// function while loop exits so that the data file can be closed
// so press the button when you're ready to be done collecting data
void button (void) {
run = false;
+ thread1.terminate();
+ thread2.terminate();
+ thread3.terminate();
+ thread4.terminate();
+ thread5.terminate();
}
-
+
// Reads the value of the potentiometer and averages over 3 readings to get rid
// of random spikes/zero values. Returns either a 1, 2 or 3 based on which 3rd
// of its range the potentiometer is in and which screen should be displayed
@@ -78,7 +86,7 @@
Thread::wait(200);
}
}
-
+
//Display the time on the top
void display_time() {
while(1) {
@@ -95,7 +103,17 @@
Thread::wait(500);
}
}
-
+
+/*
+//Make sure program still alive
+void blink_led() {
+ while(1) {
+ led2 = !led2;
+ Thread::wait(1000);
+ }
+}
+*/
+
void update_screen(void) {
while(1) {
mtx.lock();
@@ -103,21 +121,22 @@
switch(mode) {
case 1:
//Step count
- /*
uLCD.media_init();
- uLCD.set_sector_address(0x0000, 0x0000);
- uLCD.display_image(0,0);
- */
+ uLCD.set_sector_address(0x0000, 0x0005);
+ uLCD.display_image(50, 45);
uLCD.filled_rectangle(10, 110, 118, 115, BLACK);
uLCD.locate(3, 11);
uLCD.text_height(1);
uLCD.text_width(1);
uLCD.color(WHITE);
uLCD.printf("%4d steps",steps);
- uLCD.filled_rectangle(10, 110, 118, 115, WHITE);
+ //uLCD.filled_rectangle(10, 110, 118, 115, WHITE);
break;
case 2:
// Heart rate
+ uLCD.media_init();
+ uLCD.set_sector_address(0x0000, 0x000A);
+ uLCD.display_image(50, 45);
uLCD.locate(5, 11);
uLCD.text_height(1);
uLCD.text_width(1);
@@ -126,6 +145,9 @@
break;
case 3:
//Distance
+ uLCD.media_init();
+ uLCD.set_sector_address(0x0000, 0x000F);
+ uLCD.display_image(50, 45);
uLCD.locate(6, 11);
uLCD.text_height(1);
uLCD.text_width(1);
@@ -134,6 +156,9 @@
break;
case 4:
//Calories
+ uLCD.media_init();
+ uLCD.set_sector_address(0x0000, 0x0000);
+ uLCD.display_image(50, 45);
uLCD.locate(4, 11);
uLCD.text_height(1);
uLCD.text_width(1);
@@ -142,6 +167,9 @@
break;
case 5:
//Floors
+ uLCD.media_init();
+ uLCD.set_sector_address(0x0000, 0x0014);
+ uLCD.display_image(50, 45);
uLCD.locate(4, 11);
uLCD.text_height(1);
uLCD.text_width(1);
@@ -153,7 +181,7 @@
Thread::wait(100);
}
}
-
+
//Read heart rate sensor, only want to read it if in heart rate mode
void read_heartRate() {
while(mode == 2 && run) {
@@ -161,13 +189,20 @@
Thread::wait(400);
}
}
-
+
//Read barometer and count floors
void read_barometer() {
- unsigned long pressure;
+ unsigned long pressure_buff[10] = {0};
while(run) {
pressure = scp1000.readPressure();
//Floor counting algo here
+ /*if(pressure != 0 && pressure_buff[0] != 0 && pressure - pressure_buff[0] > 100) {
+ flights++;
+ }
+ for(int i = 0; i < (sizeof pressure_buff) - 1; i++) {
+ pressure_buff[i] = pressure_buff[i + 1];
+ }
+ pressure_buff[9] = pressure;*/
Thread::wait(200);
}
}
@@ -187,9 +222,9 @@
set_time(1256729737);
// LED indicates whether or not data is being collected
- myled = 0;
+ one = 0;
// Start sensors
- int sample_num = 0;
+ int sample_num = 1;
PPG.start();
IMU.begin();
IMU.calibrate(1);
@@ -200,13 +235,13 @@
float buffer[2] = {0};
float avg_buffer[2] = {0};
float avg;
-
+
// Initialize data file on usb flash drive
FILE *fp = fopen( "/msc/data.txt", "w");
if(fp == NULL) {
error("Could not open file for write\n");
}
- fprintf(fp, "sample number, pressure (Pa), ax (Gs), ay (Gs), az (Gs), lowpassed magnitude (Gs), heart rate (bpm)\n");
+ fprintf(fp, "Sample Number, Pressure (Pa), Acceleration Magnitude (Gs), Heart Rate (bpm)\n");
//Start threads
thread1.start(update_screen);
@@ -233,20 +268,24 @@
// current point and 2 points back) and was greater than the threshold
// value of 1.05
if(sample_num > 1) {
- if((avg_buffer[1] > avg_buffer[0]) && (avg < avg_buffer[1]) && (avg_buffer[1] > 1.05)) {
+ float dif1 = avg_buffer[1] - avg_buffer[0];
+ float dif2 = avg_buffer[1] - avg;
+ float peak_prominence = 0.03;
+ if(dif1 > peak_prominence && dif2 > peak_prominence) {
steps++;
}
}
avg_buffer[0] = avg_buffer[1];
avg_buffer[1] = avg;
+
// Save the data to the usb flash drive and print to the terminal
- fprintf(fp, "%d, %lu, %f, %f, %f, %f, %d\r\n", sample_num, pressure, ax, ay, az, avg, bpm);
- pc.printf("%d, %lu, %f, %f, %f, %f, %d\r\n", sample_num, pressure, ax, ay, az, avg, bpm);
+ fprintf(fp, "%d, %lu, %f, %d\r\n", sample_num, pressure, avg, bpm);
+ pc.printf("%d, %lu, %f, %d\r\n", sample_num, pressure, avg, bpm);
sample_num++;
- myled = 1;
+ one = !one;
// Sampling rate of ~200 Hz
Thread::wait(50);
}
fclose(fp);
- myled = 0;
+ one = 0;
}
\ No newline at end of file

