Led fade on pulse sensor signal?

Hi All,

This is my first post!

I am working on an art project this weekend and have got really stuck - really early on!

I am trying to make a Led fade up and down to a signal - a heart pulse from a pulse sensor.

I have used the pulse sensor code but found it too sensitive in that it was reading beats that were not there so i am using this very stripped back code which seems to work well.

I am using a pulse sensor pro and a pro mini board but right now its not even working on the Uno so I am sure its the code.

On each beat it is only blinking so I know the circuit is ok and the signal is reaching it - its just not fading up and down and I cannot make it work??

Would really appreciate a bit o help with this - its driving me blinking nuts! :o

// Pins
const int ledPin = 3;
const int sensePin = 0;

// LED blink variables
int ledState = LOW;

// Hearbeat detect variables
int newHeartReading = 0;
int lastHeartReading = 0;
int Delta = 0;
int recentReadings[2] = {0,0};
int historySize = 2;
int recentTotal = 0;
int readingsIndex = 0;
boolean highChange = false;
int totalThreshold = 3;

// Heartbeat Timing
long lastHeartbeatTime = 0;
long debounceDelay = 150;
int currentHeartrate = 0;

void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);

}
void loop() {
// Turn off LED
analogWrite(ledPin, 0);

// Read analogue pin.
newHeartReading = analogRead(sensePin);
//Serial.println(newHeartReading);
//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;

//Debug
//Serial.println(recentTotal);

// Decide whether to start an LED Blink.
if (recentTotal >= totalThreshold) {
// Possible heartbeart, check time
if (millis() - lastHeartbeatTime >= debounceDelay) {
// Heartbeat
{
analogWrite; ledPin, ( 5, 0); // Sets voltage to 0V (0/255 * 5V). LED is off.
delay(500); // Wait a second

analogWrite; ledPin, (5, 32); // Sets voltage to ~1.25V (64/255 * 5V). Pretty dim.
delay(50); // Wait a second

analogWrite(5, 64);// Set voltage to ~2.5V. Half-bright.
delay(50); // Wait a second

analogWrite(5, 96);// Set voltage to ~3.75V. Getting brighter!
delay(200); // Wait a second
// Wait a second

analogWrite(5, 64);// Set voltage to ~2.5V. Half-bright.
delay(50);

analogWrite(5, 32); // Sets voltage to ~1.25V (64/255 * 5V). Pretty dim.
delay(50); // Wait a second

analogWrite(5, 0); // Sets voltage to 0V (0/255 * 5V). LED is off.
delay(50);
}
currentHeartrate = 60000 / (millis() - lastHeartbeatTime);
lastHeartbeatTime = millis();
// Print Results
//Serial.println(“Beat”);
if (currentHeartrate <= 200)
{ Serial.println(currentHeartrate); } } } delay(10); }

You're using millis() and delay() ─ millis() is millis() and delay() is delay() and never the twain should meet.

If you take a look at this

you may manage to get a pulse detection to trigger running through it once.

 delay(500);      // Wait a second
 delay(50);      // Wait a second

It is never a good idea to have comments that clearly contradict the code. It is even worst when they contradict each other. The number in delay is in milliseconds so the first one is half a second and the second on is 50mS.

But as Runaway Pancake told you, you don't want to use delay at all.

Also please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

Thank you for your help - I really appreciate it!

I have it fading in and out at the pulse now - only I still had to use a delay or it just goes crazy blinking blinking!

Also now it remains in a slightly on state when no pulse and I cant figure out why this is?

// Pins
const int ledPin = 3;
const int sensePin = 0;
int brightness = 0;    // how bright the LED is

// LED blink variables
int ledState = LOW;

// Hearbeat detect variables
int newHeartReading = 0;
int lastHeartReading = 0;
int Delta = 0;
int recentReadings[2] = {0,0};
int historySize = 2;
int recentTotal = 0;
int readingsIndex = 0;
boolean highChange = false;
int totalThreshold = 5;


// Heartbeat Timing
long lastHeartbeatTime = 0;
long debounceDelay = 150;
int currentHeartrate = 0;

void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);


}
void loop() {
// Turn off LED
int ledState = LOW;



// Read analogue pin.
newHeartReading = analogRead(sensePin);
//Serial.println(newHeartReading);
//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;

//Debug
//Serial.println(recentTotal);

// Decide whether to start an LED Blink.
if (recentTotal >= totalThreshold) {
// Possible heartbeart, check time
if (millis() - lastHeartbeatTime >= debounceDelay) {
// Heartbeat
{


 for (brightness = 0; brightness < 200; brightness ++)
 {
   analogWrite(ledPin, brightness);
   delay(1);
 }

 for (brightness = 200; brightness > 0; brightness --)
 {
   analogWrite(ledPin, brightness);
   delay(1);
 }

  
  
}
currentHeartrate = 60000 / (millis() - lastHeartbeatTime);
lastHeartbeatTime = millis();
// Print Results
//Serial.println("Beat");
if (currentHeartrate <= 200)
{ Serial.println(currentHeartrate); } } } delay(10); }