Neopixel Question

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;
}

Hi,

What do you see on Serial Monitor at various distances? What colours did you want to see at these distances?

Paul

on the serial monitor I see the distance in the PIR and the colour is white and the colour i have put in the code is white .

whenever the distance is greater, it should go down to red... it was working well but now it's not even changing to the other colours that were before.

Based on your reply, I can say that there is a problem with your code, your wiring or your components.

If you cannot give more precice answers to questions, we cannot give more precice suggestions to help you.

-blanco-
-blanco-
-blanco-
-blanco-
-blanco-
-blanco-
-blanco-
-blanco-
Distancia en CM:
30
Porcentaje de distancia:
6
Color:
15
----------------------
16514043
----------------------
Distancia en CM:
31
Porcentaje de distancia:
8
Color:
20
----------------------
16452620
----------------------

Thanks for your reply PaulRB

That is what I get on serial. I get the cms that the Sharp is sensing, plus a percentage that it is from the sensor (closer to the sensor 0% , farthest 100% )

Now, I´ve run exactly the same code, and everything went fine. The closer I was to the sensor, I would get a red colour, when there was no object in front, I would get a white colour. (just as I´ve programmed in my code)

The thing is that, the red would stay as the main colour after a few tries of me putting an object in front of the sensor to make it red, and furthering the object to get it to white.

The point of it is that whenever there is no object, it stays white, not red.

Is is -possible- that the neopixels --save--- memory? (as in, the fact that it stays red when there is no object close)

The Neopixel holds whatever colour pattern it was last instructed to display.

If it does not change, it is because you did not give it an instruction to do so.