Text overflowed and Serial.print()

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Morning, 

 

I tried burning Arduino code to an ATTINY841, which gave an 'text overflowed by xxx bytes' error.

The code is pretty tiny, but it uses a lot of Serial.print() functions.

I understand these functions, not recommended by the community in the first place, use up the SRAM.

 

As a solution I read to replace these with Serial.print(F("text")).

I applied this for about ten of my String functions, but the amount of bytes overflowed seem to increase using this method.

 

Does anyone have a suggestion what to use to print plain text and variables, to avoid this issue?

 

Many thanks, 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sounds like you tried compiling and got the error. Whilst you say your code is tiny - looks can be deceptive. How much is it overflowing by and have you tried to trim back your code - eg commenting out sections to see what causes the overflow?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It was overflowing by about 2500 bytes, so a lot.

I commented out an entire section that consisted out of Serial.print(), and isn't completely necessary.

It is now overflowing by 950 bytes.

 

Cannot delete anything else or make it more efficient.

Last Edited: Sun. Jan 19, 2020 - 12:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you using float? Just the basic arithmetic costs about 900 bytes but as soon as you try to print them it costs about 2500 bytes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which Arduino core are you using ? There are quite a few that support ATTiny. I tend to use https://github.com/SpenceKonde/A...

 

What 3rd party libraries if any are you using ? Any redundant/unused code you can strip out ?

 

Are you using a bootloader or a hardware programmer ? The bootloader takes some space, for 441/841:

 

Flash (program memory)

4096b / 8192b ( 3456b/7552b with bootloader

Last Edited: Sun. Jan 19, 2020 - 02:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using floats and printing them, yes.

It is reading temperature and humidity values from the DHT22 sensor.

It also needs to display these values.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't use floating point then. The range of temperature and humidity is small, so you can use fixed point integer math. For temperature, you probably don't want more than one decimal place, so 15.1 degrees is stored as 151. When you come to output it, write code to insert a decimal place in the right spot.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, and I did some Googling.
I use the DHT library of which dht.somefunction() returns a float with the temperature, ie. 19.50.
Could it be solved as such:
Int temp = 100 * dht.somefunction();
Then compare this value against integers for calculations, and output it with a comma using Serial.print(temp / 100);
I'm on mobile and can't test it, but I feel it can't possibly be that simple.

Last Edited: Mon. Jan 20, 2020 - 04:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why have float at all ? DHT temp/humidity appear to send integer and then fractional parts of the reading as two 8 bit values for both relative humidity and then temperature. So don't you just hold that as two 8 bit values and finally at the moment of display then just show <temp_int>"."<temp_frac> or whatever.

 

Presumably the library you currently have is actually reading the two bytes separately then gluing them back together and passing this to you as float? If so that probably too "high level" - you don't need it to do that - just read the bytes directly yourself.