On Arduino mega, I would like to declare appropriate number of variables for analogue reading, depending on a value that I would store in 1 byte in EEPROM. These variable declarations can not be made inside the loop function but they must be available in the loop function. Therefore the variables need to be global. However, program constructs like the IF or SWITCH statements are not available outside a function (code block).
Has anyone encountered this situation before and solved it?
Any assistance or guidance is much appreciated.
Thanks!
Madhav
Demonstrative code I would have liked:
#define
#include
int Estr;
Get_Poles(); // call the function to read the EEPROM location for the parameter value, returns an integer
if (Poles == 1){
int LDR1 = 0; ...}
else if (Poles == 2) {...;}
(The if else construction may be replaced with switch construct)
void setup(){...;}
void loop(){...;}
int Get_poles(){
Estr = EEPROM.read(x,y);
return int(Estr);
...;}
If you declare the variables prior to setup(), they are global and any part of the software can access them.
I declare all of mine that way.
analogRead() returns an int, not a byte.
You will need to start with an array for your LDR variables instead of having individual variables.
int LDR[n];
...
...
void setup()
{
...
...
}
void loop()
{
...
...
}
You can make n big enough to cater for every possible number of LDRs. That is the safe way to do it and I would definitely use this approach.
If you like to live at the edge, the less safe way is to change the array to a pointer to an int
int *LDR;
and dynamically allocate the memory for the number of LDRs that you need.
int *LDR;
int Estr;
void setup()
{
pinMode(errorLedPin, OUTPUT);
Estr = Get_poles();
LDR = malloc(Estr * sizeof(int));
if (LDR == NULL)
{
// memory allocation failed, hang forever and flash led
for (;;)
{
digitalWrite(errorLedPin, HIGH);
delay(200);
digitalWrite(errorLedPin, LOW);
delay(200);
}
}
}
void loop()
{
}
You can treat the pointer just like an array.
Warning:
Dynamic allocation can be dangerous; it's safe to do it once-off like above but you need to make sure that you still have memory left to call functions, handle ISRs and for local variables. Dynamically allocated memory is not counted in the reported RAM usage of the IDE so the IDE will not be able to warn you about low memory. Don't come here if your code crashes unexpectedly when using dynamic memory allocation.
Questions
1)
Why does Get_poles return an int?
2)
Strange way of reading an eeprom in Get_poles(); does it work?
Let the String class manage the memory for you but prevent fragmentation by only adding to it once right at the start of setup() (and not declare anything before that)
#include <EEPROM.h>
#define ADDRESS 0
unsigned int addr=ADDRESS;
byte nrvalues;
String badidea="";
void setup() {
nrvalues=EEPROM.read(addr);
while ((addr/2)<nrvalues) {
badidea+=(char) EEPROM.read(addr+1);
badidea+=(char) EEPROM.read(addr+2);
addr=addr+2;
}
}
void loop() {}
unsigned int returnValue(byte valNr)
{
unsigned int msb=badidea.charAt(valNr*2);
unsigned int lsb=badidea.charAt(valNr*2+1);
return (msb<<8) | lsb;
}