Array multiple element selection ?

Hi dear community;

i have an array like this

int data[8]={5,3,6,4,7,9,8,2}

and i want to select multiple elements example 6 and 8 at once like this

Serial.println(data[2,6]);

and serial monitor show me 6 and 8 number in same line

is there a way to do this with array function ? if it possible, is there a way to make some operations with multiple elements of array like this;

Serial.println(data[0]+data[5]);

serial monitor shows me sum of these elements as 14 ?

i could not find any article on internet

how to do ?

thanks in advance

You can't this way :slight_smile:
In C you access to only one entry of the array at a time. So you would do a

Serial.print(data[0]);
Serial.println(data[5]);

You could, of course, write a function to print the value of multiple array elements

void printTwoEntries(byte a, byte b)
{
  Serial.print(data[a]);
  Serial.println(data[b]);
}

Call it like this

  printTwoEntries(0, 5)  //print elements 0 and 5
  printTwoEntries(1, 4)  //print elements 1 and 4

If you do not always want to print 2 entries then consider passing a binary value to the function and have the function print the values where there is a 1 in the associated bit of the binary variable

Thank you for ideas and quick replies to this Poor beginer =) some ideas occur in my mind about the functions by your helps i Will try. if you have different methods always welcome. Best regards

It would help to know why you want to do this and whether the number of elements to be selected will always be the same

Actually i want to make a bare led matrix (which is come without decade counter IC like Module MAX7219) programing on ARDUINO IDE complier, it should be an array "interface"

assume that there is a 3x3 led matrix led and i want to make an array "virtual interface" in complier screen like this

int MATRIX[3][3] =
{
{0,0,0},
{0,0,0},
{0,0,0},
}

My goal is index this code interface to real 3x3 led and when i change one of that 0 to 1
led turn on

i read some articles about column row scanning, arrays and i can run a led matrix with some functions but i want to simplify that

my only purposse is runing a led matrix with that array interface

int MATRIX[3][3] =

{
{0,0,0},
{0,0,0},
{0,0,0},
}

very complex for me :slight_smile: i could not figure out how to do

thanks for your helps and patience

kindly regards

Are you saying that the actual program code will be changed, compiled and uploaded to change the pattern or is user input be used to change the pattern ?

Yes sir
i have an arduino uno r3 board and a bare 8x8 led matrix "KPM-2088"
and i want to draw a pattern on led matrix using this code with ARDUINO IDE,

for example, if i want a write "Z", i want only change 0 to 1 like this

int data[8][8] = {
 {0,0,0,0,0,0,0,0},
 {0,1,1,1,1,1,1,0},
 {0,0,0,0,0,1,0,0},
 {0,0,0,0,1,0,0,0},
 {0,0,0,1,0,0,0,0},
 {0,0,1,0,0,0,0,0},
 {0,1,1,1,1,1,1,0},
 {0,0,0,0,0,0,0,0}
};

and rest of functions and codes use this array function to draw this pattern to led matrix.

:slight_smile:

Do you know how to lightup LEDs based on the array?

Yes sir, Actually i work on it, your advices give me some new ideas about functions, I will let you know about news and if i stuck while programming. if i success i will send last shape of project code,
Kindly regards and thanks for your ideas,
any other ideas are welcome.

if i want a write "Z", i want only change 0 to 1 like this

The first thing that I would do would be to declare the array as byte instead of int to save half the memory used

The next improvement would be to express the values as bits in an an array of bytes to save even more memory like this

byte pattern[] =
{
  0b00000000,
  0b01111110,
  0b00000100,
  0b00001000,
  0b00010000,
  0b00100000,
  0b01111110,
  0b00000000
};

Can you see how the pattern is in the bits of the bytes ?
That array uses 8 bytes of memory. The original one used 128 bytes.

You can use the value of each bit to set the state of the corresponding LED to show the pattern

To address individual elements in that array, you’ll learn about algorithms (or waste a lot of code & time pushing bits around.

here is a simple example using @UKHeliBob pattern that will print the shape in the Serial console (set @ 155200 bauds)

byte pattern[] =
{
  0b00000000,
  0b01111110,
  0b00000100,
  0b00001000,
  0b00010000,
  0b00100000,
  0b01111110,
  0b00000000
};

void setup() {
  Serial.begin(115200);
  for (byte line = 0; line < sizeof(pattern); line++) {
    for (char bit = 7; bit >= 0; bit--) {
      if (bitRead(pattern[line], bit) == 1) // https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitread/
        Serial.write('*'); // pixel is ON
      else
        Serial.write(' '); // pixel is OFF
    }
    Serial.println();
  }
}

void loop() {}

You’ll see I use the bitRead() macro to extract each bit from a line in the pattern

The console will show

[color=purple]        
 ****** 
     *  
    *   
   *    
  *     
 ****** 
        
[/color]

@J-M-L
Yesss thats it what i talking about.
Thank you for code sample sir =)
now i can adapting this sample for my project.

Thank you lord... =)