Hi guys,
I've got a Neopixel strip hooked up on a 4.5 V lip battery. an Arduino Mega and Sharp Sensor powered by the computer's USB.
I've got this code for the strip that will turn different colours when my sharp sensor detects a certain range.
It will be used to give me feedback when I'm parking my car, the closer I get, the colour changes to red.
Right now I'm just doing a simple test. but the problem is that when I change the distance... the Neopixels do change colour, but not to the one I want (will change to green blue, violet, etc)
Is there something that you guys think could be wrong with my code?
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
#define PIN 6
#define NUMPIXELS 60
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int sharpIR = 0;
int maxDistance = 50;
int myColor = 255;
int limitValue = 25;
int colorMultiplier = 2.55;
void setup(){
pixels.begin();
pixels.show();
colorWipe(pixels.Color(0,255,0));
Serial.begin(9600);
Serial.println("Inicio App");
}
void loop()
{
int distance = getDist();
int cmDistance = 9462/(distance - 16.92);
int distPerc = ((cmDistance-limitValue) * 100) / (maxDistance-limitValue) ;
//Serial.println("Inicio App");
if(distPerc > 100){
//do nothing, Distancia maxima superada agagar
colorWipe(pixels.Color(255,255,255));
Serial.println("-blanco-");
} else if (distPerc >= 0) {
//en rango de distancia
myColor = distPerc * 2.55;
Serial.println("Distancia en CM:");
Serial.println(cmDistance);
Serial.println("Porcentaje de distancia:");
Serial.println(distPerc);
Serial.println("Color:");
Serial.println(myColor);
Serial.println("----------------------");
colorWipe(pixels.Color(255,myColor,myColor));
}
delay(1000);
}
// NEOPIXEL METHOD: fill the dots one after the other with a color
void colorWipe(uint32_t c) {
for(int i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, c);
}
pixels.show();
}
int getDist()
{
int distRead = analogRead(sharpIR);
int distAvg = distRead;
return distAvg;
}