Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hamlet The Pig

Diego Quiroga
November 12, 2019

Hamlet The Pig

The story of the twice resurrected hog
who reignited my love for coding.

Diego Quiroga

November 12, 2019
Tweet

More Decks by Diego Quiroga

Other Decks in Technology

Transcript

  1. Working as a manager you can spend a long time

    without touching code and it is not unusual to start questioning yourself about your technical fit. Sometimes, it takes someone or something else to remind you why you got into this in the first place.
  2. Hamlet was a life-sized robotic piggy bank for donation campaigns.

    It was created by the Vancouver Community College Electronics Department back in 1991.
  3. After many years of dedicated service to the Mount Pleasant

    Neighbourhood House, Hamlet was abandoned in a basement, in very bad shape and ready for its retirement.
  4. My artist-wife Mariana volunteered there and as the passionate animal

    lover that she is, decided to bring the pig home for a facelift.
  5. Once we plugged him in. a weird barely understandable robotic

    voice thanked us when we put coins in the slot. Thirty years later and still working like the first day.
  6. As Mariana started to do her magic on him I

    began to think: ” I could have some fun with this.”
  7. My idea was to modernize it with an Arduino board,

    an MP3 shield , an SD card reader, a couple of speakers, a servo, perhaps a new IR sensor for coins.
  8. But the more code I added, the more I hated

    the Arduino IDE. It was too basic for any semi-serious project. I had no way to pinpoint problems in my tests. It really sucked.
  9. Luckily, I found PlatformIO, an ecosystem for embedded development. It

    nicely integrated with VS Code and made my life much easier. So I had no more excuses to deliver. Except I had not really considered memory usage.
  10. The Arduino Uno is very limited. You have 32K of

    ROM to flash your code into. And the incredible amount of 2,048 bytes of RAM split in two chunks. The first, for static and global variables. The second, for the heap and the stack to fight for space. Running out of memory during execution is very easy. The Arduino goes coo coo and starts doing all kinds of crazy s.. and resetting itself.
  11. You can, for example, move some constant data to the

    flash ROM, freeing up some in the first chunk of RAM dedicated to static and global variables.
  12. Here, I’m saving one byte for each one of these

    constants by using ‘byte’ instead of the default int you may have seen in all Arduino examples.
  13. This was the most challenging one. I had to resort

    to the C strings library and using pointers to avoid the heap fragmentation caused by the String object. The last time I touched C was in college twenty years ago.
  14. Later, with my product hat on, I had to consider

    my users. It should be easy to manage for volunteers and others working for the neighbourhood house.
  15. I created an additional board to power the Arduino, connect

    sensors and the other external devices.
  16. It was dark inside the pig, so I used a

    white LED as the power indicator. I added a red one to blink different patterns whenever a problem occurred (like missing SD card, wrong files, etc..)
  17. That was.. NOT easy! Also had to reverse-engineer the IR

    sensor to reuse the coin slot, find out how to it blinked the eyes in two colors with just two wires, introduce random variations to make tail wagging more 'natural', implement non-blocking interrupts to smooth audio playing, handle abnormal states and degraded performance gracefully, use proto-threading to simulate concurrent events.. In the end, it took longer than I first estimated. But at the same time, learning how to overcome the all the roadblocks that presented themselves made the process fulfilling beyond my expectations And a bunch of other stuff that I have no clue before.
  18. The End..? And this could have been the happy ending

    of our story. Unfortunately, a month after Hamlet’s triumphant return we received bad news.
  19. Burglars had broken in and hurt Hamlet badly to steal

    the money. The police dusted it looking for fingerprints and attempting to solve the mystery. For us, well, it was just another trip home.
  20. Hamlet 2019 Hardened power supply Kidproof magnetic tail New blinking

    error codes Volume control Reinforced structure/door I’m particularly proud of the magnetic attachment for the tail, kids kept breaking the initial version. Hamlet has been collecting money for Mount Pleasant for a year. They are very happy with him.
  21. What I found out Some pigs can live forever Using

    pointers is just like riding a bike Keep looking until you find the right tool Save the RAM Constraints make us incredibly creative Don’t stop learnin’ ♫ ♬ ♪ This is still my thing and I love it. There is joy to be found in limitations. Nothing like extracting all the juice from a small piece of hardware, working very close to the machine without a ton of abstraction layers in between.
  22. Learning new stuff is always fun, but in this particular

    case the most important outcome for me was to knock out my imposter syndrome and find out that I still love what I do. Thank you.