Only update some numbers in an array?

lets say i have an array of numbers, 0 through 49, that can have any number of values assigned to them, each different.

If the array “space” is 0-10, 14, or 32-40 I want to take the existing variable in the array, and add 1 to it…

How would i do that?

something like

for(i=0, i<50, i++)
if(0 <= i <= 10 || i == 14 || 32<=i<=40) {
  valuearray[i] = valuearray[i] + 1;
}

would that do it? If yes, is that the best way?

if(0 <= i <= 10 && i == 14 && 32<=i<=40)

You can’t do the this. 0<= i <= 10. It doesn’t work that way. 0<=i will be true or false, which will always be less than 10.

Also those ANDs (&&) need to be ORs (||) since there is no way possible that i can be 14 AND be greater than 32 at the same time.

You need

if(0 <= i || i <= 10 || i == 14 || 32<=i || i<=40)

Pardon the discrepancy, but the post appears to have changed while I was writing my reply

Yes, sorry delta, I realized my error before (or vs and) before i got a reply.

Thanks for the other information though, that helps! Is that the best way to do it though?

0<=i
forget the ORs That condition alone will make it EVERY item in the array.

I was also thinking, I would like to be able to change which values of the array are updated, based on a condition.

eg,

if i am doing zone 1, only change the first 10 variables of the array, if i am doing zone 2, only change the second 10 variables of the array, if i am doing zone 3, only change the third 10 variables of the array, if i am doing zone 4, only change the last 20 variables of the array, but if i am in zone 1 and 3, change the first 10 and the third 10 variables of the array.

I might be able to make different arrays for each zone, then just say "update these arrays", but then i'd need a way to combine the arrays.

Oops. Missed that. Yeah you need something more like…

if( (i >= 0 && i<=10) || (i==14) || (i >=32 && i<=40) )

Qdeathstar: I was also thinking, I would like to be able to change which values of the array are updated, based on a condition.

eg,

if i am doing zone 1, only change the first 10 variables of the array, if i am doing zone 2, only change the second 10 variables of the array, if i am doing zone 3, only change the third 10 variables of the array, if i am doing zone 4, only change the last 20 variables of the array, but if i am in zone 1 and 3, change the first 10 and the third 10 variables of the array.

I might be able to make different arrays for each zone, then just say "update these arrays", but then i'd need a way to combine the arrays.

What are these "zones" you speak of? Maybe you should post the rest of the code.

You can use pointer math to break a larger array into smaller ones and still keep the larger one intact.

Something like

byte bigArray[50];

byte* smallerArray = bigArray + 10;

now saying smallerArray[3] would be the same thing as saying bigArray[13] They now point to the same location in memory.

For your zone example
for (i=zone10; i<zone10+10;i++)
{value*=stuff;*
*} *

I would post my code, but it doesn’t have any thing dealing with the breaking of the arrays yet.

The zones are sections of an LED strip.

I am making different functions that light up the strip, but my issue is I can have two different zones doing the same function, but because the function needs to be able to count to get to the next led, I need to be able to “skip” led’s that aren’t running under that function.

Here is my code as it sits, if you want to look at it.

For your zone example
for (i=zone*10; i<zone*10+10;i++)
  {value=stuff;
 }

What does that do please?

_6803_LED_box1.ino (16.5 KB)

Qdeathstar:
Here is my code as it sits, if you want to look at it.

For your zone example

for (i=zone10; i<zone10+10;i++)
 {value=stuff;
}



What does that do please?
//The "value = stuff;" was a prompt for you to put in whatever you want to do to those members that you want.  
//Maybe 
valuearray[i]++;// to increment it by 1
//or
 valuearray[i]--;// to decrement it by 1
//or
 valuearray[i] = !valuearray[i] ;// to toggle it between 1 and 0
//or
valuearray[i] = valuearray[i]^(digitalRead(5)*myShoeSize);

//The world is your oyster.

:slight_smile:

Sorry, i meant what are the asterisks doing?

for (i=zone10; i<zone10+10;i++)

what does that do in english?
like

