RBG neopixel strip controlled by heartbeat sensor

Hello I am looking for some guidance on how to work with neopixels right now I am working on a project that uses a heartbeat sensor and adafruits neopixel strips to create "veins" that will run from a heart that pulses red.

I am trying to get the neopixels to run down the strip red and come back up the strip as blue and I'm having some trouble getting the pixels to turn blue on there way back up.

I am new to working with the neopixels so I am unsure of if my code is correct. If I could get any advice I would really appreciate it. I'm also new to this form so I apologize if my question is not valid. I have posted my code below for reference. Thank you!

#include <Adafruit_NeoPixel.h>

int val = 0;

#define PIN      6
#define N_LEDS 144

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);

float time1 = 0;
float updatedTime = 0;
float beatTime = 0;

void setup(){
  strip.begin();
  Serial.begin(9600); 
  pinMode(10, INPUT);
  pinMode(9, OUTPUT); 
  pinMode(6, OUTPUT); 
}
void loop(){
  
  val = analogRead(0);
  Serial.println(val);
  if(val>400&&val<600){
    
    analogWrite(9, 1023);
    
    time1 = updatedTime;
    updatedTime = millis();
    beatTime = (updatedTime-time1)/2;

    if(updatedTime - time1 > 380){
    for(uint16_t i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(0+i, strip.Color(255, 0, 0)); // Draw new pixel
        strip.setPixelColor(0+i, strip.Color(255, 0, 0));
        strip.setBrightness(255);
      
      if(i<144){
        strip.setPixelColor(i, strip.Color(255,0,0));
        strip.setPixelColor(i, strip.Color(255,0,0));
      }
        strip.setPixelColor(0-i, 0); // Erase pixel a few steps back
        strip.setPixelColor(0-i, 0); // Erase pixel a few steps back
        
        strip.show(); 
        if(beatTime<600){
        //delay(beatTime/200);  
        }else{
        delay(5);

      }
     }
    }


    for(uint16_t n=144; n>0; n--) {
     // delay(25);
        strip.setPixelColor(n, strip.Color(0, 0, 255)); // Draw new pixel
        strip.setPixelColor(n, strip.Color(0, 0, 255));
        strip.setBrightness(50);
        strip.setPixelColor(0+n, 0); // Erase pixel a few steps back
        strip.setPixelColor(0+n, 0); // Erase pixel a few steps back
        strip.show(); 
          
    }
  }
  digitalWrite(9, LOW);
}

What is this doing:-

analogWrite(9, 1023);

The analogWrite function only takes 0 to 255, yes it will wrap round but you should not do this.

On the way back there is no delay so you don't see anything because it goes through all the values very quickly.

ashduggan:
I am working on a project that uses a heartbeat sensor and adafruits neopixel strips to create "veins" that will run from a heart that pulses red.

Well, I trust that you realise that most veins do not pulsate and run back to the heart rather than from it. :grinning:

At least your arterial red colour and venous blue are technically appropriate. :roll_eyes:

I bought some led RBG neopixel strip directly from Chinese factory, Shenzhen Shining Future LED Technology Co., Ltd.
they help solve many problems when cotrol and power for them.

LindTan:
Shenzhen Shining Future LED Technology Co., Ltd.

@LindTan do you work for this company? You made two posts so far and mentioned them both times.

Make that three ! :wink: