Hi,
First post and first Arduino project ever, so please be patient.
So, I'm trying to make two LED pins to fade in and out inversely - when one LED reaches full power and begins fading out, the other begins to fade in and vice versa.
The trick is - the duration of the fade-in/fade-out is determined by you heart rate, as derived by the PulseSensor controller http://pulsesensor.com.
I've copy-pasted and mixed a bunch of different code snippets trying to make this happen, but I'm pretty sure I'm doing something wrong.
#include <SoftPWM.h>
const int sensePin = 0;
int fadelen=0;
// Hearbeat detect variables
int newHeartReading = 0;
int lastHeartReading = 0;
int Delta = 0;
int recentReadings[8] = {0,0,0,0,0,0,0,0};
int historySize = 8;
int recentTotal = 0;
int readingsIndex = 0;
boolean highChange = false;
int totalThreshold = 2;
// Heartbeat Timing
long lastHeartbeatTime = 0;
long debounceDelay = 150;
int currentHeartrate = 0;
void setup()
{
SoftPWMBegin();
SoftPWMSet(9, 0);
SoftPWMSet(10, 0);
}
void loop() {
newHeartReading = analogRead(sensePin);
//Calculate Delta
Delta = newHeartReading - lastHeartReading;
lastHeartReading = newHeartReading;
// Find new recent total
recentTotal = recentTotal - recentReadings[readingsIndex] + Delta;
// replace indexed recent value
recentReadings[readingsIndex] = Delta;
// increment index
readingsIndex = (readingsIndex + 1) % historySize;
// Decide whether to start an LED Blink.
if (recentTotal >= totalThreshold) {
// Possible heartbeart, check time
if (millis() - lastHeartbeatTime >= debounceDelay) {
currentHeartrate = 60000 / (millis() - lastHeartbeatTime);
lastHeartbeatTime = millis();
if (currentHeartrate <= 200) {
fadelen=(60/currentHeartrate)*3000;
SoftPWMSetFadeTime(9, fadelen, fadelen);
SoftPWMSetFadeTime(10, fadelen, fadelen);
SoftPWMSet(9, 255);
SoftPWMSet(10, 0);
delay(fadelen);
SoftPWMSet(10, 255);
SoftPWMSet(9, 0);
delay(fadelen);}
}
}
delay(10); }