Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int sample;
- unsigned long time1;
- unsigned long time2;
- unsigned long peak;
- unsigned long lastpeak;
- unsigned long gap;
- unsigned long BPM = 0;
- int state = 1;
- void setup() {
- Serial.begin(115200); // initialize the serial communication:
- pinMode(A0, INPUT);
- pinMode(6, OUTPUT);
- pinMode(7, OUTPUT);
- }
- void loop() {
- while (state == 1) {
- while (analogRead(0) < 450) {
- Serial.println(analogRead(0));
- digitalWrite(7, HIGH);
- digitalWrite(6, LOW);
- delay(20);
- }
- state = 2;
- }
- while (state == 2) {
- time1 = millis();
- state = 3;
- }
- while (state == 3) {
- while (analogRead(0) < 460) {
- Serial.println(analogRead(0));
- digitalWrite(7, HIGH);
- digitalWrite(6, LOW);
- delay(20);
- }
- state = 4;
- }
- while (state == 4) {
- while (analogRead(0) > 450) {
- Serial.println(analogRead(0));
- digitalWrite(6, HIGH);
- digitalWrite(7, LOW);
- delay(20);
- }
- state = 5;
- }
- while (state == 5) {
- time2 = millis();
- peak = (time1 + time2) / 2;
- gap = peak - lastpeak;
- BPM = 60000 / gap;
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //This is where I would put in my moving average and code to display
- //BPM is the variable name used for beats per minute
- //The threshold value in this code is 450. You can change this value to suit your heart monitor.
- //You may need to use a potentiometer to make the threshold a variable.
- //If you do this then you need to make sure you are constantly reading in the analog value from the pot
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //UNCOMMENT THIS CODE IF YOU WANT TO PLOT THE HEART SIGNAL AND BPM AT THE SAME TIME
- //Serial.print(analogRead(0));
- //Serial.print("\t");
- //Serial.println(BPM);
- lastpeak = peak;
- state = 6;
- }
- while (state == 6) {
- while (analogRead(0) > 440) {
- Serial.println(analogRead(0));
- digitalWrite(6, HIGH);
- digitalWrite(7, LOW);
- delay(20);
- }
- state = 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement