Arduino array how values are stored, how this think is working

Hi, I need little help with array, because something I didn’t understand.

Lets say we have 3 potentiometers, or 3 led or buttons. And we need to store the state of those.

For example let’s say we need to store the present value of a potentiometer.

The array will be

Int potentiometer [3] = {potensiometer1, potensiometer 2, potensiometer 3,};

In void loop we have the code that is reading the potentiometer value.

int potensiometer1 = analogRead(A0);

int potensiometer2 = analogRead(A1);.

int potensiometer3 = analogRead(A1);

the questions is how this values gos get stored to the array. Is automatic stored because array has the same name, how thinks work please.

No

In the case of your example the array has 3 elements

potentioneter[0]
potentioneter[1]
potentioneter[2]

So you would save a value like this

int potensiometer1 = analogRead(A0);
potentiometer[0] = potensiometer1 ;

or you could do

potentiometer[0] = analogRead(A0);

so is mean that inside [ ] the number you insert is store the valuw to that index. 0, 1, 2,
if you dont specify index in the [ ] what will happend?

Try it and see

and as you try things out, enable all warnings in the preference panel and try to write in potentiometer[3] (ie beyond the bounds of the array) to see what happens as well.

Good learning opportunity

if you mean to add more index

potentiometer[3] = analogRead(A0);

ok it will be error

I know I know not everyone has Google or even knows it exists so I Googled it for you C++ Arrays

2 Likes

last index in 3 element array is 2

0,1,2 - 3 elements. Why don’t you read on arrays first?

read the thread first :slight_smile:

I was the one to suggest that he tries that as a learning opportunity.

@tasoscy properly referred to the right indexes

i read my friend. and i understand.

i did my home work, and your asnwers was help full, i write this code, is a vu meter, it takes the midi cc and acording it light the led.

because i have 2 deferent row of led in the max7219 that i want to light up, i end up with this code, and i will like your oppinion, i did it corect or wrong. code is working

2 thinks that are not clear in my mind is this.

i have an array with 12 index.

lets take case 2. my logic is, if CC is 2, then case 2 will activate. now i have a four loop.
when i tell that (int i=2; ) ( i < 9; ) is equal to 2 < 9 till here i got it.

for (int i = 2; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }

here when i declare (vul[i], false.) my logic is that (i ) is 2 and this (i) is transfer to (vul[i], and it call the index number 1, that is 1 and it light the led no1

the next loop the( i ) equal to 3. so it call index no3 that is no 2, so led no2 is light up
and i assume the loop is exit when 9 < 9; it stop here?

take a look in case 9.
my logic here is that because i need to light leds in row no4 , but leds in row no2 not to turn off

my question here is that (i) should be 9 too? or 5 how i did it.. because i only light up 4 leds from the row now 4

case 2:
            for (int i = 2; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 2; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            break;

          case 3:
            for (int i = 3; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 3; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            break;
#include "MIDIUSB.h"// Library from here https://github.com/FortySevenEffects/arduino_midi_library
#include "LedControl.h"

//pin 12 is connected to the DataIn
// pin 11 is connected to the CLK
// pin 10 is connected to LOAD
LedControl lc = LedControl(12, 11, 10, 1);
byte VU_MIDI_CH = 0;
byte vuLcc = 12;
int delaytime = 200;
unsigned int vul[12] = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3,};
unsigned int vul1[8] = {0, 1, 2, 3, 4, 5, 6, 7,};

int ccLastValue = 0;

//unsigned int Vuvaleu[7] = {0, 0, 0, 0, 0, 0, 0,};
void setup() {

  lc.shutdown(0, false);

  lc.setIntensity(0, 8);

  lc.clearDisplay(0);

  Serial.begin(115200);
  lc.setLed (0, 7, 1, true); // turn 1 led on to see if the code is working

  //all leds in row 2 are turn on one by one

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

    lc.setLed(0, 2, vul[i], true);
    delay(delaytime);
  }

  //all leds in row 6 turn on one by one

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

    lc.setLed(0, 6, vul1[i], true);
    delay(delaytime);
  }
  lc.clearDisplay(0);

}




void loop() {
  midiEventPacket_t rx = MidiUSB.read();

  switch (rx.header) {
    case 0:
      break; //No pending events

    //  case 0x9:
    //      handleNoteOn(
    //        rx.byte1 & 0xF,  //channel
    //        rx.byte2,        //pitch
    //        rx.byte3         //velocity
    //      );
    //      break;

    //    case 0x8:
    //      handleNoteOff(
    //        rx.byte1 & 0xF,  //channel
    //        rx.byte2,        //pitch
    //        rx.byte3         //velocity
    //      );
    //      break;

    case 0xB:
      vumeter(
        rx.byte1 & 0xF,  //channel
        rx.byte2,        //cc
        rx.byte3         //value
      );
      break;


  }
}

