Advertisement
Armandur

Arduino v1

Dec 10th, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. //  VARIABLES
  2. int pulsePin = 0;                 // Pulse Sensor purple wire connected to analog pin 0
  3. int blinkPin = 13;                // pin to blink led at each beat
  4. int fadePin = 6;                  // pin to do fancy classy fading blink at each beat
  5. int fadeRate = 10;                 // used to fade LED on with PWM on fadePin
  6. int alarmPin = 11;
  7.  
  8. // these variables are volatile because they are used during the interrupt service routine!
  9. volatile int BPM;                   // used to hold the pulse rate
  10. volatile int Signal;                // holds the incoming raw data
  11. volatile int IBI = 600;             // holds the time between beats, the Inter-Beat Interval
  12. volatile boolean Pulse = false;     // true when pulse wave is high, false when it's low
  13. volatile boolean QS = false;        // becomes true when Arduoino finds a beat.
  14.  
  15. boolean alarm = false;
  16. int bpmCount = 0;
  17. int limit = 80;
  18.  
  19. int sample[10];
  20. int numSamples = 9; //10st
  21.  
  22. double firstAverage = 0;
  23. double secondAverage = 0;
  24.  
  25. boolean tripped = false;
  26.  
  27. void setup()
  28. {
  29.   pinMode(blinkPin, OUTPUT);         // pin that will blink to your heartbeat!
  30.   pinMode(fadePin, OUTPUT);          // pin that will fade to your heartbeat!
  31.   pinMode(alarmPin, OUTPUT);
  32.   Serial.begin(115200);             // we agree to talk fast!
  33.   interruptSetup();                 // sets up to read Pulse Sensor signal every 2mS
  34.    // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING The Pulse Sensor AT LOW VOLTAGE,
  35.    // AND APPLY THAT VOLTAGE TO THE A-REF PIN
  36.    //analogReference(EXTERNAL);  
  37. }
  38.  
  39. void loop()
  40. {
  41.   sendDataToProcessing('S', Signal); // send Processing the raw Pulse Sensor data
  42.   if (QS == true)                    // Quantified Self flag is true when arduino finds a heartbeat
  43.   {
  44.     fadeRate = 255;                  // Set 'fadeRate' Variable to 255 to fade LED with pulse
  45.     sendDataToProcessing('B',BPM);   // send heart rate with a 'B' prefix
  46.     sendDataToProcessing('Q',IBI);   // send time between beats with a 'Q' prefix
  47.     QS = false;                      // reset the Quantified Self flag for next time
  48.    
  49.     sample[bpmCount] = BPM;
  50.    
  51.     bpmCount += 1;
  52.    
  53.     if(bpmCount == numSamples)
  54.     {
  55.         firstAverage = getAverageBpm();
  56.         tripped = true;
  57.         bpmCount = 0;
  58.     }
  59.    
  60.     if(tripped)
  61.     {
  62.         secondAverage = getAverageBpm();
  63.        
  64.         if(firstAverage - secondAverage > 10)
  65.         {
  66.             alarm = true;
  67.         }
  68.         else
  69.         {
  70.             alarm = false;
  71.         }
  72.        
  73.         tripped = false;
  74.     }
  75.    
  76.     Serial.print("count: ");
  77.     Serial.println(bpmCount);
  78.   }
  79.  
  80.   if(alarm)
  81.   {
  82.     analogWrite(alarmPin, 255); //Sätt igång alarmet
  83.   }
  84.   else
  85.   {
  86.     analogWrite(alarmPin, 0); //Stäng av alarmet
  87.   }
  88.  
  89.   ledFadeToBeat(); //Fada led:en på lådan
  90.  
  91.   delay(20);
  92. }
  93.  
  94. void ledFadeToBeat()
  95. {
  96.     fadeRate -= 15;                         //  set LED fade value
  97.     fadeRate = constrain(fadeRate,0,255);   //  keep LED fade value from going into negative numbers!
  98.     analogWrite(fadePin,fadeRate);          //  fade LED
  99. }
  100.  
  101. void sendDataToProcessing(char symbol, int data)
  102. {
  103.     Serial.print(symbol);                // symbol prefix tells Processing what type of data is coming
  104.     Serial.println(data);                // the data to send culminating in a carriage return
  105. }
  106.  
  107. double getAverageBpm()
  108. {
  109.     int sum = 0;
  110.     for(int i = 0; i < 10; i++)
  111.     {
  112.         sum += sample[i];
  113.     }
  114.    
  115.     return (double)sum / numSamples+1;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement