Saturday, November 26, 2011

Girls hack Arduino

In all my programming life I never tried communicating in code with real-world objects. But now there is something to make it easy (or at least doable): Arduino. Sharon Cichelli demonstrated it at the All Girl Hack Night in October of 2011. Arduino is a microcontroller that lets you program blinky LED lights, or things that make buzzing alarm sounds, or all sorts of other electronic. It also accepts input from buttons or sensors, or anything that translates a physical action into electric pulse. You can plug things into one of its pins (there are 13 of them, if I recall), and have it "read" electric pulses from those pins, or "write" to them.

"Read" and "write" probably shouldn't be in quotes, because they are the same programming concepts as reading and writing to/from any input/output device. So, to switch on an LED light that's plugged into pin 13, you would digitalWrite(13, HIGH). As with all things digital, the function digitalWrite only recognizes on/off, or in this case, HIGH/LOW values. HIGH means the device plugged into the pin is on, LOW -- off. Simple, right? These basics allow one to sit down and program a primitive blinky light in minutes. The programming language is fairly high level. Its function names, as one can see, resemble human language -- this isn't assembly, where you push bits around with three-letter commands. It enables you to quickly do simple demos, like Sharon Cichelli did. Just as you digitalWrite to a pin to turn a device on, you digitalRead from a pin to recognize, say, a button press. Again, you pass to it the number of a pin into which the button is plugged in.

You write the code in an IDE, and with one mouse click upload it to an Arduino connected to your computer via USB.

Sharon Cichelli dictates the code that Anne types up on the laptop

Sharon Cichelli demonstrates Arduino programming



Arduino programming seems encouragingly simple at first, but as with everything, once you get deeper into it, you discover complexities. In fact, one's lack of electronics background might put brakes on the progress. Having been told that a LED light is plugged into a pin number 13, you (and by you, I mean me) might notice that the light actually has two wires, both plugged into tiny slots. Why are there two wires, and which of them is # 13, you ask. Oh, it's because the light needs to be grounded. The other wire is plugged into the grounding pin, you can ignore it for the purposes of your program. Yes, grounding is something you should have remembered from physics classes, but didn't. Then you digitalWrite to it, and it still doesn't light up -- that's because something is wrong with the hardware, and the circuit does not close as expected. Maybe the light isn't plugged in firmly enough. So, there are two dimensions to debugging Arduino code: a programmatic and an electrical dimension. As if catching just the software bugs wasn't tricky in itself!

Sharon couldn't help but demonstrate troubleshooting techniques, as things malfunctioned. Pins "floated". Still we had enough time to progress to a more advanced part of the lesson: Pulse Width Modulation. If you can only send "on" or "off" signals to a LED, how would you dim the lights, or produce nice, slow fade on / fade off effects? The fading or increase in brightness is caused by flickering very fast between on and off positions. If the light spends more time in the "off" position than "on", it appears to fade; if in on position, it grows brighter. If we graph HIGH and LOW signals versus time, where every HIGH signal is a vertical bar, we'll see that the "width" of the HIGH signal decreases (increases) over time. So that, simply put, is Pulse Width Modulation. Sharon pair-programmed with someone (sitting another person down at her laptop's keyboard, and dictating code to her) to implement the slow fade.

Besides blinking and buzzing things, you can connect Arduino to vibrating things. Erm. I mean, like a mat under a cat food bowl. A vibrating mat would scare off a cat that is afraid of vibration. This was the idea one of the girls had. How do you make a cat lose weight, if you also have a normal-weight cat, and each time one sees the other eating, she will eat too? The goal is to make the fat cat to eat less frequently than the normal-weight cat. So, this girl got an idea to put a vibrating mat under the cats' food bowl, so that when the cat steps on it, her weight would send an input to Arduino, which would trigger the vibration, scaring the cat off. The smaller cat's weight would not trigger this response. And the fat cat's weight would only do it at certain times but not others, otherwise the pudgy feline would never eat.

And if you don't have problematic cats, there is still a wide variety of applications for Halloween costumes, such as blinky eyeballs like Sharon made for her own Halloween costume).

Tuesday, November 08, 2011

Am I the only one to want this? Linking to any part of an HTML page

Here is a pipe dream. I would like HTML to have ability to link to any paragraph in any web page. I would like to make it so that when the user clicks the link, the page would open with the paragraph of interest at the top of the browser window.

I only know how to do it if some section of a web page has an <a name="xxxxxx"> in it. And if the page I'm linking to isn't created by me, I have no control over placement of <a name>'s in it. But is it possible to link to any element of a page? If not with HTML, then with Javascript or CSS? Can one write a Javascript that, when a user clicks on a link, will bring up the desired element of the page that has loaded? I kind of doubt it, because once a page has loaded in a browser, the scripts of the previous page stop executing, correct?

And I'm pretty sure there is no way to do this if the desired paragraph is not in any uniquely-identifiable HMTL element -- for example, if it only separated from other paragraphs with <br/>

If there is really no way to do this, I wonder if such functionality was never considered by They Who Design HTML? Has nobody ever wanted it? I wish for it when an explanation or example of something I'm writing about is buried deep in some other web page, and I know that my reader, if he/she clicks on the link, is not likely to find it quickly, or to spend much time searching for it.

I suppose one can write a third-party application that would let you mark up snippets of web pages and save them; then you could link to the paragraph on the third party website. It could provide a link to the original article, for those interested in the context. But that's an additional layer of complexity. There may even have been such a service, called iLighter; it allowed you to highlight web pages and save those highlighted snippets on its servers. I know I installed it in one of my web browsers a couple of years ago, but didn't do much with it. I had app fatigue even then (too many new web applications to try), which only got worse since. And iLighter doesn't seem to be around anymore -- I guess not too many people found it useful.

I know, I could write a Javascript that would pop up just the paragraph I want to link to, when the user's mouse hovers over the link to the article. So the user could get just the quote, but read the article for context if they'd like. Of course, this is inefficient, because the quote may change some time in the future, and somehow I would have to become aware of the change (which won't happen unless I monitor that web page every day), and modify my script. So, it's not a real solution.