Professional Documents
Culture Documents
long
long
long
long
heartbeatLimit;
lowerLimit = 60;
upperLimit = 200;
aPin = 2;
bPin = 3;
cPin = 4;
dPin = 5;
ePin = 6;
fPin = 7;
gPin = 8;
GND1 ;
GND2 = 9;
GND3 = 10;
GND4 = 11;
num;
dig1 = 0;
dig2 = 0;
dig3 = 0;
dig4 = 0;
DTime = 5;
//
//
//
//
//
//
void setup(){
pinMode(13,OUTPUT);
// pin 13 will blink to your heartbeat!
Serial.begin(115200); // we agree to talk fast!
// this next bit will wind up in the library. it initializes Timer1 to throw an interrupt every 1mS.
TCCR1A = 0x00; // DISABLE OUTPUTS AND BREAK 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 = 8000;
// TRIGGER TIMER INTERRUPT EVERY 1mS
sei();
// MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
pinMode(aPin, OUTPUT); // setting up the pins we declared earlier
pinMode(bPin, OUTPUT);
pinMode(cPin, OUTPUT);
pinMode(dPin, OUTPUT);
pinMode(ePin, OUTPUT);
pinMode(fPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(GND1, OUTPUT);
pinMode(GND2, OUTPUT);
pinMode(GND3, OUTPUT);
pinMode(GND4, OUTPUT);
}
void loop(){
// Serial.print("S");
// S tells processing that the following string is sensor data
// Serial.println(Signal);
// Signal holds the latest raw Pulse Sensor signal
if (B == true){
// B is true when arduino finds the heart beat
// Serial.print("BEAT ");
// 'B' tells Processing the following string is HRV data (time
between beats in mS)
// Serial.println(HRV );
// HRV holds the time between this pulse and the last pulse in mS
B = false;
// reseting the QS for next time
}
if (QS == true){
// QS is true when arduino derives the heart rate by averaging HRV over 20
beats
// Serial.print("BPM over 5 beats = ");
// 'QS' tells Processing that the following string is
heart rate data
/*
This portion of the code takes the BPM of the user, and then divides it into 3 different ints for the 7
seg display to read.
Otherwise, the 7 seg would not be able to function.
*/
num = BPM;
// dig1 = num / 1000;
// num = num - (dig1 * 1000);
dig1 = num / 100;
num = num - (dig1 * 100);
dig2 = num / 10;
dig3 = num - (dig2 *10);
// }
/*
This portion of the code makes eacch digit flash it's corresponding number on the 7 seg display. Notice
that digit 4 is commented out.
This is for 2 reasons. 1) Your BPM should not go over 999, so a fourth digit is not needed.
2) There are not enough pins on a arduino uno to support all 4 digits for this porject
Some of the methods used to display a digit (such as pickNumber()) are described at the bottom of the
code.
*/
/*
digitalWrite( GND4, LOW);
//digit 4
pickNumber(dig4);
delay(DTime);
digitalWrite( GND4, HIGH);
*/
digitalWrite( GND4, LOW);
//digit 3
pickNumber(dig3);
delay(DTime);
digitalWrite( GND4, HIGH);
digitalWrite( GND3, LOW);
//digit 2
pickNumber(dig2);
delay(DTime);
digitalWrite( GND3, HIGH);
digitalWrite( GND2, LOW);
pickNumber(dig1);
delay(DTime);
digitalWrite( GND2, HIGH);
}
//digit 1
// THIS IS THE TIMER 1 INTERRUPT SERVICE ROUTINE. IT WILL BE PUT INTO THE
LIBRARY
ISR(TIMER1_OVF_vect){ // triggered every time Timer 1 overflows
// Timer 1 makes sure that we take a reading every milisecond
Signal = analogRead(pulsePin);
// First normailize the waveform around 0
readings += Signal; // take a running total
sampleCounter++;
// we do this every milisecond. this timer is used as
a clock
if ((sampleCounter %300) == 0){
// adjust as needed
offset = readings / 300;
// average the running total
readings = 0;
// reset running total
}
NSignal = Signal - offset;
// normalizing here
// IF IT'S THE FIRST TIME THROUGH THE SKETCH, SEED THE FILTER WITH CURRENT
DATA
if (first = true){
for (int i=0; i<4; i++){
Lxv[i] = Lyv[i] = NSignal <<10; // seed the lowpass filter
Hxv[i] = Hyv[i] = NSignal <<10; // seed the highpass filter
}
first = false;
// only seed once please
}
beatCounter times when to calculate bpm by averaging the beat time values
if (beatCounter == 5){
// derive heart rate every 5 beats. adjust as needed
rate /= beatCounter;
// averaging time between beats
BPM = 60000/rate;
// how many beats can fit into a minute?
beatCounter = 0;
// reset counter
rate = 0;
// reset running total
QS = true;
// set Beat flag when BPM gets updated. NOT cleared inside this ISR
}
}
if (FSignal > 0 && Pulse == true){
// when the values are going up, it's the time between beats
digitalWrite(13,LOW);
// so turn off the pin 13 LED
Pulse = false;
// reset these variables so we can do it again!
Peak = 0;
//
}
}
heartbeatLimit = analogRead (A2);
potentiometer
heartbeatLimit = map(heartbeatLimit, 0, 1024, lowerLimit, upperLimit);
potentiometer,
limits set in the beginning of the code. Initially they are 60 and 200
if (BPM >= heartbeatLimit){
tone(12,500);
}else{
noTone(12);
}
A
B
C
D
E
F
G
void
// a method telling
six()
{
digitalWrite( aPin,
digitalWrite( bPin,
digitalWrite( cPin,
digitalWrite( dPin,
digitalWrite( ePin,
digitalWrite( fPin,
digitalWrite( gPin,
}
HIGH);
LOW);
HIGH);
HIGH);
HIGH);
HIGH);
HIGH);