WS2812B Led's Not Working when called in sub-functions

Hi All,

I'm encountering a very strange issue trying to activate some WS2812B Led's from my Ardunio Nano.

My Setup() is quite standard and is not causing any issues, it activates a circle-pattern in the LED's I specified and then turns off all LED's, no issues.

The problem is when I get into the loop() , if i use function paintWord() here, it works as expected. For example below I turned the array of leds under arrHigh to blue and then to red before changing to blue again. When I run this, it all works as expected.

void loop(){

grid.setBrightness(50);

onlyInteger();

paintWord(arrHigh, colorBlue);
delay(1000);
paintWord(arrHigh, colorRed);
delay(10000);
paintWord(arrHigh, colorBlue);
 Serial.println("Loop Complete");
}

The issue is in onlyInteger(), when calling PaintWord() inside the if statement, it isn't actually changing any LED's. When testing, nothing was changing on the LED strips even though the serial monitor was outputting "Number is 2", "Number is 3" or "Number is over 4", which makes it appear as though the if statements are being evaluated and producing the correct output, except for actually activating the LED's.

To further test I moved the first paintWord for arrTwo outside of the if statements, and it worked immediately activating the second array of LED's, so it's as if the paintword is only not working within an if statement?

void onlyInteger(){
  paintWord(arrTwo, colorJGreen);
      if (integerFromPC == 2) {
          Serial.println("Number is 2 ");
          paintWord(arrTwo, colorBlue);
  
      } else if (integerFromPC == 3) {
          Serial.println("Number is 3 ");
          paintWord(arrThree, colorBlue);
    
      }  else {
          Serial.println("Number is over 4 ");
          paintWord(arrHigh, colorBlue);
      }
}

Paintword also:

void paintWord(int arrWord[], uint32_t intColor){
  for(int i = 0; i < grid.numPixels() + 1; i++){
    if(arrWord[i] == -1){
      grid.show();
      break;
    }else{
      grid.setPixelColor(arrWord[i],intColor);
    }
  }
}

Anyone have any ideas why or how I can fix this?

Thanks

Provide the complete sketch. Your "standard" is not standard.

For example:

Your as-of-yet-imaginary code that worksdoesnotwork is taking in a CRLF and you are not evaluating it.

Sure here's the full current version, not sure what you mean by not evaluating it? The serial print within each if / else if statement is appearing as expected, the only thing that doesn't seem to be occurring is the paintWord() element in each?

#include <Adafruit_NeoPixel.h>
 
#define RGBLEDPIN    4
#define N_LEDS 161 // 11 x 14 grid

Adafruit_NeoPixel grid = Adafruit_NeoPixel(N_LEDS, RGBLEDPIN, NEO_GRB + NEO_KHZ800);
int intTestMode;

// a few colors
uint32_t colorWhite = grid.Color(255, 255, 255);
uint32_t colorBlack = grid.Color(0, 0, 0);
uint32_t colorRed = grid.Color(255, 0, 0);
uint32_t colorGreen = grid.Color(0, 255, 0);
uint32_t colorBlue = grid.Color(0, 0, 255);
uint32_t colorJGreen = grid.Color(50, 179, 30);

// the words
int arrTwo[] = {0,1,2,-1};
int arrFour[] = {24,25,26,27,-1};
int arrThree[] = {14,15,16,17,18,-1};
int arrHigh[] = {42,43,44,45,-1};
int arrForCircle[] = {3,4,5,6,7,8,9,10,16,40,42,69,70,97,98,124,128,150,149,148,147,146,145,144,143,137,113,111,84,83,56,55,29,25,-1};

// int arrForSquare[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,41,42,69,70,97,98,125,126,153,152,
// 151,150,149,148,147,146,145,144,143,142,141,140,139,112,111,84,83,56,55,28,27,26,25,24,23,22,21,20,19,18,17,16,15,40,43,68,71,96,99,124,127,128,129,130,131
// ,132,133,134,135,136,137,138,113,110,85,82,57,54,29,30,31,32,33,34,35,36,37,38,39,44,67,72,95,100,123,122,121,120,119,118,117,116,115,114,109,86,81,58,53,52,51,50,49,48,47,46
// ,45,66,73,94,101,102,103,104,105,106,107,108,87,80,59,60,61,62,63,64,65,74,93,92,91,90,89,88,79,78,77,76,75,-1};

int arrUpArrow[] = {154,-1};

int integerFromPC = 3;


