Added Value - abner722/BUM-Fitness-Pal GitHub Wiki
##Project Code:
#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
#define speakerInput 8
#define muscleSensorIn A1
// Variables
int blinkPin = 13;
int fadePin = 5;
int pulsePin = 0;
int fadeRate = 0;
const int numReadings = 50;
int analogReadings[numReadings];
double total = 0;
double average = 0;
int readIndex = 0;
Adafruit_MMA8451 mma = Adafruit_MMA8451();
double xval = 0.0;
double yval = 0.0;
double zval = 0.0;
double xval2 = 0.0;
double yval2 = 0.0;
double zval2 = 0.0;
int steps = 0;
double x_diff = 0;
double y_diff = 0;
// Volatile Variables, used in the interrupt service routine!
volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
volatile int Signal; // holds the incoming raw data
volatile int IBI = 600; // int that holds the time interval between beats! Must be seeded!
volatile boolean Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".
volatile boolean QS = false; // becomes true when Arduoino finds a beat.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Regards Serial OutPut -- Set This Up to your needs
static boolean serialVisual = true; // Set to 'false' by Default. Re-set to 'true' to see Arduino Serial Monitor
void setup(){
pinMode(blinkPin,OUTPUT); // pin that will blink to your heartbeat!
pinMode(fadePin,OUTPUT); // pin that will fade to your heartbeat!
Serial.begin(9600); // we agree to talk fast! //115200
interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
// IF YOU ARE POWERING The Pulse Sensor AT VOLTAGE LESS THAN THE BOARD VOLTAGE,
// UN-COMMENT THE NEXT LINE AND APPLY THAT VOLTAGE TO THE A-REF PIN
// analogReference(EXTERNAL);
for(int thisReading = 0; thisReading < numReadings; thisReading++){
analogReadings[thisReading] = 0;
}
lcd.begin(16,2);
lcd.print("Steps: ");
lcd.setCursor(0,1);
lcd.print("Heartrate: ");
//lcd.setCursor(1, 13);
//lcd.print("BPM");
pinMode(speakerInput, OUTPUT);
pinMode(muscleSensorIn, INPUT);
Serial.println("Adafruit MMA8451 test!");
if (! mma.begin()) {
Serial.println("Couldnt start");
while (1);
}
Serial.println("MMA8451 found!");
mma.setRange(MMA8451_RANGE_2_G);
//lcd.print(BPM); was using the hello world lcd display example code
}
// Where the Magic Happens
void loop(){
serialOutput() ;
if (QS == true){ // A Heartbeat Was Found
// BPM and IBI have been Determined
// Quantified Self "QS" true when arduino finds a heartbeat
//fadeRate = 255; // Makes the LED Fade Effect Happen
// Set 'fadeRate' Variable to 255 to fade LED with pulse
serialOutputWhenBeatHappens(); // A Beat Happened, Output that to serial.
QS = false; // reset the Quantified Self flag for next time
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mma.read();
sensors_event_t event;
sensors_event_t event2;
mma.getEvent(&event);
mma.getEvent(&event2);
xval = event.acceleration.x;
yval = event.acceleration.y;
zval = event.acceleration.z;
//This code was used to keep track of the steps the user has taken
xval2 = event2.acceleration.x; //Two acceleration events were tracked, and then the difference of the acceleration
yval2 = event2.acceleration.y; // was calculated. This was used as a threshold for our steps tracker.
zval2 = event2.acceleration.z;
x_diff = xval2 - xval;
x_diff = abs(x_diff);
y_diff = yval2 - yval;
y_diff = abs(y_diff);
if(y_diff > 0.75){
steps = steps + 1;
}
if(x_diff > 0.75){
steps = steps + 1;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//************************************************************
total = total - analogReadings[readIndex];
analogReadings[readIndex] = analogRead(muscleSensorIn);
total = total + analogReadings[readIndex];
readIndex = readIndex + 1;
if(readIndex >= numReadings){
readIndex = 0;
}
average = 5*(total/numReadings)/1023; //This block of code is used to smoothe out the analog
// signal received from the muscle sensor
Serial.println(average);
delay(20);
if(average > 2.5){ //The average is converted to a voltage rating of 5V and if the average was above above 2.5, then the speaker will play music
digitalWrite(speakerInput, LOW);
}
else {
digitalWrite(speakerInput, HIGH);
}
//*************************************************************
lcd.setCursor(7, 0);
lcd.print(steps);
lcd.setCursor(10, 1); //Printing data to the LCD
lcd.print(BPM);
delay(200);
}