SHARE
TWEET

Untitled

a guest May 26th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int sample;
  2. unsigned long time1;
  3. unsigned long time2;
  4. unsigned long peak;
  5. unsigned long lastpeak;
  6. unsigned long gap;
  7. unsigned long BPM = 0;
  8. int state = 1;
  9. void setup() {
  10.   Serial.begin(115200); // initialize the serial communication:
  11.   pinMode(A0, INPUT);
  12.   pinMode(6, OUTPUT);
  13.   pinMode(7, OUTPUT);
  14. }
  15. void loop() {
  16.   while (state == 1) {
  17.     while (analogRead(0) < 450) {
  18.       Serial.println(analogRead(0));
  19.       digitalWrite(7, HIGH);
  20.       digitalWrite(6, LOW);
  21.       delay(20);
  22.     }
  23.     state = 2;
  24.   }
  25.   while (state == 2) {
  26.     time1 = millis();
  27.     state = 3;
  28.   }
  29.   while (state == 3) {
  30.     while (analogRead(0) < 460) {
  31.       Serial.println(analogRead(0));
  32.       digitalWrite(7, HIGH);
  33.       digitalWrite(6, LOW);
  34.       delay(20);
  35.     }
  36.     state = 4;
  37.   }
  38.   while (state == 4) {
  39.     while (analogRead(0) > 450) {
  40.       Serial.println(analogRead(0));
  41.       digitalWrite(6, HIGH);
  42.       digitalWrite(7, LOW);
  43.       delay(20);
  44.     }
  45.     state = 5;
  46.   }
  47.   while (state == 5) {
  48.     time2 = millis();
  49.     peak = (time1 + time2) / 2;
  50.     gap = peak - lastpeak;
  51.     BPM = 60000 / gap;
  52.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53.  
  54.    
  55.     //This is where I would put in my moving average and code to display
  56.     //BPM is the variable name used for beats per minute
  57.     //The threshold value in this code is 450. You can change this value to suit your heart monitor.
  58.     //You may need to use a potentiometer to make the threshold a variable.
  59.     //If you do this then you need to make sure you are constantly reading in the analog value from the pot
  60.    
  61.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62.  
  63.     //UNCOMMENT THIS CODE IF YOU WANT TO PLOT THE HEART SIGNAL AND BPM AT THE SAME TIME
  64.     //Serial.print(analogRead(0));
  65.     //Serial.print("\t");
  66.     //Serial.println(BPM);
  67.     lastpeak = peak;
  68.     state = 6;
  69.   }
  70.   while (state == 6) {
  71.     while (analogRead(0) > 440) {
  72.       Serial.println(analogRead(0));
  73.       digitalWrite(6, HIGH);
  74.       digitalWrite(7, LOW);
  75.       delay(20);
  76.     }
  77.     state = 1;
  78.   }
  79. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top