conflicting code?

I have a range sensor and an addressable LED strip

the concept is the further your hand gets from the strip the more LEDS in the strand light up.

the problem is I can only seem to get it to work in one direction

im sure its something small im missing

#include "LPD6803.h"

#define N_PIXELS 50 // Number of pixels in strand

const int pingPin = 7;

int dataPin = 2;
int clockPin = 3;

LPD6803 strip = LPD6803(50, dataPin, clockPin);

void setup() {

Serial.begin(9600);
strip.begin();
}

void loop() {
uint8_t i;
int duration, v;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
v = (duration/2) / 29.1;
v = map(v,20,0,50,0);
if (v <= 50) {
Serial.println(v);}
else
v = 2;
Serial.println(v);

delay(50);

for(i=0; i<v;) {
if (i<v, i++);
strip.setPixelColor(i,0,0,175);
if (i=v; i>0;) i--)
strip.show();

}
}

unsigned int Color(byte r, byte g, byte b)
{
//Take the lowest 5 bits of each value and append them end to end
return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F);
}

if (i<v, i++);
; no no no!

Get into the habit of this style.
if (i<v, i++)
{
// your conditional code
}
.

so like this?

for(i=0; i<v;) {
if (i<v, i++)
strip.setPixelColor(i,0,0,175);
}
for (i=v; i>0;) {
if (i<v, i--)
}
strip.setPixelColor(i,0,0,175);
strip.show();

It's easier to read when { } are on separate lines.
if( )
{
}

also use CTRL T to format your code to make it easier to read.

also:
To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

.

sorry, is that better?

#include "LPD6803.h"

#define N_PIXELS  50  // Number of pixels in strand

const int pingPin = 7;

int dataPin = 2;
int clockPin = 3; 

  LPD6803 strip = LPD6803(50, dataPin, clockPin);

void setup() {
  
  Serial.begin(9600);
  strip.begin();
}

void loop() {
  uint8_t  i;
  int duration, v;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  v = (duration/2) / 29.1;
  v = map(v,20,0,50,0);
  if (v <= 50) {
    Serial.println(v);}
    else
    v = 2;
    Serial.println(v);

  delay(50);

  for(i=0; i<v;) {
    if (i<v, i++)
    strip.setPixelColor(i,0,0,175);
  }
  for (i=v; i>0;) {
    if (i<v, i--)
  }
    strip.setPixelColor(i,0,0,175);
    strip.show();
    
  }
}

unsigned int Color(byte r, byte g, byte b)
{
  //Take the lowest 5 bits of each value and append them end to end
  return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F);
}

Better
I prefer { } on lines by them selves.

for (i = 0; i < v; i++)
{
strip.setPixelColor(i,0,0,175);
}

--Michael