Skip to content

Commit 38ad4c6

Browse files
committed
Splitting getRed() from getFIFORed() for better examples. When you call getRed() you now get the immediate environmental reading.
1 parent c46feab commit 38ad4c6

File tree

11 files changed

+145
-407
lines changed

11 files changed

+145
-407
lines changed

Examples/Example1_Basic_Readings/Example1_Basic_Readings.ino

+13-27
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@
44
Date: October 2nd, 2016
55
https://github.com/sparkfun/MAX30105_Breakout
66
7-
Outputs all Red/IR/Green values at 25Hz.
7+
Outputs all Red/IR/Green values.
88
99
Hardware Connections (Breakoutboard to Arduino):
1010
-5V = 5V (3.3V is allowed)
1111
-GND = GND
1212
-SDA = A4 (or SDA)
1313
-SCL = A5 (or SCL)
1414
-INT = Not connected
15-
15+
1616
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V
1717
but it will also run at 3.3V.
18+
19+
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
1820
*/
1921

2022
#include <Wire.h>
2123
#include "MAX30105.h"
2224

2325
MAX30105 particleSensor;
2426

25-
long startTime;
26-
long samplesTaken = 0; //Counter for calculating the Hz or read rate
27-
2827
void setup()
2928
{
3029
Serial.begin(115200);
@@ -38,30 +37,17 @@ void setup()
3837
}
3938

4039
particleSensor.setup(); //Configure sensor. Use 6.4mA for LED drive
41-
42-
startTime = millis();
4340
}
4441

4542
void loop()
4643
{
47-
particleSensor.check(); //Check the sensor, read up to 3 samples
48-
49-
while (particleSensor.available()) //do we have new data?
50-
{
51-
samplesTaken++;
52-
53-
Serial.print(" R[");
54-
Serial.print(particleSensor.getRed());
55-
Serial.print("] IR[");
56-
Serial.print(particleSensor.getIR());
57-
Serial.print("] G[");
58-
Serial.print(particleSensor.getGreen());
59-
Serial.print("] Hz[");
60-
Serial.print((float)samplesTaken / ((millis() - startTime) / 1000.0), 2);
61-
Serial.print("]");
62-
63-
Serial.println();
64-
65-
particleSensor.nextSample(); //We're finished with this sample so move to next sample
66-
}
44+
Serial.print(" R[");
45+
Serial.print(particleSensor.getRed());
46+
Serial.print("] IR[");
47+
Serial.print(particleSensor.getIR());
48+
Serial.print("] G[");
49+
Serial.print(particleSensor.getGreen());
50+
Serial.print("]");
51+
52+
Serial.println();
6753
}

Examples/Example2_Presence_Sensing/Example2_Presence_Sensing.ino

+17-32
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
-SDA = A4 (or SDA)
1414
-SCL = A5 (or SCL)
1515
-INT = Not connected
16-
16+
1717
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V
1818
but it will also run at 3.3V.
1919
@@ -24,10 +24,9 @@
2424

2525
MAX30105 particleSensor;
2626

27-
long startTime;
2827
long samplesTaken = 0; //Counter for calculating the Hz or read rate
29-
3028
long unblockedValue; //Average IR at power up
29+
long startTime; //Used to calculate measurement rate
3130

3231
void setup()
3332
{
@@ -58,14 +57,7 @@ void setup()
5857
unblockedValue = 0;
5958
for (byte x = 0 ; x < 32 ; x++)
6059
{
61-
//Wait for new readings to come in
62-
while (particleSensor.available() == false)
63-
{
64-
particleSensor.check(); //Check the sensor, read up to 3 samples
65-
}
66-
6760
unblockedValue += particleSensor.getIR(); //Read the IR value
68-
particleSensor.nextSample(); //We're finished with this sample so move to next sample
6961
}
7062
unblockedValue /= 32;
7163

@@ -74,31 +66,24 @@ void setup()
7466

7567
void loop()
7668
{
77-
particleSensor.check(); //Check the sensor, read up to 3 samples
78-
79-
while (particleSensor.available()) //do we have new data?
80-
{
81-
samplesTaken++;
69+
samplesTaken++;
8270

83-
Serial.print("IR[");
84-
Serial.print(particleSensor.getIR());
85-
Serial.print("] Hz[");
86-
Serial.print((float)samplesTaken / ((millis() - startTime) / 1000.0), 2);
87-
Serial.print("]");
71+
Serial.print("IR[");
72+
Serial.print(particleSensor.getIR());
73+
Serial.print("] Hz[");
74+
Serial.print((float)samplesTaken / ((millis() - startTime) / 1000.0), 2);
75+
Serial.print("]");
8876

89-
long currentDelta = particleSensor.getIR() - unblockedValue;
77+
long currentDelta = particleSensor.getIR() - unblockedValue;
9078

91-
Serial.print(" delta[");
92-
Serial.print(currentDelta);
93-
Serial.print("]");
79+
Serial.print(" delta[");
80+
Serial.print(currentDelta);
81+
Serial.print("]");
9482

95-
if (currentDelta > (long)100)
96-
{
97-
Serial.print(" Something is there!");
98-
}
99-
100-
Serial.println();
101-
102-
particleSensor.nextSample(); //We're finished with this sample so move to next sample
83+
if (currentDelta > (long)100)
84+
{
85+
Serial.print(" Something is there!");
10386
}
87+
88+
Serial.println();
10489
}

Examples/Example4_HeartBeat_Plotter/Example4_HeartBeat_Plotter.ino

+15-20
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
5) Checkout the blips!
1515
6) Feel the pulse on your neck and watch it mimic the blips
1616
17-
It is best to attach the sensor to your finger using a rubber band or other tightening
18-
device. Humans are generally bad at applying constant pressure to a thing. When you
19-
press your finger against the sensor it varies enough to cause the blood in your
17+
It is best to attach the sensor to your finger using a rubber band or other tightening
18+
device. Humans are generally bad at applying constant pressure to a thing. When you
19+
press your finger against the sensor it varies enough to cause the blood in your
2020
finger to flow differently which causes the sensor readings to go wonky.
2121
2222
Hardware Connections (Breakoutboard to Arduino):
@@ -25,7 +25,7 @@
2525
-SDA = A4 (or SDA)
2626
-SCL = A5 (or SCL)
2727
-INT = Not connected
28-
28+
2929
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V
3030
but it will also run at 3.3V.
3131
*/
@@ -47,7 +47,15 @@ void setup()
4747
while (1);
4848
}
4949

