Variable starts counting by itself

I just started working some code and am having an issue with one of the arrays. The code is to make some RGB LEDs fade from one colour to another, hold on to the colour for a little while, then fade to a new one. For right now I’m just testing it out with one RGB LED, so some of the code is just set for one.

The variable I’m having an issue with is hold_time. Each time a LED is assigned a colour, it’s also assigned a random number that tells it how many cycles to skip once it has been given a colour before letting the LED be assigned a new one. If you search for hold_time, it only occurs in 3 situations. With each colour when it’s assigned a random number 6 to 20, when it prints it’s value to serial and when it is compared to active to see if it’s time to let the LED get a new colour. When I monitor it, I can see it’s being assigned a number in that range, but then it starts slowly counting up one number at a time.

I have tried commenting out any code that affects it and making only be built, set to 4 and printed. It still starts counting.

Why is this variable counting?

int ledcount=10; //number of LEDs to control
int red[10]; //current red value
int green[10]; //current green value
int blue[10]; //current blue value
int red_target[10]; //target red value
int green_target[10]; //target green value
int blue_target[10]; //target blue value
int red_pin[10]; //pin for red value
int green_pin[10]; //pin for green value
int blue_pin[10]; //pin for blue value
//int colour[2]; //pre determined colours
int active[10]; //tracks what step the LED is on
int hold_time[10]; //how long will it stay one colour before changing
int rate[10]; //how quickly the actual colour change will happen
int timing_count=0; //counts loops, rate[] & hold_time[] use this
int led; //current LED



void setup()
{
  Serial.begin(9600);
  
  //assign pin outs
  red_pin[0] = 9;
  green_pin[0] = 10;
  blue_pin[0] = 11;
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);

}

void loop()
{

  set_colours(); //assign a colour and set the LED to active
  active_scan(); //find active LEDs and adjust the output colour  
  colour_check(); //check if LED colour has reached it's target
  colour_hold(); //keep the colour from changing right away
  
  //keep the timing count looping
  timing_count++;
  if (timing_count == 12) {
    timing_count=0;
  }
//  delay(100);
}

//////////(functions) \\\\\\\\\\\

void set_colours()
{

for(led = 0 ; led <= ledcount ; led ++) {
      

if (active[led]==0)  {
  
  switch(random(1,10)) {  // pick colours

  case 1:  ///red
    red_target[led] = 255;
    green_target[led] = 0;
    blue_target[led] = 0;
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;

  case 2:  // orange
    red_target[led] = 255;
    green_target[led] =  230;
    blue_target[led] = 200;
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;

  case 3:  //yellow
    red_target[led] = 255;
    green_target[led] = 255;
    blue_target[led] = 0;
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;

  case 4:  ///green
    red_target[led] = 0;
    green_target[led] = 255;
    blue_target[led] = 0;
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;

  case 5:  //cyan
    red_target[led] = 0;
    green_target[led] = 255; 
    blue_target[led] = 255;
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;

  case 6:  //blue
    red_target[led] = 0;
    green_target[led] =  0;
    blue_target[led] = 255;
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;

  case 7:  //magenta
    red_target[led] = 255;
    green_target[led] = 0;
    blue_target[led] = 255;
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;

  case 8:  //pink
    red_target[led] = 255;
    green_target[led] = 126; 
    blue_target[led] = 255;
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;

  case 9:  //random
    red_target[led] = random(0,255);
    green_target[led] = random(0,255);
    blue_target[led] = random(0,255);
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;

  case 10: //white
    red_target[led] = 255;
    green_target[led] = 255;
    blue_target[led] = 255;
    active[led]=1;
    rate[led]=random(4);
    hold_time[led]=random(6,20);
    break;
  }


}

//Serial.print(led);
//Serial.println(red_target[led]);
}
}

void active_scan() //change active LEDs and controls the speed of transition
{
for (led=0; led<=ledcount; led++) {
  if (active[led] == 1) {

  switch(rate[led])  {
    
    case 0: //full speed
    change_colours();
    break;
    
    case 1: //full speed
    change_colours();
    break;
    
    case 2: //half speed
//    if (timing_count == 0 ||
//        timing_count == 2 ||
//        timing_count == 4 ||
//        timing_count == 6 || 
//        timing_count == 8 || 
//        timing_count == 10)  {
     change_colours();
//        }
     break; 
     
    case 3: //third speed
//    if (timing_count == 0 ||
//        timing_count == 3 ||
//        timing_count == 6 || 
//        timing_count == 9)  {
     change_colours();
//        }
     break;
    
    case 4: //quarter speed
//    if (timing_count == 0 ||
//        timing_count == 4 ||
//        timing_count == 8)  {
     change_colours();
//        }
     break;  
    }    
  }
  }  
}

