Hi, i'm trying to optimize my code squeezing out some bytes keeping under 97% of total memory usage on my Arduino Uno Rev.3
I have some places in my code where i need to construct a string with some parameters to be sent as char array and i was using sprintf for that reason but i've noticed that everytime i use a sprintf i loose memory.
Right now i have this :
sprintf(tweetText, "Avvio di Arduino completato in ms : %d",millis());
tweet(tweetText);
tweetText is a function of twitter library for arduino, i'll post it here just in case :
void tweet(char msg[])
{
//Serial.println("connecting ...");
if (twitter.post(msg))
{
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
// int status = twitter.wait(&Serial);
int status = twitter.wait();
if (status == 200)
{
//Serial.println("OK.");
}
else
{
//Serial.print("failed : code ");
//Serial.println(status);
}
}
else
{
//Serial.println("connection failed.");
}
// Now we clear our array
//memset( tweetText, 0, sizeof(tweetText) );
}
I was tring to optimize the code doing something like this but i'm not sure i'm doing it right :
void prepara_tweet()
{
if (case_sprintf == 1) msgtweet = "Avvio di Arduino completato in ms :" + millis();
int str_len = msgtweet.length() + 1;
char tweetText[str_len];
msgtweet.toCharArray(tweetText,str_len);
Serial.print("tweet text = ");
Serial.println(tweetText);
tweet(tweetText);
case_sprintf = 0;
}
case_sprintf is a variable i created where i store a number from 1 to x so i can then do i if case_sprintf == x then msgtweet = this or that
Optimizing code is far more easier with access to the complete code. I can only give hints upon assumptions or generic ones and that is not very efficient. Please post your whole code (or attach it)
from the book of open doors:
use short messages
use the F macro where possible
find repeating code and make functions out of it (optionally with parameters)
I can post the whole code but you will notice it is very big indeed and the parts of interest are those i posted.
Anyway in attachment the whole code.
Thanks in advance for your help.
Trying to optimise code is a diminishing returns thing. First time you look over it, you might re-factor a substantial portion and save a fair bit, but if still can't get enough space then eventually you've just got to accept you've outgrown your platform - look at ArduPilot. There's plenty of other Arduino and MCU proto-board products around and if you were to switch to a more power powerful MCU you could spend more time being productive rather than wondering how to scrimp and save a few bytes here and there.
Camel:
Trying to optimise code is a diminishing returns thing. First time you look over it, you might re-factor a substantial portion and save a fair bit, but if still can't get enough space then eventually you've just got to accept you've outgrown your platform - look at ArduPilot. There's plenty of other Arduino and MCU proto-board products around and if you were to switch to a more power powerful MCU you could spend more time being productive rather than wondering how to scrimp and save a few bytes here and there.
thanks for the info, i'm gonna look for that.
Anyways, it's interesting trying to optimize the code cause you learn many things and you have to try everything possible to gain memory so ...it's not that bad ... but as you said ..better spending time being productive.
In addition to code size savings, you get buffer overrun protection and easy syntax with StaticString.
The first example is what i'm doing in my code, i'll check but it seams like that.
I have never used etk, what's that for and why do you put ss at the end of etk statement?
Does that mean it will always have a fixed value? I need to put milliseconds at the end of the message because twitter doesnt allow to send twice the same message.
Thanks for the nice and neat example and for the help.
An OT question related to your blog, ..you're on WP, but what do you use to show so nicely your code? Is it a plugin? Can i Know the name? Thanks...now i have alot to study! I think this rope is what i needed!
An OT question related to your blog, ..you're on WP, but what do you use to show so nicely your code? Is it a plugin? Can i Know the name? Thanks...now i have alot to study! I think this rope is what i needed!
Thanks! It is a plugin and it's called the 'Crayon Syntax Highlighter'.
sprintf() is a powerful function, but most of the time it's overkill because you don't use all of the features supported by the function. The code below only uses 616 bytes of code: