I posted here the other day. My problem was with addressable LED strips and range sensors.
concept is the further your hand is away from the range sensor the more LEDs in the strip light up.
however my code only allows this to work in one direction. As I raise my hand away from the sensor the LEDs illuminate as desired but are then "stuck" lowering my hand does not tell them to turn back off.
Admittedly, I am not a programmer. I only posses the most basic idea of what I'm doing and I do not yet really "grasp" all the syntax of a "neat and cohesive" script.
That being said, I think my problem is here:
for(i=0; i<v;) {
if (i<v, i++);
strip.setPixelColor(i,0,0,175);
if (i=v; i>0;) i--)
strip.setPixelColor(i,0,0,0);
strip.show();
Ill attach the complete code at the end; What im trying to say here is
v = value translated from the range sensor
i = the number of pixels in the strip or the "last pixel that is still illuminated"
if the value of 'v' is greater than the value of 'i' make 'v' equal to 'i' and illuminate the 'v' value of pixels.
if the value of 'v' is less than the value of 'i' make 'v' equal to 'i' and turn of the pixels no longer in the equation.
(now that we have checked both possible outcomes)
show the one that is relevant.
I think where im getting in trouble is when I try to redefine the v=0; i=0 stuff
here is the full code, try not to laugh
#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.setPixelColor(i,0,0,0);
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);
}