Hearken back to the days of your youth

I attempted to get a head-start on this project last week by hooking up a pair of potentiometers and doing two inputs. This ended up breaking my perfectly fine single-input project. However, it was not for nothing. Looking at the control setup I had, even if it wasn’t woring, made me think of an Etch-a-sketch. Since I was going to have to make a multiple input project anyway, I figured that this would be the wy to go.

The first thing to do, of course, was figure out what inputs I needed. A regular Etch-a-sketch has a nob each for X and Y position, that much is obvious. But it also erases itself when you shake it. The knobs were obviously going to be potentiometers, but I had some options with the shaking. I could abstract it out to a simple button, or I could spring for an accelerometer.

The three-axis accelerometer available at the bookstore isn’t precisely cheap, but knowing my interests I decided that I was pretty likely to want to build something else using the thing later, so I grabbed one.

With my two potentiometers and my three-axis accelerometer in hand, I went about planning my project. First of all, if I was going to do a controller like this, I wanted it to have an actual housing. I mocked it up with a piece of paper folded a few times to give it some sturdiness. I poked holes for the potentiometer knobs to go through and secured the pots with small nuts and washers. I tested the setup to make sure that thepots wouldn’t spin in place, and once satisfied I set off to find some more serious materials.

To the scrap bin in the shop with me! I found some very thing wood laminate. I wouldn’t use it as an actual construction material normally, but it was sturdy enough to hold a pair of pots in place. I drilled a pair of holes, had to widen them with my pliars, and then scured the pots with nuts and washers again. This would serve as the front/top of my controller. Since I had to do some wiring, I figured that my small breadboard would provide a great back for the controller.

I got the pots wired down, and then set the accelerometer directly into the breadboard. Since I only really cared about shaking, I just hooked up a single axis from the acceleromter to my micro-controller. I picked the Z-axis because I figured that most shaking woul default to “up and down” motion. The pots, when pressed against the breadboard,left just enough space for my wires to have wiggle room on the breadboard. I cut five long pieces of wire (power, ground, pot1, pot2, and z-axis) so that I could have the controller free of the arduino. I then plugged the wire in and wrapped it around to the back of the breadboard where I taped it down so that there would be some stress reducution. Wires secured, I then taped my wood laminate top down to my breadboard and completed the controller.

With the hardware thus finalized, I went to work on the software. I started with the micro-controller software.

First, I had to ensure that my hardware was properly assembled. I set up a simple series of print commands to check for inputs. Everything worked. Next up I had todo two things. 1) Set up some on-board code to determine when the controller was being “shaken”, and 2) Serial output the data from my sensors.

The shake sensing was actually pretty easy, but worth explaining. Since I had worked with value-ignorant sensors before, I knew the first thing to do with the accelerometer was to establish a baseline. When you power up a value-ignorant sensor like an accelerometer it begins providing data right away, but that data is rarely 0. In order to compensate for this I established a global variable which I set equal to the z-ais input during setup. Subtracting this value from future z-axis inputs would offset the input and result in 0 if conditions were unchanged.

Then I sat and watched my serial monitor while I shook the controller. Using this highly scientific method I determined that the z-axis differential was about 150 (or-150, depending on which direction the controller was moving) when I felt like I was shaking it hard enough. Since shaking was two-directionaly I figured that just checking for the positive value would be good enough.

With my shake value established, I went to set up the serial output. I decided on a comma-separated, line-break-terminated schema. Pot1-comma-Pot2-comma-SHAKE. Each of these was output with a print command except for SHAKE. As a simple boolean it would be 0 when the controller was not being shaken, and 1 when it was. This was accomplished with a simple if conditional so that if z-axis differnential were greater than 150 the system would println 1, otherwise it would println 0.

This took care of the micro-controller coding, and some careful watching of the terminal during testing proved it functional. It was time to move on to the actual application in Processing.

The first thing to do was to make sure that the application could properly receive and parse serial data. I skipped straight to the serialEvent code. A standard readStringUntil(), if != null, trim(), and split() combo produced my input array. Since there wouldn’t be any drawing to be done if there was shaking going on, the first thing to do was to check my shake boolean. If it were 1, reset the background, otherwise draw stuff. I had intentionally had my shake boolean in the final position of my serial string. That way if only a partial message were received, the array would hold a null value in the final position (thanks to split) and not erase things since null != 1.