void setup() {

  // set up the debuging serial output
  Serial.begin(9600);
  delay(200);
  
  // setup the LED strip  
  grid.begin();
  grid.show();

  // set the bright ness of the strip
  grid.setBrightness(50);
  paintCircle(colorBlue,100,2);

  delay(50);
  colorWipe(colorBlack, 0);
  grid.setBrightness(50);
  Serial.println("Setup Complete");
  
}

void loop(){

grid.setBrightness(50);

onlyInteger();

paintWord(arrHigh, colorBlue);
delay(1000);
paintWord(arrHigh, colorRed);
delay(10000);
paintWord(arrHigh, colorBlue);

Serial.println("Loop Complete");

}


void onlyInteger(){
  paintWord(arrTwo, colorJGreen);
      if (integerFromPC == 2) {
          Serial.println("Number is 2 ");
          paintWord(arrTwo, colorBlue);
  
      } else if (integerFromPC == 3) {
          Serial.println("Number is 3 ");
          paintWord(arrThree, colorBlue);
    
      }  else {
          Serial.println("Number is over 4 ");
          paintWord(arrHigh, colorBlue);
      }
}

void rainbow(uint8_t wait) {
  //secret rainbow mode
  uint16_t i, j;
 
  for(j=0; j<256; j++) {
    for(i=0; i<grid.numPixels(); i++) {
      grid.setPixelColor(i, Wheel((i+j) & 255));
    }
    grid.show();
    delay(wait);
  }
}

static void chase(uint32_t color, uint8_t wait) {
  for(uint16_t i=0; i<grid.numPixels()+4; i++) {
      grid.setPixelColor(i  , color); // Draw new pixel
      grid.setPixelColor(i-4, 0); // Erase pixel a few steps back
      grid.show();
      delay(wait);
  }
}

void fadeOut(int time){
  for (int i = 50; i > 0; --i){
    grid.setBrightness(i);
    grid.show();
    delay(time);
  }
}

void fadeIn(int time){
  for(int i = 1; i < 50; ++i){
    grid.setBrightness(i);
    grid.show();
    delay(time);  
  }
}

void colorWipe(uint32_t color, uint8_t wait) {
  for(uint16_t i=0; i<grid.numPixels(); i++) {
      grid.setPixelColor(i, color);
  }
  grid.show();
  delay(wait);
}

uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return grid.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return grid.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return grid.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

void paintWord(int arrWord[], uint32_t intColor){
  for(int i = 0; i < grid.numPixels() + 1; i++){
    if(arrWord[i] == -1){
      grid.show();
      break;
    }else{
      grid.setPixelColor(arrWord[i],intColor);
    }
  }
}

static void paintCircle(uint32_t color, uint8_t wait, uint16_t loops) {
  for(uint16_t j=0; j < loops + 1; j++){
    paintWord(arrForCircle, colorRed);
  for(uint16_t i=0; i < 35; i++) {
      grid.setPixelColor(arrForCircle[i]  , color); // Draw new pixel
      grid.show();
      delay(wait);
  }}
}

static void paintSquare(uint32_t color, uint8_t wait, uint16_t loops) {
  for(uint16_t j=0; j < loops + 1; j++){
    paintWord(arrForSquare, colorRed);
  for(uint16_t i=0; i < 154; i++) {
      grid.setPixelColor(arrForSquare[i]  , color); // Draw new pixel
      grid.show();
      delay(wait);
  }}
}

void spellWord(int arrWord[], uint32_t intColor){
  for(int i = 0; i < grid.numPixels() + 1; i++){
    if(arrWord[i] == -1){
      break;
    }else{
      grid.setPixelColor(arrWord[i],intColor);
      grid.show();
      delay(500);
    }
  }
}

You are not running what you are posting.

sketch.ino:169:15: error: 'arrForSquare' was not declared in this scope
     paintWord(arrForSquare, colorRed);
               ^~~~~~~~~~~~

The code "works" for me (a red circle covered by a blue sweeping circle). You probably (also) have power supply issues. 11 * 14 * 60mA = 9.24A.

You gave no code to evaluate, like showing a knob of a broken machine to evaluate engine failure.

Things like for (int i = 0; i < grid.numPixels() + 1; i++) { are wrong; you're writing outside boundaries of an array; e.g. in

void spellWord(int arrWord[], uint32_t intColor) {
  for (int i = 0; i < grid.numPixels() + 1; i++) {
    if (arrWord[i] == -1) {
      break;
    } else {
      grid.setPixelColor(arrWord[i], intColor);
      grid.show();
      delay(500);
    }
  }
}

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