Arduino Forum

Using Arduino => Programming Questions => Topic started by: BassTeQ on Nov 29, 2014, 11:08 pm

Title: Low on memory any ideas?
Post by: BassTeQ on Nov 29, 2014, 11:08 pm
Hi all, running a little low on memory on my Uno, looking at the code -> http://codepad.org/XY494JZ8 (http://codepad.org/XY494JZ8), is there anything that could be changed to help free some up?

The available free memory is output every 6 seconds along with the results of the ping.

Appreciate any assistance, thanks.
Title: Re: Low on memory any ideas?
Post by: KenF on Nov 29, 2014, 11:15 pm
First trick.  Wherever you have print("something in inverted commas")
replace it with print(F("something in inverted commas"));
Title: Re: Low on memory any ideas?
Post by: AWOL on Nov 29, 2014, 11:19 pm
What's the point of StrContains?

What's wrong with strstr_P?
Title: Re: Low on memory any ideas?
Post by: UKHeliBob on Nov 29, 2014, 11:22 pm
It's not much, but do all of these variables need to be ints or could some/all be bytes ?
Code: [Select]
const int FanSwitchPIN = 7;
const int RebootPIN = 8;
const int TempSensorPIN = 0;

int buttonState = 0;
int TempSensor = 0;
int BadPingCnt = 0;
int PCReset = 0;

The pin numbers obviously don't.  Are there any local variables in functions that could be declared as smaller data types ?
Title: Re: Low on memory any ideas?
Post by: Riva on Nov 30, 2014, 08:42 am
Does your linked code work?
Code: [Select]

Serial.println("setup loop");
Ethernet.begin(mac, ip);
server.begin();           // start to listen for clients
Serial.begin(9600);

I did not realize you could start printing to serial before it's initialized?
Title: Re: Low on memory any ideas?
Post by: joebert on Nov 30, 2014, 05:38 pm
I'd re-evaluate the use of "printf / sprintf", and whether their convenience is worth allocating 12.5% of my memory to "buffer".
I'm guessing "__FlashStringHelper*" errors are why the F() macro isn't being used inside sprintf lines, which ties up another 55+ bytes for storing the templates for those sprintf calls.



"PingTime" is overflowing, it has room for "Reboot". Maybe you meant to use "buffer" instead?

Code: [Select]
printf(PingTime,"%s", "Rebooting");



It looks like the main issue comes down to output buffering being anticipated, but strayed away from. So that sizable "buffer" variable, combined with smaller redundant variables designed to improve readability, but eventually leading to variable confusion like what happened in "PingTime", ends up eating more memory than is needed.
Title: Re: Low on memory any ideas?
Post by: BassTeQ on Dec 01, 2014, 11:15 am
Thanks all for the replies, yes the code does work.
I'll try as suggested and reply with the outcome next week.

Much appreciated.