series of brainfarts // code does not behave in any way as I expected to

I have the following code: (simulation in tinkercad)

#include <Adafruit_NeoPixel.h>

#define PIN 11	 // input pin Neopixel is attached to

#define NUMPIXELS      19 // number of neopixels in strip

#define  delayval  200  // timing delay in milliseconds

int LEDColor[7][4]={{127,127,0,0},{127,127,0,0},{127,127,0,0},{127,127,0,0},{127,127,0,0},{127,127,0,0},{127,127,0,0}};	//LED block see above
unsigned long nextMillis[]={0,0,0,0,0,0,0};

void setup() {
  // Initialize the NeoPixel library.
  pixels.begin();
  pinMode(9, OUTPUT);
  Serial.begin(9600);

}

void loop() {
  for (int i=0; i++; i <= 8) //sizeof(nextMillis))
  {
    Serial.println("doin loop");
	Serial.println(i);
    if (millis()>=nextMillis[i]){
      nextMillis[i]=millis()+ranPause();
      //Serial.println(nextMillis[i]);
      pixels.setPixelColor(i, pixels.Color(LEDColor[i][0],LEDColor[i][1],LEDColor[i][2]));
      pixels.show();  
    }
  }
  delay(delayval); // to help the simulation to keep up
  //Serial.println("mainloop pause");
}
int ranPause(){
  return random(100, 150);
}

Here is are my problems:

  • I expect sizeof(nextMillis) to be 8 but it is 28. Why? I want to use the size of the array at the for loop.
  • The for loop never gets triggered (according to what I see on the serial). Why?
  1. The "for" loop MUST run. Why do you say that it does not.

  2. An "unsigned long" is four bytes. An array of seven unsigned longs will be 4 x 7 = 28 bytes. "sizeof" measures in bytes. Perhaps you want sizeof(nextMillis) / sizeof(nextMillis[0])

I have little idea why you would expect 8.

Experienced users here often define a macro which I think looks like

#define elements(x) sizeof(x)/sizeof(x[0])

and then you can do

elements(nextMillis)

(UNTESTED ! )

for (int i=0; i++; i <= 8)

The for loop does not run because the condition to allow it to continue running is false, and that condition is evaluated before the contents of the loop are executed. The condition is "i++". I know you intended the condition to be "i <= 8" but you got them the wrong way around. "i++" is false/zero (the value of i, from before the increment, gets used), so the loop terminates immediately.

I expect sizeof(nextMillis) to be 8 but it is 28. Why? I want to use the size of the array at the for loop.

Why 8 when there are 7 elements in the array ?

In any case, sizeof() returns the number of bytes used and unsigned longs take 4 bytes each

To get the number of elements in an array of any kind use

numberOfElements = sizeof(theArrray) / sizeof(theArray[0])

PaulRB:

for (int i=0; i++; i <= 8)

The for loop does not run because the condition to allow it to continue running is false, and that condition is evaluated before the contents of the loop are executed. The condition is "i++". I know you intended the condition to be "i <= 8" but you got them the wrong way around. "i++" is false/zero (the value of i, from before the increment, gets used), so the loop terminates immediately.

Ah thanks ... there was it ... dunno why I messed that up

vaj4088:

  1. An "unsigned long" is four bytes. An array of seven unsigned longs will be 4 x 7 = 28 bytes. "sizeof" measures in bytes. Perhaps you want sizeof(nextMillis) / sizeof(nextMillis[0])

Didn't knew that it gave back the bytes. Other languages do similar things and give back the number elements in array.

Ok, more things that don't work like I expect:
when I run

for (int i=0; i <= (sizeof(nextMillis)/sizeof(nextMillis[0])); i++){
      [... other parts of the code block ...]
      Serial.print(i);
      [... other parts of the code block ...]
}

serial shows as i sometime rouge values.
Also at same time one LED which should turns on or updates. (so it is not just the serial)
Quirks of tinkercad?
Side-note: The LED also shows rouge behavior when I use for (int i=0; i <= 7 ; i++)

current full code:

/*
  describe your code here
*/

#include <Adafruit_NeoPixel.h>

#define PIN 11	 // input pin Neopixel is attached to

#define NUMPIXELS      19 // number of neopixels in strip

#define  delayval	20 // timing delay in milliseconds


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