for(i=0, i < 10, i++) {

in english is “for i, starting at zero, while i is less than 10, increment i”…

Those mean multiply.

In english, for i equal zone times ten, while i is less than zone times ten plus ten, increment i.

ah, I was thinking some weird operator pointer thing… thanks.

While there might be a little more typing I think the logic would be easier if you use two arrays.

The main array (myArrayA)has elements 0 - 49, but some of the elements are not used (say only 20 elements are used)

The second array has 20 elements and the contents of those elements are the indices to the active elements in the other array. For example myArrayB[20] = {1,2,3,4,5,6,7,8,9,10,14,32,33 — etc}

Then to iterate through the active elements in myArrayA you could do

for (n = 0, n < 20, n++) {
    myArrayA [myArrayB [n] ] = newValue;
}

If you want other groupings of the elements in myArrayA you could create other arrays such as myArrayC and myArrayD

…R

Altnernative and more readable(?) approach:

bool updatable (byte index)
{
  if (i <= 10) return true ;
  if (i == 14) return true ;
  if (i >= 32 && i <= 40) return true ;
  return false ;
}

...
for (byte i=0, i<50, i++)
  if (updatable (i))
    valuearray [i] ++ ;

By separating the code for determining if an index is to be updated into
a function of its own you make the for-loop much simpler and clearer
to read.

You also name the function which makes the code more self-explanatory.

Should you be doing this same test in several places then the function also
serves to avoid duplication of code. Code duplication loads to many bugs
when a program is later changed and only some of the copies get updated
and others overlooked, leading to inconsistencies - ensuring the code is in
one place prevents this kind of error from arising.

I’m a little skeptical that you actually want 0…10 and 32…40 ranges inclusive.
Did you mean 0…9 and 32…39 ?

MarkT: Altnernative and more readable(?) approach:

Neat idea, but is not equivalent to my code in Reply #14.

However I have no idea which of the two best represents what the OP wants.

...R

Thank you for the replies!

I have attached a picture in which i have tried to lay out what i hope to accomplish.

So then, mark, if I used your example, i think i’d have to make a modification like this:

bool WhiteFunctionForEachPixel (byte icNumber)
{ 
  
  if (i <= LastZone1Pixel && zone1Fuction == 0) return true ;   // zero represents the "white" function
  if (i > LastZone1Pixel && i <= LastZone2Pixel && zone2Fuction == 0) return true ;
  if (i > LastZone2Pixel && i <= LastZone3Pixel && zone3Fuction == 0) return true ;
  if (i > LastZone3Pixel && i <= LastZone4Pixel && zone4Fuction == 0) return true ;
  return false ;
}

but i’d have to make a function like this for every led show function i have, eg

bool GreenFunctionForEachPixel (byte icNumber)
{ 
  
  if (i <= LastZone1Pixel && zone1Fuction == 1) return true ;   // one represents the "green" function
  if (i > LastZone1Pixel && i <= LastZone2Pixel && zone2Fuction == 1) return true ;
  if (i > LastZone2Pixel && i <= LastZone3Pixel && zone3Fuction == 1) return true ;
  if (i > LastZone3Pixel && i <= LastZone4Pixel && zone4Fuction == 1) return true ;
  return false ;
}

///

actually, this should work, shouldn’t it?

bool FunctionForEachPixel (byte icNumber, byte function)
{ 

  if (i <= LastZone1Pixel &&  function == zone1Fuction) return true ;
  if (i > LastZone1Pixel && i <= LastZone2Pixel && function == zone2Fuction) return true ;
  if (i > LastZone2Pixel && i <= LastZone3Pixel && function == zone3Fuction) return true ;
  if (i > LastZone3Pixel && i <= LastZone4Pixel && function == zone4Fuction) return true ;
  return false ;
}

...


void white(){
  
  for (byte i=0; i<NUM_LEDS; i++) {
    if (FunctionForEachPixel (i, 0)){
      leds[i].r = 255; 
      leds[i].g = 255; 
      leds[i].b = 255;
    }
  }
}

void green(){
  
  for (byte i=0; i<NUM_LEDS; i++) {
    if (FunctionForEachPixel (i, 1)){
      leds[i].r = 0; 
      leds[i].g = 255; 
      leds[i].b = 0;
    }
  }
}

_6803_LED_box3.ino (16.2 KB)

hmm, that doesn’t work…

function will get a different value every time it goes through a color-function like “white” or “green”

I’m back to needing a test for every color-function…

I changed the code to something like this

bool FunctionForEachPixel (byte icNumber)
{ 
  if (icNumber <= LastZone1Pixel &&  function == zone1Function) return true ;
  if (icNumber > LastZone1Pixel && icNumber <= LastZone2Pixel && function == zone2Function) return true ;
  if (icNumber > LastZone2Pixel && icNumber <= LastZone3Pixel && function == zone3Function) return true ;
  if (icNumber > LastZone3Pixel && icNumber <= LastZone4Pixel && function == zone4Function) return true ;
  return false ;
}


void white(){
  function = 0;
  for (byte i=0; i<NUM_LEDS; i++) {
    if (FunctionForEachPixel (i)) {
    leds[i].r = 255; 
    leds[i].g = 255; 
    leds[i].b = 255;
    }
  }
}

void custom(){
   function = 1;
   for (byte i=0; i<NUM_LEDS; i++) {
    if (FunctionForEachPixel (i)) {
      leds[i].r = Red; 
      leds[i].g = Green; 
      leds[i].b = Blue;
    }
  }
}

but if my program calls both functions “white” and “custom”, function is changed each time they are called (and they are called a lot of times per second)…

_6803_LED_box2.ino (16.8 KB)