
PROCESS
PROCESS
Arduino
main.ino
// Variables
int pulsePin = 0; // pilse sensor connects to A0
int fadePin = 5; // pin to do fading blink at each beat
int fadeRate = 0; // used to fade LED on with PWM on fadePin
int speaker = 8; // speaker in pin8
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.
volatile boolean QS = false; // becomes true when Arduoino finds a beat.
static boolean serialVisual = false; //Re-set to 'true' to see Arduino Serial Monitor ASCII Visual Pulse
int switchPin1 = 2; // Switch connected to pin 2 and 3 respectively
int switchPin2 = 3;
void setup() {
pinMode(fadePin, OUTPUT); // pin that will fade according to heartbeat
Serial.begin(115200);
interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
}
void loop() {
/*------reference from http://pulsesensor.com/------*/
digitalWrite(5, HIGH);
if (QS == true) { // if a Heartbeat was found
fadeRate = 255; // 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
sendDataToProcessing('B', BPM); // send heart rate with a 'B' prefix
sendDataToProcessing('Q', IBI); // send time between beats with a 'Q' prefix
tone(speaker, 100); //set the tone of speaker
}
ledFadeToBeat(); // LED Fade Effect Happen
delay(20); // take a break
if (digitalRead(switchPin1) == HIGH) { //if switchPin1 is ON,
Serial.println("c"); //send c to Processing to recognize switchPin1 is pressed
} else {
Serial.println("w"); // send a to Processing ro recognize switchPin1 is NOT pressed
}
if (digitalRead(switchPin2) == HIGH) { // If the switchPin2 is ON,
Serial.println("v"); //send v to Processing to recognize switchPin2 is pressed
} else {
Serial.println("q"); // send q to Processing to recognize switchPin2 is NOT pressed
}
}
void ledFadeToBeat() {
fadeRate -= 15; // set LED fade value
fadeRate = constrain(fadeRate, 0, 255); // keep LED fade value from going into -ve number
if (fadeRate == 0) {
noTone(speaker);
}
analogWrite(fadePin, fadeRate); // fade LED
}
void sendDataToProcessing(char symbol, int data ) {
Serial.print(symbol); // symbol prefix tells Processing what type of data is coming
Serial.println(data); // the data to send culminating in a carriage return
}
AllSerialHandling.ino
/*------reference from http://pulsesensor.com/------*/
///////// Set it to 'true' or 'false' when it's declared at start of code.
void serialOutput() { // Decide How To Output Serial.
if (serialVisual == true) {
arduinoSerialMonitorVisual('-', Signal); // goes to function that makes Serial Monitor Visualizer
} else {
sendDataToSerial('S', Signal); // goes to sendDataToSerial function
}
}
// Decides How To OutPut BPM and IBI Data
void serialOutputWhenBeatHappens() {
if (serialVisual == true) { // Code to Make the Serial Monitor Visualizer Work (for debug usage)
Serial.print("*** Heart-Beat Happened *** "); //ASCII
Serial.print("BPM: ");
Serial.print(BPM);
Serial.print(" ");
} else {
sendDataToSerial('B', BPM); // send heart rate with a 'B' prefix
sendDataToSerial('Q', IBI); // send time between beats with a 'Q' prefix
}
}
// Sends Data to Pulse Sensor Processing
void sendDataToSerial(char symbol, int data ) {
Serial.print(symbol);
Serial.println(data);
}
// Code to Make the Serial Monitor Visualizer Work
void arduinoSerialMonitorVisual(char symbol, int data ) {
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 1024; // sensor maximum, discovered through experiment
int sensorReading = data;
// map the sensor range to a range of 12 options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 11);
// do something different depending on the
// range value:
switch (range) {
case 0:
Serial.println(""); /////ASCII Art Madness
break;
case 1:
Serial.println("---");
break;
case 2:
Serial.println("------");
break;
case 3:
Serial.println("---------");
break;
case 4:
Serial.println("------------");
break;
case 5:
Serial.println("--------------|-");
break;
case 6:
Serial.println("--------------|---");
break;
case 7:
Serial.println("--------------|-------");
break;
case 8:
Serial.println("--------------|----------");
break;
case 9:
Serial.println("--------------|----------------");
break;
case 10:
Serial.println("--------------|-------------------");
break;
case 11:
Serial.println("--------------|-----------------------");
break;
}
}
Interrupt.ino (use for the speaker)
/*------reference from http://pulsesensor.com/------*/
//for speaker
volatile int rate[10]; // array to hold last ten IBI values
volatile unsigned long sampleCounter = 0; // used to determine pulse timing
volatile unsigned long lastBeatTime = 0; // used to find IBI
volatile int P = 512; // used to find peak in pulse wave, seeded
volatile int T = 512; // used to find trough in pulse wave, seeded
volatile int thresh = 512; // used to find instant moment of heart beat, seeded
volatile int amp = 100; // used to hold amplitude of pulse waveform, seeded
volatile boolean firstBeat = true; // used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = false; // used to seed rate array so we startup with reasonable BPM
void interruptSetup() {
// Initializes Timer1 to throw an interrupt every 2mS.
TCCR1A = 0x00; // DISABLE OUTPUTS AND PWM ON DIGITAL PINS 9 & 10
TCCR1B = 0x11; // GO INTO 'PHASE AND FREQUENCY CORRECT' MODE, NO PRESCALER
TCCR1C = 0x00; // DON'T FORCE COMPARE
TIMSK1 = 0x01; // ENABLE OVERFLOW INTERRUPT (TOIE1)
ICR1 = 16000; // TRIGGER TIMER INTERRUPT EVERY 2mS
sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
}
// makes sure reading will take every 2 miliseconds
ISR(TIMER1_OVF_vect) { // triggered when Timer2 counts to 124
cli(); // disable interrupts while we do this
Signal = analogRead(pulsePin); // read the Pulse Sensor
sampleCounter += 2; // keep track of the time in mS with this variable
int N = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise
// find the peak and trough of the pulse wave
if (Signal < thresh && N > (IBI / 5) * 3) { // avoid dichrotic noise by waiting 3/5 of last IBI
if (Signal < T) { // T is the trough
T = Signal; // keep track of lowest point in pulse wave
}
}
if (Signal > thresh && Signal > P) { // thresh condition helps avoid noise
P = Signal; // P is the peak
} // keep track of highest point in pulse wave
// signal surges up in value every time there is a pulse
if (N > 250) { // avoid high frequency noise
if ( (Signal > thresh) && (Pulse == false) && (N > (IBI / 5) * 3) ) {
Pulse = true; // set the Pulse flag when we think there is a pulse
IBI = sampleCounter - lastBeatTime; // measure time between beats in mS
lastBeatTime = sampleCounter; // keep track of time for next pulse
if (secondBeat) { // if this is the second beat
secondBeat = false; // clear secondBeat flag
for (int i = 0; i <= 9; i++) { // seed the running total to get a realisitic BPM at startup
rate[i] = IBI;
}
}
if (firstBeat) { // if it's the first time we found a beat, if firstBeat
firstBeat = false; // clear firstBeat flag
secondBeat = true; // set the second beat flag
sei(); // enable interrupts again
return; // discard IBI value
}
// keep a running total of the last 10 IBI values
word runningTotal = 0; // clear the runningTotal variable
for (int i = 0; i <= 8; i++) { // shift data in the rate array
rate[i] = rate[i + 1]; // and drop the oldest IBI value
runningTotal += rate[i]; // add up the 9 oldest IBI values
}
rate[9] = IBI; // add the latest IBI to the rate array
runningTotal += rate[9]; // add the latest IBI to runningTotal
runningTotal /= 10; // average the last 10 IBI values
BPM = 60000 / runningTotal; // how many beats can fit into a minute? that's BPM!
QS = true; // set Quantified Self flag
}
}
if (Signal < thresh && Pulse == true) { // when the values are going down, the beat is over
Pulse = false; // reset the Pulse flag so we can do it again
amp = P - T; // get amplitude of the pulse wave
thresh = amp / 2 + T; // set thresh at 50% of the amplitude
P = thresh; // reset these for next time
T = thresh;
}
if (N > 2500) { // if 2.5 seconds go by without a beat
thresh = 512; // set thresh default
P = 512; // set P default
T = 512; // set T default
lastBeatTime = sampleCounter; // bring the lastBeatTime up to date
firstBeat = true; // set these to avoid noise
secondBeat = false; // when we get the heartbeat back
}
sei(); // enable interrupts
}