void vumeter(byte channel, byte number, byte value) {

  int value_ = value;

  if (value_ != ccLastValue) {

    if (channel == VU_MIDI_CH) {

      if (number == vuLcc ) {


        switch (value_) {
//led in row 2 are light acording to the case
          
          case 0:

            for (int i = 0; i < 12; i++) {
              lc.setLed(0, 2, vul[i], false);
              lc.setLed(0, 4, vul[i], false);
            }

            break;

          case 1:
            for (int i = 1; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 1; i++) {
              lc.setLed(0, 2, vul[0], true);
            }
            break;


          case 2:
            for (int i = 2; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 2; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            break;

          case 3:
            for (int i = 3; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 3; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            break;

          case 4:

            for (int i = 4 ; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 4; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            break;

          case 5:
            for (int i = 5; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 5; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            break;

          case 6:
            for (int i = 6; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 6; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            break;

          case 7:
            for (int i = 7; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 7; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            break;

          case 8:
            for (int i = 8; i < 9; i++) {
              lc.setLed(0, 2, vul[i], false);
            }
            for (int i = 0; i < 8; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            break;

            // led in the row 4 are light acording to the case, but leds in the row 2 should stay on

          case 9:
            for (int i = 0; i < 9; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            for (int i = 0; i < 5; i++) {
              lc.setLed(0, 4, vul[i], false);
            }
            for (int i = 0; i < 0; i++) {
              lc.setLed(0, 4, vul[i], true);
            }
            break;

          case 10:
            for (int i = 0; i < 9; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            for (int i = 1; i < 9; i++) {
              lc.setLed(0, 4, vul[i], false);
            }
            for (int i = 0; i < 1; i++) {
              lc.setLed(0, 4, vul[i], true);
            }
            break;

          case 11:
            for (int i = 0; i < 9; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            for (int i = 2; i < 9; i++) {
              lc.setLed(0, 4, vul[i], false);
            }
            for (int i = 0; i < 2; i++) {
              lc.setLed(0, 4, vul[i], true);
            }
            break;

          case 12:
            for (int i = 0; i < 9; i++) {
              lc.setLed(0, 2, vul[i], true);
            }
            for (int i = 3; i < 9; i++) {
              lc.setLed(0, 4, vul[i], false);
            }
            for (int i = 0; i < 3; i++) {
              lc.setLed(0, 4, vul[i], true);
            }
            break;
        }
        ccLastValue = value;
      }
    }
  }
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}


void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);

}

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

I don't know the LedControl library - if it works ...

i will go from 2 to 8 inclusive when the for loop runs and thus when i is 2, you'll access vol[2] which is the third element in the array.

When i is 2 the value at that location in the vul array 2, not 1

unsigned int vul[12] = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3,};

Remember that the array index start at 0, not 1

The vul1 array looks redundant because the value at index 0 is 0, at index 1 is 1, at index 2 is 2 and so on so you might just as well use the index values as the value in the array

yes i think i understand it, i assume i didnt write it correct in post.

index 0 is no 1 when you call index 0 and index 1 is no 2 when you call index 1?

this is the correct

to my eyes, is working perfect so far, i dont know, if my code is been write in the correct way considering the case fuction..

i dont know if i use the correct logic to over come the problem, when leds in deferend row will need to start to blink.

if this is what you mean:

  • index 0 is the first entry of the array
  • index 1 is the second entry of the array
  • index 2 is the third entry of the array
  • ...

then you are correct

this is how i understand it

mr ukhelibob said

When i is 2 the value at that location in the vul array 2, not 1
he confuse me.

o i think here i miss understand.

there is no (i=0)

i=1 is mean it call index 1, with the value of 0
i=2 is mean it call index 2 with value 1

is this bellow corect? if (i=0) i think nothink will happend.

because (i=0; 0 < 0; i++) nothing happen, am i correct??

case 9:
            for (int i = 0; i < 9; i++) {
              lc.setLed(0, 0, vul[i], true);
            }
            for (int i = 0; i < 9; i++) {
              lc.setLed(0, 1, vul[i], false);
            }
            for (int i = 0; i < 0; i++) {
              lc.setLed(0, 1, vul[i], true);
            }
            break;

@UKHeliBob is correct

the value at index 2 (the third entry) is 2
or in C++ vul[2) == 2 ➜ is true

Try this

void setup()
{
  Serial.begin(115200);
  byte array[] = {100, 101, 102, 103};
  for (int c = 0; c < 4; c++)
  {
    Serial.print("At index ");
    Serial.print(c);
    Serial.print(" the value is ");
    Serial.println(array[c]);
  }
}

void loop()
{
}