Category Archives: Hacking

On the Matter of Size

When you write a more complex program that uses several libraries, you will sometimes run into a problem with that the sketch doesn’t fit in the Arduino. What to do?
You can find some ideas in the Arduino forum and elsewhere. Some ideas:

Your Own Code

Do you need all those libraries? Really? Check again.
Rewrite your code. Do you make the same 4 line code snippet in several places? Use a function. With some good comments, it will not be more difficult to read your code.
Also, try different approaches. Since the compiler uses a optimizer, the result might not be what you think.

Variable Types

Floats are often bloats… Do you really need floats? Or can you handle it with a integer that is 10 or 100 times the float value?
Remember that RAM is even more scarce than flash memory on the Arduino, and integers normally takes less memory than floats in Ram also.

 

Libraries Blues

Libraries are a good thing. You don’t have to care about the details how to handle a DHT11 or DHT21.
But they are also a bad thing for flash. There are a couple of DHT libraries out there. Some are specific DHT11, some handle DHT11 and DHT21. If you only have one of them, maybe you should go for the specific version.
To minimize the size of the code, you could be helped by reading the documentation and code. Some libraries (like uTFT library) have defines to remove unused parts.
If this is not enough, you might have to rewrite the library yourself. See this text about that.

For instance, the uTFT library uses floats and doubles to draw lines. This should be possible to work around, and save some 2kB of flash.

The Arduino Dev Kit

This is a part I didn’t expect would give me headache…
But look at these three outputs:
Binary sketch size: 27 526 bytes (of a 30 720 byte maximum)
Binary sketch size: 32 130 bytes (of a 30 720 byte maximum)
Binary sketch size: 32 150 bytes (of a 30 720 byte maximum)
This is the output from three versions of the dev kit. (1.0.5+dfsg2-2, 1.0.5, and 1.0.6).
Now, for this problem, I have so far not found a good solution. In other types of SDKs you normally can change the parameters sent to the compiler, but with the Arduino kit it isn’t that easy. I also suspect that one or more library has been ‘improved’, but this type of improvement is not needed by me.
As you can see, it is a quite big difference also, more than 4 kB.

Not using c/c++

I have an Arduino Nano with FlashForth installed. This might be a solution also. See more in an upcoming blog entry…

What you get is not allways what you order, or Hacking Time!

I have still not got the complete delivery from China (I’ll tell you what shop in China when everything is tested).
Still, two things I got (well, 2 items of the same type) gave me some trouble. Two nice solar powered LED lights (30 white LEDs).

IMGP4520
The text said that they had a on/off button, but no. They turn on when the light on the solar panel goes down…
So, what’s my options? Send them back (and pay as much in freight as they cost) or doing it the hacker way, making them work as I want?
The hacker way, of cause!
Since the function indicated that the important stuff is in the solar panel, I opened one and found this:

IMGP4523

OK, maybe that PCB can be changed a bit…. Let us take a closer look:

IMGP4525

A quick schematics layout (not 100% proofed yet…)

skärmdump6

U1(WS01) and U2 (8205) are the battery recharge circuit. I will try to remember to come back to them in a later post…

What was interesting to me was how to get the LEDs to go on even if the solar panel has light on it. The answer was quite easy. Remove R1. Then Q1 is closed and Q2/Q3 will turn on the light. After that it was just a matter of placing an external switch on the cable.

(ToDo: Get a better picture of the schematics….)

For those interested I’ve added data sheets for the two ICs and a PDF of the schematics (Made in KiCAD).

LedLamp WS01 8205

The lamp in the shop…