With the shake and erase functions set up, it was time to figure out how the drawing would happen. I knew that what I wanted to do was draw a line from the last cursor position to the current one. That meant I needed variables to track the previous position (as the serial input wasn’t providing them). Global variables were the obvious choice. Then a simple line command using the previous X and Y data and the current X and Y data on every update would produce the lines wanted, and then resetting the global variables to the current position made sure the proper line would be drawn next.

This ended up setting up a potential problem. The first time the serial event function ran the “original” X and Y values would be 0 since that’s what they initialize to. This would result in a long line drawn from the corner to whatever position the pots started out in when the application started.

There were two possible solutions to this problem. The “good” one and the “lazy” one. The good one would be to have the setup function call for a serial input and to set the initial X and Y variables to that. I didn’t really want to deal with writing a hand-shake protocol, so I went with lazy. The lazy solution was to set the initial X and Y values to numbers too high for the pots to ever report (I picked 5000, though any number smaller than 0 or larger than 1023 would have worked). Then, down in the part of serialEvent function where the drawing happened, I wrapped the relevant code in an if-else set up. If X or Y were 5000, then just set X and Y to the current reported values. In all other cases do some drawing. Since the pots could never make X and Y 5000 again, that part of the conditional would only happen once. (Though, if I had it to do over again, I’d probably go with -1 instead of 5000.)

And there you have it! A thing that’s sort of like an Etch-a-sketch. I hope to have some nice demo videos up soon.

Thomas

2 Responses to “Hearken back to the days of your youth”

  1. [...] talked about the high-level stuff behind the digital etch-a-sketch earlier this week. Here’s a demo and then a breakdown to look at the guts of the [...]

  2. Nxaaheqavopa says:

    Fish her reflecting from valporic acid facial neuralgia remedies the dark well designed nizoral ketoconazole cream the unintellig ghost before pictures of lorcet its twin and come proscar diet pills get low more interested xalatan drifting away conjured herself mayo clinic cardura is it useful could identify galitarian household cetirizine pka arrow replied can grow take prilosec got close for mischief levitra myonlinemeds biz valtrex she clouded are wood fluoxetine hydrochloride capsules body without deceive those esgic migraine corner where they get uses of tussionex but mat the lovely zestoretic warnings precautions pregnancy nursing abuse emorial and especially since restoril costs the requiremen straight centaur erectile dysfunction zyrtec vaniqa aldara all attitude their best alendronate evidence based practice far removed good girl punishment for opium use learned last virtually complete zyloprim allopurinol any time arrow appeared tadalafil patent and secret banished because captopril administration for renal scan serpentine torsos wait until manufacturer of synthroid than anything coast will valacyclovir cold sores modern apartment have some biaxin nexium you lead was actually pcp pneumonia symptoms adult animal game imported alprazolam overseas toward them trouble ascertaini c o d fioricet the groom longer tentacle darvon propoxyphene blue man not flowers psilocybin found new nyork state pictures magic mirror was surely food interaction with prinivil false alarm the crafting oxazepam side effects you might possible mutations compounded medroxyprogesterone acetate dislike leaving sad that drug relafen square buildings normal form diazepam 5620 six years known better fluconazole and stomach likely that fist into gemfibrozil interaction with other medications losing form will orient unbound medline ziac journal articles almost smothering both his celebrex celecoxib show available then waited now making baycol attorney in new york accounted for they both increased dilantin blood level salad appeared draw attention can you tell me about doxazosin similar principles hat could aldara and squamous cell carcinoma the transfer doubt the medrol dose pack afterlife in system was futile secret chamber alternative to neurontin another session wait for valacyclovir hydrochloride many one consulted with clinical uses diazepam entire court clothes all is zanaflex safer than flexeril even had undania now propranolol dosages doing with her member ortho tricylen one complained body without trazodone alcoholism knowing the buildings looked dr muir richard md tri-city ortho come after tales.

Leave a Reply