chipKIT® Development Platform

Inspired by Arduino™

arrays

Created Mon, 09 Apr 2012 21:32:06 +0000 by scoprire


scoprire

Mon, 09 Apr 2012 21:32:06 +0000

A question from a noobie

i have a code with 3 arrays, i have to increase the index of the array when i push a button (one botton for one array)

const int bottone_rosso = 9;
const int bottone_blu = 10;
const int bottone_temp = 11;

int bot_rosso = 0;
int bot_blu = 0;
int bot_temp = 0;

int temperature_Rosso[] = {80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130};  
int temperature_Blu[] = {50, 55, 60, 65, 70, 75, 80, 85, 90};
char temp_unit[] = {C, F}; 

int rosso
int blu
char temp

void setup()
{
 pinMode(bottone_rosso, INPUT);
  pinMode(bottone_blu, INPUT);
  pinMode(bottone_temp, INPUT);
}

void loop()
{
  bot_rosso = digitalRead(bottone_rosso);
  bot_blu = digitalRead(bottone_blu);
  bot_temp = digitalRead(bottone_temp);
  
  if (bot_rosso == HIGH)
  {
   i = i++;
   rosso = temperature_Rosso[i];
   serial.print(rosso);
   delay(1000);
}
}

but i don't know how initialize the other index because the if i use "i" i increase the index of the first array.. i can use pointers? if yes, how? thanks


EmbeddedMan

Mon, 09 Apr 2012 21:50:41 +0000

The easiest thing would be to figure out how many elements you need in the array total at compile time, and then allocate that much space. For example, if you needed 1000 elements, you would do:

int temperature_Rosso[1000];

Then you can fill the array with values as you run.

Yes, you can do it with malloc() and pointers, but if you can bound the number of elements you'll need at compile time, it will be much simpler code.

*Brian


scoprire

Mon, 09 Apr 2012 22:23:07 +0000

i solved saving the index of every array in one variable and initializing the index with the value of the variable before increase the value

const int bottone_rosso = 11;
const int bottone_blu = 12;
const int bottone_temp = 13; 

int i = 0;
int ir = 0;
int ib = 0; 
int it = 0;

int bot_rosso = LOW;
int bot_blu = LOW;
int bot_temp = 0;

int temperature_Rosso[] = {80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130};  
int temperature_Blu[] = {50, 55, 60, 65, 70, 75, 80, 85, 90};
char temp_unit[] = { 'C', 'F'}; 

int rosso;
int blu;
void setup()
{
}
void loop()
{
bot_rosso = digitalRead(bottone_rosso);
  bot_blu = digitalRead(bottone_blu);
  bot_temp = digitalRead(bottone_temp);
  
  while (bot_rosso == HIGH)
  {
    i = ir;
    i = ++i;
    if (i == 11)
    {
      i = 0;
    }
    rosso = temperature_Rosso[i];
    Serial.write(17);
    Serial.write(22);
    Serial.write(128);
    Serial.print("                ");
    Serial.write(148);
    Serial.print("                ");
    Serial.write(134);
    Serial.print(rosso);
    ir = i;
    delay(500);
    bot_rosso = digitalRead(bottone_rosso);
  }
  
  while (bot_blu == HIGH)
  {
    i = ib;
    i = ++i;
    if (i == 9)
    {
      i = 0;
    }
    blu = temperature_Blu[i];
    Serial.write(17);
    Serial.write(22);
    Serial.write(128);
    Serial.print("                ");
    Serial.write(148);
    Serial.print("                ");
    Serial.write(134);
    Serial.print(blu);
    ib = i;
    delay(500);
    bot_blu = digitalRead(bottone_blu);
  }
  
  while (bot_temp == HIGH)
  {
    i = it; 
    if (i == 0)
    {
      i = 1;
    }
    else
    {
      i = 0;
    }
    temp = temp_unit[i];
    Serial.write(17);
    Serial.write(22);
    Serial.write(128);
    Serial.print("                 ");
    Serial.write(148);
    Serial.print("                 ");
    Serial.write(135);
    Serial.print(temp);
    it = i;
    delay(500);
    bot_temp = digitalRead(bottone_temp);
  }