/*
int redColor = 127;
int greenColor = 127;
int blueColor = 0;

// contents: red value, green value, blue value, position on the color cycle 
int LED1Color[]={127,127,0,0};	// pair 1 = 1	& 13
int LED2Color[]={127,127,0,0};	// pair 2 =	3	& 14
int LED3Color[]={127,127,0,0};	// pair 3 = 5	& 15
int LED4Color[]={127,127,0,0};	// pair 4 = 7	& 16
int LED5Color[]={127,127,0,0};	// pair 5 = 9	& 17
int LED6Color[]={127,127,0,0};	// pair 6 = 11	& 18
int LED7Color[]={127,127,0,0};	//top LED = 19
*/
int LEDColor[7][4]={{127,127,0,0},{127,127,0,0},{127,127,0,0},{127,127,0,0},{127,127,0,0},{127,127,0,0},{127,127,0,0}};	//LED block see above
int LED8Color[]={127,127,0,0};	//LED		2
int LED9Color[]={127,127,0,0};	//LED		4
int LED10Color[]={127,127,0,0};	//LED		6
int LED11Color[]={127,127,0,0};	//LED		8
int LED12Color[]={127,127,0,0};	//LED		10
int LED13Color[]={127,127,0,0};	//LED		12

unsigned long nextMillis[]={0,0,0,0,0,0,0};

int bright =0;

void setup() {
  // Initialize the NeoPixel library.
  pixels.begin();
  pinMode(9, OUTPUT);
  Serial.begin(9600);

}

void loop() {
//  for (int i=0; i <= (sizeof(nextMillis)/sizeof(nextMillis[0])); i++) //sizeof(nextMillis))
  for (int i=0; i <= 7 ; i++)
  {
    if (millis()>=nextMillis[i]){
      nextMillis[i]=millis()+ranPause();
//      Serial.print(i);
//      Serial.print(" --> ");
//      Serial.println(nextMillis[i]);
      pixels.setPixelColor(i, pixels.Color(LEDColor[i][0],LEDColor[i][1],LEDColor[i][2]));
      colorRotation(i,LEDColor[i][0],LEDColor[i][1],LEDColor[i][2],LEDColor[i][3]);
      pixels.show();  
    }
  }
  delay(delayval);  

}


void setBright() { // placeholder for now
  bright = random (0, 127);
}

int ranPause(){
//  return random(100, 150); // to slow in simulator more tweaking on actual hardware needed.
  return random(5, 10);
}

void colorRotation(int LED,int RredColor,int RgreenColor,int RblueColor,int circ){

  switch (circ) {
    case 0: //green
        if (RgreenColor <= 254) {
//              RredColor--; // to slow
//	          RgreenColor++;
              RredColor=RredColor-10;
	          RgreenColor=RgreenColor+10;
          if (RredColor<0) {RredColor=0;}
          if (RgreenColor>255) {RgreenColor=255;}
        }
    	else {
        	  circ++;
        }
        break;
    case 1: //blue
    	if (RblueColor <= 254) {
//      		RgreenColor--;
//      		RblueColor++;
      		RgreenColor=RgreenColor-10;
      		RblueColor=RblueColor+10;
          if (RgreenColor<0) {RgreenColor=0;}
          if (RblueColor>255) {RblueColor=255;}
    	}
    	else {
        	  circ++;
        }
    	break;
    case 2: //red
    	if (RredColor <= 254) {
//      		RblueColor--; 
//      		RredColor++;
      		RblueColor=RblueColor-10;
      		RredColor=RredColor+10;
          if (RblueColor<0) {RblueColor=0;}
          if (RredColor>255) {RredColor=255;}
    	}
    	else {
        	  circ=0;
        }
    	break;
    default:
    	circ=0;
    	break;
  }

//LEDColor[LED][]= {RredColor,RgreenColor,RblueColor,circ};  
//LEDColor[LED]= {RredColor,RgreenColor,RblueColor,circ};  Both do not work. Why?
  LEDColor[LED][0]= RredColor;
  LEDColor[LED][1]= RgreenColor;
  LEDColor[LED][2]= RblueColor;
  LEDColor[LED][3]= circ;
}

What do you mean by "rogue values"? Copy some example output from serial monitor and paste it between code tags.

Sorry can't (anymore) have rewritten the code a few times since then.
Well, instead of a number between 0 and 7 it did put out a "A" (yes as capital) with some sort thingy on top or a "y" with some sort thingy on top.

Just randomly in the middle ... might have been the JS on which the simulation runs, tho.

for (int i=0; i <= 7 ; i++) This is incorrect for a seven element array.
The condition should be " < 7"

PaulRB was correct. I missed that subtlety!

Urgh ... I could SHOULD have catched that as well ... I even wrote a switch function from 0 to 6 after that (in that loop).
My brain is so rotten I probably pass as a zombie.