Only 29 pixels instead of 60

I have uploaded this sketch for a neopixel vu meter with a 1 mtr 60 pixel strip to my Arduino uno. When I run the sketch with a 1 mtr strip connected, only 29 pixels light up, yet it is: #define N_PIXELS 60 // Number of pixels in strand.

Can anyone please tell me why only 29 pixels light up when all 60 pixels light up using a different sketch.
Thanks

type onclude <Adafruit_NeoPixel.h>
#include <math.h>
#include <SPI.h>

#define N_PIXELS  60  // Number of pixels in strand
#define MIC_PIN   A5  // Microphone is attached to this analog pin
#define LED_PIN    6  // NeoPixel LED strand is connected to this pin ## 1000 for classroom
#define SAMPLE_WINDOW   70// Sample window for average level //was 70
#define NOISE     12  // Noise/hum/interference in mic signal 
#define PEAK_HANG 24 //Time of pause before peak dot falls
#define PEAK_FALL 0 //Rate of falling peak dot
#define INPUT_FLOOR 24 //Lower range of analogRead input 10(on USB ) 25 (on battery)
#define INPUT_CEILING 85 //Max range of analogRead input, the lower the value the more sensitive 65 (on Bat) is sensitive) (85 may be ok)

int csPinRED = 8; // RED DISPLAY
int csPinAMBER = 7; //AMBER DISPLAY

int REDcycles = 0;
int AMBERcycles = 0;

byte peak = 16;      // Peak level of column; used for falling dots
unsigned int sample;

byte dotCount = 0;  //Frame counter for peak dot
byte dotHangCount = 0; //Frame counter for holding peak dot

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);

float fscale( float originalMin, float originalMax, float newBegin, float
newEnd, float inputValue, float curve){

  float OriginalRange = 0;
  float NewRange = 0;
  float zeroRefCurVal = 0;
  float normalizedCurVal = 0;
  float rangedValue = 0;
  boolean invFlag = 0;


  // condition curve parameter
  // limit range

  if (curve > 10) curve = 10;
  if (curve < -10) curve = -10;

  curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output 
  curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function

  /*
   Serial.println(curve * 100, DEC);   // multply by 100 to preserve resolution  
   Serial.println(); 
   */

  // Check for out of range inputValues
  if (inputValue < originalMin) {
    inputValue = originalMin;
  }
  if (inputValue > originalMax) {
    inputValue = originalMax;
  }

  // Zero Refference the values
  OriginalRange = originalMax - originalMin;

  if (newEnd > newBegin){ 
    NewRange = newEnd - newBegin;
  }
  else
  {
    NewRange = newBegin - newEnd; 
    invFlag = 1;
  }

  zeroRefCurVal = inputValue - originalMin;
  normalizedCurVal  =  zeroRefCurVal / OriginalRange;   // normalize to 0 - 1 float

  // Check for originalMin > originalMax  - the math for all other cases i.e. negative numbers seems to work out fine 
  if (originalMin > originalMax ) {
    return 0;
  }

  if (invFlag == 0){
    rangedValue =  (pow(normalizedCurVal, curve) * NewRange) + newBegin;

  }
  else     // invert the ranges
  {   
    rangedValue =  newBegin - (pow(normalizedCurVal, curve) * NewRange); 
  }

  return rangedValue;
}

//Used to draw a line between two points of a given color
void drawLine(uint8_t from, uint8_t to, uint32_t c) {
  strip.setPixelColor(28, 255, 0, 0);
  strip.setPixelColor(27, 255, 0, 0);
  strip.setPixelColor(26, 255, 0, 0);
  strip.setPixelColor(25, 255, 0, 0);
  strip.setPixelColor(24, 255, 0, 0);
  strip.setPixelColor(23, 255, 0, 0);
  strip.setPixelColor(22, 255, 0, 0);
  strip.setPixelColor(21, 255,69,0);
  strip.setPixelColor(20, 255,69,0);
  strip.setPixelColor(19, 255,69,0);
  strip.setPixelColor(18, 255,69,0);
  strip.setPixelColor(17, 255,69,0);
  strip.setPixelColor(16, 255,69,0);
  strip.setPixelColor(15, 255,69,0);
  strip.setPixelColor(14, 255,69,0);
  strip.setPixelColor(13, 0,255,0);
  strip.setPixelColor(12, 0,255,0);
  strip.setPixelColor(11, 0,255,0);
  strip.setPixelColor(10, 0,255,0);
  strip.setPixelColor(9, 0,255,0);
  strip.setPixelColor(8, 0,255,0);
  strip.setPixelColor(7, 0,255,0);
  strip.setPixelColor(6, 0,255,0);
  strip.setPixelColor(5, 0,255,0);
  strip.setPixelColor(4, 0,255,0);
  strip.setPixelColor(3, 0,255,0);
  strip.setPixelColor(2, 0,255,0);
  strip.setPixelColor(1, 0,255,0);
  strip.setPixelColor(0, 0,255,0);
  uint8_t fromTemp;

 
  
  if (from > to) {
    fromTemp = from;
    from = to;
    to = fromTemp;
  }
  for(int i=from; i<=to; i++){
    strip.setPixelColor(i, c);
  }

 
}

