-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat.ino
96 lines (76 loc) · 1.88 KB
/
heartbeat.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define PULSE_PIN 0
#define PULSE_THRESHOLD 10
#define LED_PIN 6
#define NUM_LEDS 144
#define BRIGHTNESS 10
#define DELAY 10
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <FastLED.h>
#include <PulseSensorPlayground.h>
CRGB leds[NUM_LEDS];
PulseSensorPlayground pulseSensor;
const int pulseNum = 10;
int pulseIndex = 0;
class CustomPulse{
public:
int index;
int fade;
int latency;
bool stopped;
CustomPulse()
{
index = 0;
fade = 40;
stopped = true;
latency = 5;
}
void reset(){
index = 0;
stopped = false;
}
bool update(){
index++;
if(index == NUM_LEDS - latency){
Serial.println("Finished Pulse");
}
if(index < NUM_LEDS && stopped == false){
leds[index] = CHSV(96, 255, 255);
leds[index].fadeToBlackBy(0);
}
for (int i = 0; i < fade; ++i) {
int newIndex = index - i;
if( newIndex > 0 && newIndex < NUM_LEDS) {
leds[newIndex] = CHSV(96, 255, 255);
leds[newIndex].fadeToBlackBy((255/fade)*i);
}
}
}
};
CustomPulse pulse[pulseNum];
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
pulseSensor.analogInput(PULSE_PIN);
pulseSensor.setThreshold(PULSE_THRESHOLD);
if (pulseSensor.begin()) {
Serial.println("We created a pulseSensor Object !"); //This prints one time at Arduino power-up, or on Arduino reset.
}
}
void loop() {
int bpm = pulseSensor.getBeatsPerMinute();
if (pulseSensor.sawStartOfBeat()) {
Serial.println("Pulse reset");
pulse[pulseIndex].reset();
pulseIndex++;
pulseIndex = pulseIndex % pulseNum;
}
for (int i = 0; i < NUM_LEDS; ++i) {
leds[i] = CHSV(0, 0, 0);
}
for(int i=0; i<pulseNum; i++)
{
pulse[i].update();
}
FastLED.show();
}