Hi
I'm coding my first ever arduino project and stumbled upon something weird.
As far as I know, the variables inside a function are not global. But when I verify a sketch with an extra function (other than setup and loop), the global variables ge up.
When I comment out the code inside that extra function, the global variables go down again.
It's problematic because the function first gets a random number between 0-20, and then outputs text to a 16x2 LCD, with the text being different for every number. Getting rid of that function saves me 30% of global variables.
Am i misunderstanding global/local variants or is it just the IDE software that's being wrong.
Thanks!
This code uses: 5514 bytes (17%) program memory, global variables use 547 bytes (26%) of dynamic memory, 1501 bytes left for local variables.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4 ,5, 6, 7, 3, POSITIVE);
void RandomizerThingy();
void setup(){
lcd.begin(16,2);
lcd.clear();
Serial.begin(115200);
}
void loop(){
//code here
RandomizerThingy();
// OTHER CODE
}
void RandomizerThingy() {
randomSeed(analogRead(0));
int randNumber = random(20);
switch (randNumber) {
case 0:
Msg(lcd, "Something", "Something", 5000);
break;
case 1:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print (" S");
delay(500);
lcd.print ("O");
delay(500);
lcd.print ("M");
delay(500);
lcd.print ("E");
delay(500);
lcd.print ("T");
delay(500);
lcd.print ("H");
delay(500);
lcd.setCursor(0, 0);
lcd.print (" I");
delay(500);
lcd.print ("N");
delay(500);
lcd.print ("G");
delay(2000);
break;
case 2:
Msg(lcd, "NOPE NOPE NOPE", " NOPE NOPE NOPE", 5000);
break;
case 3:
Msg(lcd, "Something", "Something", 5000);
break;
case 4:
Msg(lcd, "Something", "Something", 5000);
break;
case 5:
Msg(lcd, "Something", "Something", 5000);
break;
case 6:
Msg(lcd, "Something", "Something", 5000);
break;
case 7:
Msg(lcd, "Something", "Something", 5000);
break;
case 8:
Msg(lcd, "Something", "Something", 5000);
break;
case 9:
Msg(lcd, "Something", "Something", 5000);
break;
case 10:
Msg(lcd, "Something", "Something", 5000);
break;
case 11:
Msg(lcd, "Something", "Something", 5000);
break;
case 12:
Msg(lcd, "Something", "Something", 5000);
break;
case 13:
Msg(lcd, "Press button", "when light is on", 0);
randNumber = random(20);
delay(randNumber);
int LEDpin = 1;
pinMode (LEDpin, OUTPUT);
digitalWrite(LEDpin, HIGH);
delay(100);
digitalWrite(LEDpin, LOW);
Msg(lcd, "Too slow", "!!!", 5000);
break;
case 14:
Msg(lcd, "Something", "Something", 5000);
break;
case 15:
Msg(lcd, "Something", "Something", 5000);
break;
case 16:
Msg(lcd, "Something", "Something", 5000);
break;
case 17:
Msg(lcd, "Something", "Something", 5000);
break;
case 18:
Msg(lcd, "Something", "Something", 5000);
break;
case 19:
Msg(lcd, "Something", "Something", 5000);
case 20:
Msg(lcd, "Something", "Something", 5000);
break;
}
return;
}
// A helper function to display messages of a specified duration
void Msg(LiquidCrystal_I2C &lcd, const char *top, const char *bottom, unsigned long del)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(top);
lcd.setCursor(0, 1);
lcd.print(bottom);
delay(del);
}
This code (when commented out all code in the randomizer function) uses: 4038 bytes (12%) program memory, global variables use 439 bytes (21%) of dynamic memory, 1609 bytes left for local variables.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4 ,5, 6, 7, 3, POSITIVE);
void RandomizerThingy();
void setup(){
lcd.begin(16,2);
lcd.clear();
Serial.begin(115200);
}
void loop(){
//code here
RandomizerThingy();
// OTHER CODE
}
void RandomizerThingy() {
/*
randomSeed(analogRead(0));
int randNumber = random(20);
switch (randNumber) {
case 0:
Msg(lcd, "Something", "Something", 5000);
break;
case 1:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print (" S");
delay(500);
lcd.print ("O");
delay(500);
lcd.print ("M");
delay(500);
lcd.print ("E");
delay(500);
lcd.print ("T");
delay(500);
lcd.print ("H");
delay(500);
lcd.setCursor(0, 0);
lcd.print (" I");
delay(500);
lcd.print ("N");
delay(500);
lcd.print ("G");
delay(2000);
break;
case 2:
Msg(lcd, "NOPE NOPE NOPE", " NOPE NOPE NOPE", 5000);
break;
case 3:
Msg(lcd, "Something", "Something", 5000);
break;
case 4:
Msg(lcd, "Something", "Something", 5000);
break;
case 5:
Msg(lcd, "Something", "Something", 5000);
break;
case 6:
Msg(lcd, "Something", "Something", 5000);
break;
case 7:
Msg(lcd, "Something", "Something", 5000);
break;
case 8:
Msg(lcd, "Something", "Something", 5000);
break;
case 9:
Msg(lcd, "Something", "Something", 5000);
break;
case 10:
Msg(lcd, "Something", "Something", 5000);
break;
case 11:
Msg(lcd, "Something", "Something", 5000);
break;
case 12:
Msg(lcd, "Something", "Something", 5000);
break;
case 13:
Msg(lcd, "Press button", "when light is on", 0);
randNumber = random(20);
delay(randNumber);
int LEDpin = 1;
pinMode (LEDpin, OUTPUT);
digitalWrite(LEDpin, HIGH);
delay(100);
digitalWrite(LEDpin, LOW);
Msg(lcd, "Too slow", "!!!", 5000);
break;
case 14:
Msg(lcd, "Something", "Something", 5000);
break;
case 15:
Msg(lcd, "Something", "Something", 5000);
break;
case 16:
Msg(lcd, "Something", "Something", 5000);
break;
case 17:
Msg(lcd, "Something", "Something", 5000);
break;
case 18:
Msg(lcd, "Something", "Something", 5000);
break;
case 19:
Msg(lcd, "Something", "Something", 5000);
case 20:
Msg(lcd, "Something", "Something", 5000);
break;
}
return;
*/
}
// A helper function to display messages of a specified duration
void Msg(LiquidCrystal_I2C &lcd, const char *top, const char *bottom, unsigned long del)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(top);
lcd.setCursor(0, 1);
lcd.print(bottom);
delay(del);
}
This is just a small part of my code as an example. In my (almost) final code with more functions, it's more like 88% of dynamic memory that's used by global variables.