Monday, March 30, 2015

"Back to the Future": The coding version

So there's this.  Tongue firmly lodged in cheek, I call it "The Mark I" (code name "Sylvester.")


See, over the past 10 years, we've tried training Mister Kitty to stay off the counter.  With the result that he's merely become progressively stealthier about it.  Which basically makes it an arms race.  Opening a new front in the war, the "Mark I" compensates for our less-than-preternatural hoomin hearing.

The thing hanging off a mini-USB cable at the top right is a programmable microcontroller (an Adafruit Trinket).  The green rectangle with all the pin-holes is a mini-size solderless breadboard, used to connect all the pieces.  The white square inside the green rectangle is a passive infrared (PIR) sensor.  The black button under the yellow and blue wires is a Piezo element (for making sounds).  And a pushbutton is trying to hide under the second blue wire.

The microcontroller is programmed to listen to the PIR sensor for the signal that it's detected movement of a warm body within its radius of operation.  Finding movement, the microcontroller has the Piezo element play a few bars of Tweety Bird's song ("I tawt I taw a puddy-tat...").  The pushbutton arms and disarms the device.

Crude?  Sure.  Ugly?  Yep.  But it scored its first success this morning when I busted His Doodness.  One yell and he was downstairs, not to be seen for a couple of hours thereafter.  The hope is that eventually he'll realise the futility of his schemes and keep his filthy litter-clogs off the counter.  And then I can re-use all the parts in other projects. 

As a programmer, this sort of project is something of a departure from the norm.  For one thing, software takes second place to hardware.  As the builder, you're responsible for making sure all the circuitry works and you don't fry components, blow fuses, or burn down the house.  That's not something that I normally have to worry about, even when, say, adding more memory to my PC.

But even the software side takes some getting used to.  It's not the language (which is a variant of C++, something I learned back in Programmer School) that's the issue.  It's the confines of the platform.  The Trinket's hardware isn't too far ahead of what went into 80s arcade games.  So you're not going to be writing the War and Peace of computer programs, right?

More significantly, there is no operating system--the processor merely runs the same program over and over until power is interrupted or new code is uploaded.  Now, if you've never written code for a web browser or a smartphone, the only thing you need to understand is that both these environments are set up to be able to do several things at once.

For instance, when you type a search into Google, each time you type a letter, the web browser goes out to a Google web server and lets it know what's currently in the search-box.  The web server responds by sending back its best matches.  That's how the results can change even as you type.  The major point is that the browser can listen for your key-strokes, fire off its information, and present the results as three loosely-connected processes.  In a word:  Multi-tasking.  Just like we easily-distracted hoomins can do.

The Trinket, by contrast, can only do one thing at a time.  In the case of the "Mark I," for instance, it can't listen for the reset button while a note is playing.  So I have to sneak in a check on the button's state in between notes.  But even then I have to be precise about the timing because the button needs time to "debounce" (don't worry about the definition of that--it's not important) and I have to make sure that doesn't add too much of a pause before the next note.

It took more error-and-trial than I should probably admit to, just to make something relatively simple like that work.  I suspect that the browser/smartphone mindset I've lived in for over a decade has a lot to do with that.  Unlearning is always harder than relearning.

Trust me, I'm absolutely not whining.  Back when the Trinket's processing power would have been considered cutting-edge, I would have had to program it in Assembler.  Which is the coding equivalent of building the Taj Mahal one Lego at a time.  Maybe that's what hipster programmers do; I dunno.

But I will say that having this little thing basically kick my butt is actually pretty fun in a "retro" sort of way.  Building like it's 1982...partying like it's 1999.