Using arrays with two variables

Hi all,

Hoping someone can help me (a novice coder) out with arrays.

I’m wanting to call/display values based off a profile and switch number - in essence:

const int profile0[8]= {110.00, 120.00, 130.00, 140.00, 150.00, 160.00, 170.00, 180.00};
const int profile1[8]= {110.10, 120.10, 130.10, 140.10, 150.10, 160.10, 170.10, 180.10};
const int profile2[8]= {110.20, 120.20, 130.20, 140.20, 150.20, 160.20, 170.20, 180.20};

I wish to be able to switch between ‘profiles’ and then display the data within.

Normally arrays are in the form arrayName[int] for this but I want to be able to have something like

profile[profileNumber][switchNumber] - is that even possible somehow??

I could then use a loop/sub-routine like this: (made more complicated by the fact I want to add flexibility to truncate data based off a limit set by a variable ‘sensorsUsed’)

void printProfileSettings(){
    Serial.print("Profile ");
    Serial.print(profileNumber);
    Serial.println(" set");
    for (switchNumber = 0; switchNumber < sensorsUsed; switchNumber++){
      if (switchNumber < (sensorsUsed-2)){
      Serial.print(profile[profileNumber][switchNumber]); 
      }
    if (switchNumber == (sensorsUsed-1){
      Serial.println(profile[profileNumber][switchNumber]); 
      }
    }
}

BUT obviously that won’t work as the issue is arrayName[int][int] which can’t happen.

Can anyone give me some tips or ideas please :slight_smile:

Thanks

I suspect you may have also misunderstood the term “int”.

They don’t hold fractions.

It’s a contraction of “integer”

const int profile [][8] = {{....},{....}
};

Matra:
BUT obviously that won't work as the issue is arrayName[int][int] which can't happen.

Why do you think it can't happen? It's just a two-dimensional array. They're very common.

const int SensorsUsed = 8;


const float Profiles[][SensorsUsed] =
{
  {110.00, 120.00, 130.00, 140.00, 150.00, 160.00, 170.00, 180.00},
  {110.10, 120.10, 130.10, 140.10, 150.10, 160.10, 170.10, 180.10},
  {110.20, 120.20, 130.20, 140.20, 150.20, 160.20, 170.20, 180.20}
};


void printProfileSettings(int profileNumber)
{
  Serial.print("Profile ");
  Serial.print(profileNumber);
  Serial.println(" set");
  for (int switchNumber = 0; switchNumber < SensorsUsed; switchNumber++)
  {
    Serial.print(Profiles[profileNumber][switchNumber]);
    if (switchNumber < SensorsUsed - 1)
      Serial.print(", ");
  }
  Serial.println();
}

johnwasser:

const int SensorsUsed = 8;

const float Profiles[SensorsUsed] =
{
  {110.00, 120.00, 130.00, 140.00, 150.00, 160.00, 170.00, 180.00},
  {110.10, 120.10, 130.10, 140.10, 150.10, 160.10, 170.10, 180.10},
  {110.20, 120.20, 130.20, 140.20, 150.20, 160.20, 170.20, 180.20}
};

void printProfileSettings(int profileNumber)
{
  Serial.print(“Profile “);
  Serial.print(profileNumber);
  Serial.println(” set”);
  for (int switchNumber = 0; switchNumber < SensorsUsed; switchNumber++)
  {
    Serial.print(Profiles[profileNumber][switchNumber]);
    if (switchNumber < SensorsUsed - 1)
      Serial.print(", ");
  }
  Serial.println();
}

Got it!

I see where I went wrong, thanks :slight_smile:

(also thanks for improving the loop! I see what you did and it’s way more efficient that what I did)