void setup() 
{
  // This is only needed on 5V Arduinos (Uno, Leonardo, etc.).
  // Connect 3.3V to mic AND TO AREF ON ARDUINO and enable this
  // line.  Audio samples are 'cleaner' at 3.3V.
  // COMMENT OUT THIS LINE FOR 3.3V ARDUINOS (FLORA, ETC.):
  //  analogReference(EXTERNAL);

  // Serial.begin(9600);
  strip.begin();
  strip.setBrightness(50);
  strip.show(); // Initialize all pixels to 'off'
  Serial.begin(9600);  

  pinMode(csPinRED, OUTPUT);
  digitalWrite(csPinRED, HIGH); //By default, don't be selecting OpenSegment
  pinMode(csPinAMBER, OUTPUT);
  digitalWrite(csPinAMBER, HIGH); 
  Serial.println("OpenSegment Example Code");

  SPI.begin(); //Start the SPI hardware
  SPI.setClockDivider(SPI_CLOCK_DIV64); //Slow down the master a bit

  //Send the reset command to the display - this forces the cursor to return to the beginning of the display
  digitalWrite(csPinRED, LOW); //Drive the RED CS pin low to select OpenSegment
  digitalWrite(csPinAMBER, LOW); //Drive the AMBER CS pin low to select OpenSegment
  spiSendValueAMBER(0000);
  spiSendValueRED(0000);
  SPI.transfer('v'); //Reset command
}

void loop() 
{
  unsigned long startMillis= millis();  // Start of sample window
  float peakToPeak = 0;   // peak-to-peak level

  unsigned int signalMax = 200;
  unsigned int signalMin = 1024;
  unsigned int c, y;

  


  // collect data for length of sample window (in mS)
  while (millis() - startMillis < SAMPLE_WINDOW)
  {
    sample = analogRead(MIC_PIN);
    if (sample < 1024)  // toss out spurious readings
    {
      if (sample > signalMax)
      {
        signalMax = sample;  // save just the max levels
      }
      else if (sample < signalMin)
      {
        signalMin = sample;  // save just the min levels
            
      }
    }
  }
  peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
 
  // Serial.println(peakToPeak);


 


  //Scale the input logarithmically instead of linearly
  c = fscale(INPUT_FLOOR, INPUT_CEILING, strip.numPixels(), 0, peakToPeak, 2);

  


  if(c < peak) {
    peak = c;        // Keep dot on top
    dotHangCount = 0;    // make the dot hang before falling
  }
  if (c <= strip.numPixels()) { // Fill partial column with off pixels
    drawLine(strip.numPixels(), strip.numPixels()-c, strip.Color(0, 0, 0));
    
  }

//if (millis() - startMillis > 70){
      if (c < 8 ){
      REDcycles++; //Counting RED cycles! Yay!
      Serial.print("RED Cycle: ");
      Serial.println(REDcycles);
      spiSendValueRED(REDcycles); //Send the four characters to the display
     delay(1); //If we remove the slow debug statements, we need a very small delay to prevent flickering
    }
      if(c > 8 && c < 16){
      AMBERcycles++; //Counting AMBER cycles!
      Serial.println("amber"); //Serial.println(redambercounter);
      Serial.print("AMBER Cycle: ");
      Serial.println(AMBERcycles);
      spiSendValueAMBER(AMBERcycles); //Send the four characters to the display
      delay(1); //If we remove the slow debug statements, we need a very small delay to prevent flickering
    }

//}
  strip.show();
   
  
  // Frame based peak dot animation
  if(dotHangCount > PEAK_HANG) { //Peak pause length
    if(++dotCount >= PEAK_FALL) { //Fall rate 
      peak++;
      dotCount = 0;
    }
  } 
  else {
    dotHangCount++; 
  }


}//endloop



//Given a number, spiSendValue chops up an integer into four values and sends them out over spi
void spiSendValueRED(int tempCyclesRED)
{
  digitalWrite(csPinRED, LOW); //Drive the CS pin low to select OpenSegment

  SPI.transfer(tempCyclesRED / 1000); //Send the left most digit
  tempCyclesRED %= 1000; //Now remove the left most digit from the number we want to display
  SPI.transfer(tempCyclesRED / 100);
  tempCyclesRED %= 100;
  SPI.transfer(tempCyclesRED / 10);
  tempCyclesRED %= 10;
  SPI.transfer(tempCyclesRED); //Send the right most digit

  digitalWrite(csPinRED, HIGH); //Release the CS pin to de-select OpenSegment
}

void spiSendValueAMBER(int tempCyclesAMBER)
{
  digitalWrite(csPinAMBER, LOW); //Drive the CS pin low to select OpenSegment

  SPI.transfer(tempCyclesAMBER / 1000); //Send the left most digit
  tempCyclesAMBER %= 1000; //Now remove the left most digit from the number we want to display
  SPI.transfer(tempCyclesAMBER / 100);
  tempCyclesAMBER %= 100;
  SPI.transfer(tempCyclesAMBER / 10);
  tempCyclesAMBER %= 10;
  SPI.transfer(tempCyclesAMBER); //Send the right most digit

  digitalWrite(csPinAMBER, HIGH); //Release the CS pin to de-select OpenSegment
}




r paste code here

Have you tested the strip with known good example code? That way you know that all the pixels work.

drawLine has only 29 pixels (strip.setPixelColor)?

Yes I have, and I did say that using a different sketch all 60 pixels light up.

The 29 explicitly set pixels in drawline is where I would start looking. Put some prints in that routine and see what your loop does to the rest.

@killzone_kid,
Aah, yes it does if you mean this part:

void drawLine(uint8_t from, uint8_t to, uint32_t c) {
  strip.setPixelColor(28, 255, 0, 0);
  strip.setPixelColor(27, 255, 0, 0);

So to get it to 60 , what do I have to do with or to the sketch.
I hope there is an easy way.

@widbill,
sorry I don't know what you mean about putting prints in the routine, unless you mean like continuing from 28 upwards.

Thanks

@wildbill
Ok , that is what I have done: added another 31 lines with the relevant colour I wanted.
Thanks to all for pointing this out to me.

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