Need help on shrinking my code! Only need to cut off 12 bytes

I have an Adafruit Trinket (16mHz/5V) that I am planning to use with a quad-alphanumeric display and a DHT22 temp/humidity sensor. The goal is to display the temp read from the sensor on the display. Here is my code:

#include <avr/power.h>
#include <TinyWireM.h>

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "DHT.h"

#define DHTPIN 4

#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

void setup() {
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  
  alpha4.begin(0x70);

  dht.begin();
}

char db[4];

void loop() {

  int d = dht.readTemperature(true)*100;

  dtostrf(d,2,2,db);

  alpha4.writeDigitAscii(0, db[0]);
  alpha4.writeDigitAscii(1, db[1], true);
  alpha4.writeDigitAscii(2, db[2]);
  alpha4.writeDigitAscii(3, db[3]);

  alpha4.writeDisplay();
}

The binary sketch size is 5,322 bytes, while the maximum is 5,310 bytes; so close! Any help would be extremely appreciated, as this would allow me to finish my little project.

Thank you,

Trevor

Look in the libraries, they are taking most of your program space, see if there is anything in them you don't really need, and remove it.

Albeit the compiler does its best to remove unused code, but sometimes there are properties of classes that you won't use but the compiler can't possibly know this.

e.g.

textsize,
rotation;

in the Adafruit_GFX.h

I'm not even sure rotation works, and if you are not changing the text size, you could hard code it.

I suspect that would easily give you 12 bytes

what chip is it for?

  dtostrf(d,2,2,db);

char * dtostrf (double __val, signed char __width, unsigned char __prec, char *__s)

It's hard to see how you are going to fit a decimal point, and two characters after the decimal point in two characters, and still have room for anything before the decimal point.

pYro_65:
what chip is it for?

The board is the Adafruit Trinket 16mHz/5V, which is using the ATTiny 85 from Atmel.

try this

#include <avr/power.h>
#include <TinyWireM.h>

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "DHT.h"

#define DHTPIN 4

#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

void setup() 
{
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  
  alpha4.begin(0x70);

  dht.begin();
}

void loop() 
{
  int d = dht.readTemperature(true)*100;
  
  alpha4.writeDigitAscii(0, d/1000 + '0' );
  d %= 1000;
  alpha4.writeDigitAscii(1, d/100 + '0' , true);
  d %= 100;
  alpha4.writeDigitAscii(d/10 + '0' , db[2]);
  d %= 10
  alpha4.writeDigitAscii(3, d + '0' );

  alpha4.writeDisplay();
}

please post the results

@Rob, you removed char db[4], but still use it:

alpha4.writeDigitAscii(d/10 + '0' , db[2]);

If the second param is the decimal place, I guess we can just drop it ( and add the two ):

alpha4.writeDigitAscii(2, d/10 + '0');

@op, what happened to the rest of the space? Or is there a bootloader on the tinkerkit?
What version of the IDE do you use? 1.5.7 will not compile without removing at least the dtostrf call.

With Robs change I get:

Sketch uses 3,958 bytes (48%) of program storage space. Maximum is 8,192 bytes.

I also have an additional method of reducing the size. The total savings could allow more stuff!

Visit my page here http://arduino.land/Code/SmallSetup/ and install the SmallSetup library ( just updated it for the tiny chips ), then use the second method to use it. Result now is (including Robs savings):

Sketch uses 3,678 bytes (44%) of program storage space. Maximum is 8,192 bytes.

@ Pyro...
Thank You, That is truly amazing...

Doc

Docedison:
@ Pyro...
Thank You, That is truly amazing...

You're welcome!
The library is a bit of an accident, I was only trying to learn about the memory sections, and saw some great savings.
Probably one of the only libraries that reduces a sketch size when its added, even an empty sketch.

The code posted in the original post compiles in 5292 bytes for me. Maybe I have different ATtiny hardware configuration.

Anyway, this looks wrong:

char db[4];

No space for the null delimiter.

Adding the following to the end (and changing nothing else) it drops down to 5278 bytes, a saving of 14 bytes.

int main ()
  {
  init ();
  setup ();
  while (true) loop ();  
  }

However as Rob says, getting rid of the dtostrf call saves 1688 bytes, so that is the place to look.

pYro_65:
@Rob, you removed char db[4], but still use it:

alpha4.writeDigitAscii(d/10 + '0' , db[2]);

....

Ooops sloppy code :blush:
you see definitely not tested as mentioned in my post :wink:

Thanks everyone for your help! I got it compiled and working like a charm on the Trinket. I won't be using it on the final product, as I will be adding more functionality and need more pins, so I upgraded to the Pro Trinket. All your guys' help will definitely come in handy in the future!