Help shrinking code

Hey everyone!
So im working on a small project consisting of a ds18b20 temp sensor and a ATtiny85, the problem is that the ATtiny85 only has 8kb flash and 2kb is used by the bootloader.

The scketch I’ve written reaches 7.5kb which is 127% of the available memory so its obviously to big. I think the problem lies in that the Dallas Temperature library is large but I’m not sure, I’m new to shrinking library’s.

Could anybody help me out if it’s possible to shrinking it down less than 6kb.

Library’s used in the sketch:

Dallas Temperature
OneWire
DigiKeyboard

Thanks in advanced! :slight_smile:

#include <OneWire.h>
#include <DallasTemperature.h>
#include "DigiKeyboard.h"

#define ONE_WIRE_BUS 0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


void setup() {
 
DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.sendKeyStroke(0, MOD_GUI_LEFT);
DigiKeyboard.delay(1000);
DigiKeyboard.print("notepad.exe");
DigiKeyboard.delay(1000);
DigiKeyboard.sendKeyStroke(KEY_ENTER);
DigiKeyboard.delay(1000);
DigiKeyboard.print("Ready to start logging");
DigiKeyboard.delay(300);
DigiKeyboard.sendKeyStroke(KEY_ENTER);
DigiKeyboard.delay(1000);
 
sensors.begin();
}

void loop() {

sensors.requestTemperatures();
DigiKeyboard.print(sensors.getTempCByIndex(0));
DigiKeyboard.delay(200);
DigiKeyboard.sendKeyStroke(KEY_ENTER);
DigiKeyboard.delay(59800);
}

Ditch the boot loader and load code with a programmer. No sense in throwing away a quarter of your code space on something like that.

No, the board does not require an external programmer to be reprogrammed, it loads code directly through a USB connection.

Right. But if you remove the boot loader and program with a programmer instead you'll have a lot more room for code.

There's not a lot else you can do to shrink that besides maybe rewriting some of those libraries. And that may not even help much. They're usually pretty compact.

The only other choice is to get a different chip with more program space.

I would preferably rewrite the library’s, but if I would remove the boot loader would it then still have the same functionality like act as a Keyboard and such, and how would I go about it?

And if you have any tips in stripping library’s I’d gladly take some, there are a few functions that I wouldn’t use such as conversions of temperature.

Functions you don't use aren't compiled into your code. Deleting those won't help.

I'm sorry but there's not a magic formula for shrinking a library. Just go through the code and try to find places you can optimize for size. There might not be much you can do.

The only difference between having or not having the boot loader is in uploading code. Once your code is running the boot loader is just wasted space. You don't need it for your code to work.

There are tons of places to get info on programming an attiny without a boot loader. Do some reasearch and come back if you have questions.

Just to clarify, normally I use a avr programmer to program my ATtiny´s but in this case, it’s a complete board designed to be programmed via USB. So, if I would just program this board like I normally do with standalone ATtinys it will ignore a bootloader and save space?

Thanks for the help so far!

Pretty much it.

One way to reduce this might be to find more primitive functions.

When you issue DigiKeyboard.sendKeyStroke(KEY_ENTER);, the function is prepared to send any keystroke at all out of hundreds of possibilities (escape, arrows, you name it). So there's a whole bunch of code in there that you never use that has to get linked in regardless.

If the sendKeyStroke() function breaks down KEY_ENTER into something simpler, you may be able to call that "something simpler" method directly and avoid a fair bit of code.

Similar comments apply to sensors.getTempCByIndex(0)

Just swap out and recompile on a teensy 3.2. Its a LOT easier than trying to rewrite someone else's libraries (like passing a live grenade to the future you.) or mucking about with changing how the programing code works.

When I switched to a teensy, the big difference was suddenly I have 256K Flash Memory, 64K RAM. Woo Hoo! Its like being let out of memory prison.

And yes, you use the same code & IDE to program both Arduino & Teensy.

-jim lee

@jimLee
I reply #6 OP states that it's a complete board designed for a given purpose. Swapping the ATtiny for a Teensy might not be a viable option.

Oh got it.

There's always something, ain't there?

-jim lee

I got the impression it is a Digispark.