1+ /*
2+ This example will test functionality of PulseSensor user functions.
3+ Program any hardware with this sketch and open a serial port
4+ to view the test instructions, operation and results.
5+ Functionality that is explicity tested by the program:
6+ pause()
7+ resume()
8+ getInterBeatIntervalMs()
9+ getBeatsPerMinute()
10+ getLatestSample()
11+ getPulseAmplitude()
12+ getLastBeatTime()
13+ All other functionality is implicitly tested by successfully running the program.
14+
15+
16+
17+ Check out the PulseSensor Playground Tools for explaination
18+ of all user functions and directives.
19+ https://github.com/WorldFamousElectronics/PulseSensorPlayground/blob/master/resources/PulseSensor%20Playground%20Tools.md
20+
21+ Copyright World Famous Electronics LLC - see LICENSE
22+ Contributors:
23+ Joel Murphy, https://pulsesensor.com
24+ Yury Gitman, https://pulsesensor.com
25+ Bradford Needham, @bneedhamia, https://bluepapertech.com
26+
27+ Licensed under the MIT License, a copy of which
28+ should have been included with this software.
29+
30+ This software is not intended for medical use.
31+ */
32+
33+ /*
34+ Test notes here
35+ */
36+
37+ #include < PulseSensorPlayground.h>
38+
39+ // Test variables
40+ #define TEST_DURATION 10000 // run time in milliseconds
41+ unsigned long thisTime;
42+ bool testing = false ;
43+ bool normal = false ;
44+ uint8_t errorCode = 0x00 ; // maybe used for anything automatic?
45+ int testBPM, testIBI, testAmp, testLastBeatTime; // test variables
46+ int beatCounter;
47+ int firstBeatTime, lastBeatTime, firstToLastBeatTime;
48+
49+ // Standard PulseSensor Stuff
50+ const int OUTPUT_TYPE = SERIAL_PLOTTER;
51+ const int PULSE_INPUT = A0;
52+ const int PULSE_BLINK = LED_BUILTIN;
53+ const int PULSE_FADE = 5 ;
54+ const int THRESHOLD = 550 ; // Adjust this number to avoid noise when idle
55+
56+ PulseSensorPlayground pulseSensor;
57+
58+ void setup () {
59+
60+ Serial.begin (115200 );
61+ pulseSensor.analogInput (PULSE_INPUT);
62+ pulseSensor.blinkOnPulse (PULSE_BLINK);
63+ pulseSensor.fadeOnPulse (PULSE_FADE);
64+
65+ pulseSensor.setSerial (Serial);
66+ pulseSensor.setOutputType (OUTPUT_TYPE);
67+ pulseSensor.setThreshold (THRESHOLD);
68+
69+ // Now that everything is ready, start reading the PulseSensor signal.
70+ if (!pulseSensor.begin ()) {
71+ /*
72+ PulseSensor initialization failed,
73+ likely because our particular Arduino platform interrupts
74+ aren't supported yet.
75+
76+ If your Sketch hangs here, try PulseSensor_BPM_Alternative.ino,
77+ which doesn't use interrupts.
78+ */
79+ for (;;) {
80+ // Flash the led to show things didn't work.
81+ digitalWrite (PULSE_BLINK, LOW);
82+ delay (50 ); Serial.println (' !' );
83+ digitalWrite (PULSE_BLINK, HIGH);
84+ delay (50 );
85+ }
86+ }
87+ pulseSensor.pause ();
88+ delay (100 );
89+ printInstructions ();
90+
91+ }
92+
93+ void loop () {
94+ if (testing){
95+ runTest (millis ());
96+ }
97+ if (normal){
98+ runNormal ();
99+ }
100+ checkSerial ();
101+ } // loop
102+
103+
104+ /*
105+ Receives millis() and runs a test for TEST_DURATION
106+ */
107+ void runTest (unsigned long startTime){
108+ beatCounter = 0 ; // reset the beat counter
109+ testIBI = 0 ;
110+ testBPM = 0 ;
111+ testAmp = 0 ;
112+ firstBeatTime = -1 ;
113+ lastBeatTime = -1 ;
114+ Serial.println (" \n\t START TEST" );
115+ pulseSensor.resume (); // start the sensing!
116+ while ((millis () - startTime) < TEST_DURATION){
117+ Serial.println (pulseSensor.getLatestSample ()); // print raw data for plotter or monitor review
118+ if (pulseSensor.sawStartOfBeat ()){
119+ beatCounter++;
120+ if (firstBeatTime < 0 ){ firstBeatTime = pulseSensor.getLastBeatTime (); }
121+ testBPM += pulseSensor.getBeatsPerMinute ();
122+ testIBI += pulseSensor.getInterBeatIntervalMs ();
123+ testAmp += pulseSensor.getPulseAmplitude ();
124+ }
125+ delay (20 );
126+ }
127+ lastBeatTime = pulseSensor.getLastBeatTime ();
128+ pulseSensor.pause ();
129+ testBPM /= beatCounter;
130+ testIBI /= beatCounter;
131+ testAmp /= beatCounter;
132+ firstToLastBeatTime = lastBeatTime - firstBeatTime;
133+
134+ printResults ();
135+ printInstructions ();
136+ testing = false ;
137+ }
138+
139+
140+ void runNormal (){
141+ if (pulseSensor.UsingHardwareTimer ){
142+ delay (20 );
143+ pulseSensor.outputSample ();
144+ } else {
145+ if (pulseSensor.sawNewSample ()) {
146+ if (--pulseSensor.samplesUntilReport == (byte) 0 ) {
147+ pulseSensor.samplesUntilReport = SAMPLES_PER_SERIAL_SAMPLE;
148+ pulseSensor.outputSample ();
149+ }
150+ }
151+ }
152+ if (pulseSensor.sawStartOfBeat ()) {
153+ pulseSensor.outputBeat ();
154+ }
155+ }
0 commit comments