Close

Completed Project Log

A project log for Accelerated Dice

A randomized dice using an Adafruit ATTINY85 breakout board, 8x8 LED matrix, and an accelerometer.

ford-sleemanFord Sleeman 01/05/2017 at 04:150 Comments

Instead of posting individual logs for the project, I am posting a single completed one. Below is the path I took while I was working on this project and the challenges I ran into.

The first set of this project was to acquire the required parts which was easy since it was mostly from adafruit.com. Once I got the main components I tried playing around with the examples provided and was able to get the LED matrix and the accelerometer working together with little trouble. Next, I wrote custom code to generate the numbers displayed on the matrix as the Adafruit libraries, while user friendly, were way to big for a 1k limit.

After it looked like this project was viable from a technical standpoint I started working on a 3D printed enclosure to make this a real dice that could be rolled. When all was said and done, I might have spend almost as much time on the enclosure design as the software and hardware. After about three iterations I finally got an enclosure that fit all the components reasonably well.

Initially I was planning on using the raw accelerometer 3-axis values for triggering the rolling action. While this appeared to be working it took a lot of code a hundreds of bytes of data. Even resorting to a single axis did not save enough space since they were all using the same functions that had to compiled if they were used only once. Fortunately I realized that simply reading the current accelerometer orientation took much less code and ultimately is what I really wanted.

After making these changes I will still around 1.2k bytes and with only a few more bytes to shave some space. After spending hours souring the internet for how to reduce the file size I learned that the Arduino IDE compiles a lot of extra stuff right from the start and interrupts were being implemented for a number of functions I was calling. Using a blank Arduino sketch for the Adafruit Trinket board with only an empty setup() and loop() functions the resulting sketch size was 286 bytes! That is over 25 percent of the total space I had to work with so it was obvious I had to go into the Arduino core code to remove unneeded code.

When I was ready to start working on the Arduino code I realized I could not figure out where was the code to modify. I figured out that under the Arduino IDE File→Preferences there is an option under “Show verbose output during:” named “compilation”. This will spit out all of the build instructions including the library and core files used to actually create the sketch. In my case, the core code was under C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino.

The first thing I changed was to comment the init(); command from the main.cpp file. This function call initialized some interrupts which I was not going to use. After making that change the new blank sketch is now only 54 bytes! That huge saving is what I needed to make a respectable dice. I believe there are a few more bytes that can be saved, possibly from the Wire library, but this 200+ byte savings was enough for this project.

When modifying the Arduino core code it important to remove the pre-compiled object files. Unlike the files in your sketch directory, the core code is not recompiled by default. The Arduino IDE uses a temporary build directory that can be found in the build log. This directory path will be something like C:\Users\{user name}\AppData\Local\Temp\arduino_build_81478. Under that directory are sub-directories named core and libraries. The core directory will need to be cleared when modifying Arudino core code and the libraries directory will need to be cleared when modifying an included library. Failing to do will use the old compiled object files.

One strange thing I noticed was that when I modified the Wire library, or tried to completely break it, the compiled sketch did not seem to show the change. I was expecting the sketch to fail to compile at least since I purposefully added invalid code. Even though I modified every copy of Wire.cpp I could find the sketches did not show the change. This likely means that either there is another copy of Wire.cpp being used (unlikely since the one used is listed in the build commands) or there is a pre-compiled object file being used that I was not removing. This mystery still continues but will try to figure that out later.

The Adafruit Trinket uses an Attiny85 chip and in theory this code, or perhaps with a very small modification, should work with other Attiny85 chips. The Trinket does use a USB bootloaded because it cannot be programmed with serial. Since I am not using any feature in the bootloader other than actually booting the Trinket, I working under the assumption that it will not count against the 1k limit. When the sketch is compiled, the size is printed and I am using this number for my final project size and I got 1,008 bytes. I have attached a screen shot of the sketch build.

Discussions