50-
particleSensor.setup(); //Configure sensor. Use 6.4mA for LED drive
50+
//Setup to sense a nice looking saw tooth on the plotter
51+
byte ledBrightness = 0x1F; //Options: 0=Off to 255=50mA
52+
byte sampleAverage = 8; //Options: 1, 2, 4, 8, 16, 32
53+
byte ledMode = 3; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
54+
byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
55+
int pulseWidth = 411; //Options: 69, 118, 215, 411
56+
int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
57+
58+
particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
5159

5260
//Arduino plotter auto-scales annoyingly. To get around this, pre-populate
5361
//the plotter with 500 of an average reading from the sensor
@@ -57,29 +65,16 @@ void setup()
5765
long baseValue = 0;
5866
for (byte x = 0 ; x < avgAmount ; x++)
5967
{
60-
//Wait for new readings to come in
61-
while (particleSensor.available() == false)
62-
{
63-
particleSensor.check(); //Check the sensor, read up to 3 samples
64-
}
65-
6668
baseValue += particleSensor.getIR(); //Read the IR value
67-
particleSensor.nextSample(); //We're finished with this sample so move to next sample
6869
}
6970
baseValue /= avgAmount;
7071

72+
//Pre-populate the plotter so that the Y scale is close to IR values
7173
for (int x = 0 ; x < 500 ; x++)
7274
Serial.println(baseValue);
7375
}
7476

7577
void loop()
7678
{
77-
particleSensor.check(); //Check the sensor, read up to 3 samples
78-
79-
while (particleSensor.available()) //do we have new data?
80-
{
81-
Serial.println(particleSensor.getIR());
82-
83-
particleSensor.nextSample(); //We're finished with this sample so move to next sample
84-
}
79+
Serial.println(particleSensor.getIR()); //Send raw data to plotter
8580
}

Examples/Example5_HeartRate/Example5_HeartRate.ino

+28-35
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
-SDA = A4 (or SDA)
1919
-SCL = A5 (or SCL)
2020
-INT = Not connected
21-
21+
2222
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V
2323
but it will also run at 3.3V.
2424
*/
@@ -58,47 +58,40 @@ void setup()
5858

5959
void loop()
6060
{
61-
particleSensor.check(); //Check the sensor often
61+
long irValue = particleSensor.getIR();
6262

63-
if (particleSensor.available()) //do we have new data?
63+
if (checkForBeat(irValue) == true)
6464
{
65-
long irValue = particleSensor.getIR();
66-
67-
if (checkForBeat(irValue) == true)
65+
//We sensed a beat!
66+
long delta = millis() - lastBeat;
67+
lastBeat = millis();
68+
69+
beatsPerMinute = 60 / (delta / 1000.0);
70+
71+
if (beatsPerMinute < 255 && beatsPerMinute > 20)
6872
{
69-
//We sensed a beat!
70-
long delta = millis() - lastBeat;
71-
lastBeat = millis();
72-
73-
beatsPerMinute = 60 / (delta / 1000.0);
74-
75-
if (beatsPerMinute < 255 && beatsPerMinute > 20)
76-
{
77-
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
78-
rateSpot %= RATE_SIZE; //Wrap variable
79-
80-
//Take average of readings
81-
beatAvg = 0;
82-
for (byte x = 0 ; x < RATE_SIZE ; x++)
83-
beatAvg += rates[x];
84-
beatAvg /= RATE_SIZE;
85-
}
73+
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
74+
rateSpot %= RATE_SIZE; //Wrap variable
75+
76+
//Take average of readings
77+
beatAvg = 0;
78+
for (byte x = 0 ; x < RATE_SIZE ; x++)
79+
beatAvg += rates[x];
80+
beatAvg /= RATE_SIZE;
8681
}
82+
}
8783

88-
particleSensor.nextSample(); //We're finished with this sample so move to next sample
84+
Serial.print("IR=");
85+
Serial.print(irValue);
86+
Serial.print(", BPM=");
87+
Serial.print(beatsPerMinute);
88+
Serial.print(", Avg BPM=");
89+
Serial.print(beatAvg);
8990

90-
Serial.print("IR=");
91-
Serial.print(irValue);
92-
Serial.print(", BPM=");
93-
Serial.print(beatsPerMinute);
94-
Serial.print(", Avg BPM=");
95-
Serial.print(beatAvg);
91+
if (irValue < 50000)
92+
Serial.print(" No finger?");
9693

97-
if(irValue < 50000)
98-
Serial.print(" No finger?");
99-
100-
Serial.println();
101-
}
94+
Serial.println();
10295
}
10396

10497

0 commit comments

Comments
 (0)