Not able to get more than one pixel on neopixel strip lit

Hi everyone, just as a disclaimer before I ask anything, this is my first real programming project so try not to judge me too harshly. I am working on a project to basically use a potentiometer to emulate a battery charge display. As the potentiometer gets turned, various segments of the Neopixel array will light up or turn off (think the battery indicator on your phone). As it is turned up, the array should "fill up" by lighting up pixels in order: from red, to yellow, to green. They should also speed up and slow down depending on how far the potentiometer is turned.

Currently, I can only get one pixel to light up and I cannot figure out why. Any help or pointers are greatly appreciate.

#include <Adafruit_NeoPixel.h>

//global definitions
#define PIN 6
#define LENGTH 12
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LENGTH, PIN, NEO_GRB + NEO_KHZ800);

int potValue = 0;
int rate = 0;
int i;
int blip = 10000; //ms
int n; //pixels
int blipTime = 0;

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

void loop() {
    potValue =analogRead(A0);
   //assign rates to pot values
  if (potValue < 205 && potValue >= 0) {
    rate = -200;
  } else if (potValue < 410 && potValue >=206) {
    rate = -100;
  } else if (potValue < 615 && potValue >=411) {
    rate = 2;
  } else if (potValue < 820 && potValue >=616) {
    rate = 100;
  } else {
    rate = 200;
  }
  ////////////////
  //light up LEDs
  //first segment
  if (rate == -200){ 
       i = i + rate*(-1);
   if (i >= blip) {
    n = n + 1 ;
   }
   strip.setPixelColor(n, 255, 255, 255);
   strip.show();

  }
  //second segment
  else if (rate == -100){
       i = i + rate*(-1);
   if (i >= blip) {
    n = n - 1 ;
   }
   strip.setPixelColor(n, 255, 255, 255);
   strip.show();
  }
  //third segment
  else if (rate == 0){
        i = i + rate;
   if (i >= blip) {
    n = n - 1 ;
   }
   strip.setPixelColor(n, 255, 255, 255);
   strip.show();   
  }
  //fourth segment
  else if (rate == 100){
        i = i + rate;
   if (i >= blip) {
    n = n + 1 ;
   }
   strip.setPixelColor(n, 255, 255, 255);
   strip.show();   
  }
  //fifth segment
  else if (rate == 200){
        i = i + rate;
   if (i >= blip) {
    n = n + 1 ;
   }
   strip.setPixelColor(n, 255, 255, 255);
   strip.show(); 
   
  }
  delay(500);
 //Serial.println(rate);
}




#include <Adafruit_NeoPixel.h>

//global definitions
#define PIN 6
#define LENGTH 12
Adafruit_NeoPixel strip(LENGTH, PIN, NEO_GRB + NEO_KHZ800);

int potValue = 0;
uint32_t white = strip.Color(255, 255, 255);

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

void loop() {
  potValue = analogRead(A0);
  strip.clear();
  strip.fill(white, 0, potValue/85);
  strip.show();
  delay(100);
}

Be careful with not including all intended values. Look at the first and second "if" conditions.

The first condition tests if the value is less than 205.
The second condition tests if the value is greater or equal to 206.
That leaves value = 205 failing all conditions and falls through as a "default" and rate will be assigned 200.
Same is true with 410, 615 and 820.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.