The purpose of this project was to create a personal birthday gift. And while doing so have some fun with electronics, software and 3D printing!
The strawberry & whipped cream cake has 4 operation modes, set by rotating the strawberry encoder.
The default mode only has led-effects and no sound. Then you have the birthday mode, in which the cake plays happy birthday until you never want to celebrate a birthday again. It also has a "shuffle" mode. Here it plays random songs. And finally there is a surprise mode in which the lights are off and no music plays, if it becomes light it suddenly plays a song.
Part1 : The xylophone
How Many keys and what frequencies ?
After a few iterations I settled for this compromise :
- 12 keys, made of copper, outer diameter 10mm, inner 8mm.
- Support notes C6, D6, D#6, E6, F6, F#6, G6, A6, A#6, B6, C7, D7.
The formula for the length of the keys I got from http://users.df.uba.ar/sgil/physics_paper_doc/papers_phys/lapp.pdf
I cut a section of copper a few mm larger than calculated and then gently filed the edges. I regularly checked the frequency with an app ( https://stonekick.com/tuner.html ). Note : drilling a hole in the tube or sanding the tube changes the frequency slightly.
A few learners :
- You really need a lot more keys to play most music. I would guess at least 18 or 24. With 12 keys the device can only play parts of songs or songs with a simple melody.
- Although the xylophone is polyphonic ( all 12 keys can be hit at the same time ) in practice most playable songs do not use this.
- The tubes are polished with sanding paper and then a clear varnish is applied. If you put the key in a cordless drill it is easy to make it really smooth by making the key spin and hold a 600 grit sanding paper to it.
Part2 : The Electromagnets & strikers
This turned out more difficult than I anticipated.
I started with an electromagnet that pulled an iron core into its center if a current flowed through it. This turned out to be not such a good idea because it requires a lot of current in the electromagnet and you can only pull something into the core, you cannot repel it, which makes it more difficult to hit the key.
Then I read about using a neodymium magnet as the core. This reduces the required current significantly and now it is possible to hit the key by repelling the magnet from the electromagnet.
But - nothing comes for free - another problem popped up : the magnets in the strikers influenced one another. A lot of tweaks had to be done ( at one time iron rings were added at the bottom part of the coils to guide the magnetic field) but eventually a solution was found by using just a single magnet for each striker, and keeping the strikers enough apart.
The final striker consist of three parts : the hollow part of a iron furniture screw, a plastic holder and a neodymium magnet. No need for glue, the magnet holds it together.
I made the electronics so that I can control the electromagnets current through software. Although varying the striking power is not strictly necessary to play songs, it made it easy to debug and tune.
The final electromagnets have 570 turns of 0.2mm diameter enameled wire. There resistance is about 5.5Ohm.
To reduce the sound made by the strikers falling back into the cores felt textile is used on critical places, and the inside of the electromagnet and outside of the strikers have been smoothed.
I am not 100% happy with the sound quality though, the sound of hitting the keys is a bit to prominent for my liking..
Part3 : The electronics
Schematics
Nothing special about the schematic. An Atmega328PB microcontroller communicates serially with a 12 channel DAC. Each of the DAC outputs is followed by a power bipolar transistor arranged as an emitter follower.
Diodes D3..D14 protect the transistors against inductive recoil energy.
I added some basic over voltage, esd and wrong polarization protection. By adding these the change of destroying components during testing is much reduced.
PCB
The PCB is what I call a 1.5 Layer PCB. The bottom is a complete ground plane so no need to etch that. All tracks run on the top layer and to access GND you simply add a via.
In one of my next projects I'll order the pcb online, but for now etching it myself if much faster for prototyping.
Mounting the relatively small components is easiest if you use tweezers and a binocular microscope.
BOM
All components were purchased at mouser.com
Led ring and 79
good old WS2812 intelligent LEDS are used. 18 for the "79" and 35 for the ring, all connected in series.
Power supply
Any 5V/2A stabilized power-supply should work. I took a USB A to USB B cable and cutoff the USB B connector. I soldered the red/black wire to the 5V/GND on my PCB. the USB A connector can be plugged in a USB charger or a powerbank.
Part3 : The mechanicalparts
We bought our Prusa mk3s printer about 8 months ago and it really lives up to expectations. It delivers beautiful results and works without issues. The textured steel sheet give really nice surfaces.
My son is the 3D designer and he uses fusion-360 (for personal use) like a pro.
Part3 : The software
Excelmacro to convertmidi to "arduino" array
With an SD card it would be possible to read midi files directly in the arduino software. Without one the next best things is to convert the midi file to a more compact array format. The latter is what I did.
step1 : choose a midi file and pre process it. E.g. Cutout a piece or only keep 1 "track". I used MuseScore3 (free) to do this.
step2 : use the midicsv tool ( https://www.fourmilab.ch/webtools/midicsv/ ) to convert the midi file to a csv file.
step3 : I made an excel macro that :
- Opens the csv file.
- Transposes the notes so that the lowest note corresponds to the lowest note available on the cake (C6).
- Allows you to check if all notes in the song can be played on the cake. If not back to the drawing board (Musescore or just find another midi file).
- Generates an array that can be pasted in the arduino source code.
Setting up arduino for the Atmega328PB controller.
Before the arduino IDE can be used a boot-loader must be loaded in the processor. See https://www.circuito.io/blog/atmega328p-bootloader/ how to do so. As I forgot to add a pinheader on the MISO/MOSI/SCK pins I had to solder small wires to those pins first.
The extra features of the Atmega328PB are not available by default so I had to install the Watterott package ( https://github.com/watterott/Arduino-Boards) in the IDE to get things working.
After a lot of problems I found out the baudrate in boards.txt needed to be changed from 57600 to 115200.
The c++ software
An event driven application might be cleaner, but I decided to go for a synchronous solution. The main loop runs at a fixed 300Hz and every object or statemachine is updated every cycle.
Highlevel overview application
DacBH2221FV.h allows you to write a value to any dac channel.
The Key class contains all that is needed to drive a single key. Keys are identified by their midi number.
The MusicPlayer class holds 12 keys and knows how to play a song. The Songs are stored as compact arrays of struct ( see SongData.h )
The ControlLights class offers simple functions to control the 79 and ring leds.
RotarySwitch and SwitchAndLDR classes allow access to the rotary switch and light sensor.
And finally the Controller class makes all of it work together.
Comments