Go Down

Topic: Meeting a variable created in setup(). (Read 521 times) previous topic - next topic

albuino

I need to declare a double array (doubleArrayVariable[][]) which will be used in loop lots of times.
However I need to read a EEPROM value in setup() in order to define the array lenght.
The problem is that if I define it in setup(), then it won't be known in loop().
What could I do?

Arrch

Declare it outside of both (global).

albuino

But I don't know the size until setup().

Arrch


But I don't know the size until setup().


Then declare a global pointer and use malloc() in setup. Post your code to get advice that isn't just stabbing in the dark.

albuino

Quote

#include <EEPROM.h>
byte variable[][];
setup() {
int size1 = EEPROM.read(0);
int size2 = EEPROM.read(1);
// Here I already know the sizes.
}
loop() {
// Handling variable.
}

PaulS

Code: [Select]
byte **variable = NULL;

setup()
{
  int size1 = EEPROM.read(0);
  int size2 = EEPROM.read(1);
  // Here I already know the sizes.
  variable = (byte **)malloc(size1 * size2 * sizeof(byte);
}

void loop()
{
}
The art of getting good answers lies in asking good questions.

albuino

Very nice, I didn't know the malloc() funtion. The only doubt is that I am only used to managing double array by rows and columns. With this code how I know how many rows and columns there are?

Quote

byte **variable = NULL;
variable = (byte **)malloc(2 * 3 * sizeof(byte));

versus

byte variable[2][3];

guix

#7
Dec 20, 2012, 08:02 pm Last Edit: Dec 20, 2012, 09:38 pm by guix Reason: 1
There is only one dimension, but you can fake rows and colums with soemthing like that:
Code: [Select]

#define setValue( row, col, value ) variable[ col + ( row * size2 ) ] = value

setValue( 1, 2, 10 )
//equivalent to
variable[1][2] = 10;


Ok it's crap..

albuino

Ok, actually the number of columns is always known. So it is only necessary reading a value from the EEPROM memory.
Why doesn't this code work?

Code: [Select]

byte *variable[2] = NULL;

setup()
{
  int size = EEPROM.read(0);
  variable = (byte[2] *)malloc(size * sizeof(byte[2]));
}

void loop()
{
}

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

albuino


There is only one dimension, but you can fake rows and colums with soemthing like that:
Code: [Select]

#define setValue( row, col, value ) variable[ row + ( col * size2 ) ] = value

setValue( 1, 2, 10 )
//equivalent to
variable[1][2] = 10;


Ok it's crap..


Crap? Not at all. I really like it.

AWOL

No, it really is crap without semicolons  ;)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

guix

I made a mistake in my code, it's more like this:
Code: [Select]

variable[ col + ( row * size2 ) ]


johncc


Very nice, I didn't know the malloc() funtion. The only doubt is that I am only used to managing double array by rows and columns. With this code how I know how many rows and columns there are?

Does this help:
Code: [Select]

byte **variable = NULL;
int rows;
int cols;
void setup()
{
  rows = EEPROM.read(0);
  cols =  EEPROM.read(1);
  // Here I already know the sizes.
   variable = (byte **)malloc(rows * cols * sizeof(byte));
}

void loop()
{
  // "Handling variable" as you put it
   for (int r=0; r<rows; r++)
     for (int c=0; c<cols; c++) {
        variable[r][c] = 17;
     }
}



Go Up