void change_colours() //set colour value and write to pin
{
//  for (led=0; led==ledcount; led++) {
    
    if(red[led]>red_target[led]) {
      red[led]--;
    }
    
    else{
      if(red[led]<red_target[led]) {
      red[led]++;
      }
      
    }
    
        if(green[led]>green_target[led]) {
      green[led]--;
    }
    
    else{
      if(green[led]<green_target[led]) {
      green[led]++;
      }
      
    }
    
         if(blue[led]>blue_target[led]) {
      blue[led]--;
    }
    
    else{
      if(blue[led]<blue_target[led]) {
      blue[led]++;
      }
      
    }
    
 //write the current colour out to LED
  Serial.println("XXXXXXXXXX");
 Serial.println(red[0]);
 Serial.println(green[0]);
 Serial.println(blue[0]);
 Serial.println("XXXXXXXXXX");
  Serial.println(red_target[0]);
 Serial.println(green_target[0]);
 Serial.println(blue_target[0]);
 Serial.println("XXXXXXXXXX");
  Serial.println(red_pin[0]);
 Serial.println(green_pin[0]);
 Serial.println(blue_pin[0]);
  Serial.println("XXXXXXXXXX");
  Serial.println(active[0]);
  Serial.println(hold_time[0]);
  analogWrite(red_pin[led],red[led]);
  analogWrite(green_pin[led],green[led]);
  analogWrite(blue_pin[led],blue[led]);
//  }

}

void colour_check() //see if LED has reached the target colour
{
  for (led=0; led<=ledcount; led++) {
    if (active[led] == 1)  {
      if (red[led] == red_target[led] &&
         green[led] == green_target[led] &&
         blue[led] == blue_target[led])  {
           active[led] = 2;
         }
    }
  }
}

void colour_hold() //hold on to the colour for a little bit before switching
{
  for (led=0; led<=ledcount; led++)  {
    if (active[led] > 1)  {
       if (timing_count == 0)  {
         active[led]++;
         if (active[led] == hold_time[led])  {
           active[led] = 0;
         }
       }
    }
  }
}

If you search for hold_time, it only occurs in 3 situations.

Have you actually tried this?

The IDE has a handy feature using ctrl-T - I recommend it.

for (led=0; led<=ledcount; led++) {

You don’t have a “active_led[10]”
(or a red [10], or a green [10]…)

Have you actually tried this?

Searching for hold_time? Yes. That's why I said it only occurs in 3 situations. If you're referring to it being in there more times than that, that's because most of the time it's the same situation.

As for the "active_led[10]" and other ones....what are you talking about? Why should I have that?

I’m talking about not having element ten in any of those arrays.

i.e. This declaration

int red[10];

allow you to use red[0] upto red[9]. If you try to write to red[10] you are probably going to overwrite the first element of another array.

I don't even have an active_led[] array. And I'm really only watching element 0 in this test, element 10 makes no difference. Any time I put anything into the array it's counting from 0 to int ledcount, which is set to 10. So there's as much being put into 10 as anything else.

I haven't used Arduino much, so obviously I'm not understanding all the ways it works. But with an issue of a variable that starts counting up by itself, what does any of this stuff you're saying have to do with it?

I wasn't trying to write to element 10, though in looking at the code I realized that ledcount was set to 10, not 9. Just fixed that... is this what you were reffering to?

for (led=0; led<=ledcount; led++)

Work this one through with pencil-and-paper

it’s counting from 0 to int ledcount

Bingo!

I get it now, just got confused when you mentioned arrays that weren't part of the code and things I didn't think I was even trying to write to. Thank you, moving on to the next issue with it.

I realized that ledcount was set to 10, not 9. Just fixed that.

A more traditional way of “fixing that” is to keep ledcount at 10 (if you really have 10 LEDs) and let the loop say

for (led=0; led[glow]<[/glow]ledcount; led++)

I realized that ledcount was set to 10, not 9

Imagine that you'd done:

const int ledcount=10; //number of LEDs to control
int red[ledcount];

And then changed "ledcount" to 9, without changing your "for" loops.