Project 13 uses the Piezo as a sensor... be sure to plug it into Analog pin 5 and not Digital pin 5! (Made that mistake and took me a few minutes to figure out what was wrong.)
When the program runs, the LED blinks twice to indicate the program is running... then it patiently waits for you to tap the Piezo. The video shows it in action.
Sorry for the rat's nest of wires... because I'm using a slightly different type of screw terminal, I used the jumper wires to allow me to share the GND pin. The MakerShield is still making it very easy to wire up these circuits so I'm taking advantage of it while I still can.
One question this program raised - the last bit of the code:
if (ledValue <=0) {ledValue = 0;} I'm not really understanding why this is required (although it is - see Video 2)... if ledValue drops below 0 and goes negative, the ledValue variable should simply jump to 255 when the Piezo is tapped, so it's not like the variable has to count up from a large negative value or anything. My best guess is that there must be a lower limit to the variable and when it hits it, it then "rolls over" back to 255. I may not be expressing that in the proper terminology, but... am I right? Wrong? Anyone have a better explanation for the pulsing?
BTW - A variation of this project is featured in Make magazine, Volume 25 - it's a candy dispenser that uses this technique to "listen" to a person's knock - if the knock is the proper tune, a piece of candy is released down the chute. You can watch an interview with the builder and see it in action in this video.
Wednesday, February 9, 2011
Subscribe to:
Post Comments (Atom)
Jim, you are exactly right. When the variable goes to -1, it is in fact "rolling over" back up to 255 (8 bit register). Because the 0.05 decrement is running in an endless loop, the number will continue to count down and roll back to 255.
ReplyDeleteWhenever you tap the piezo, no matter what value it's currently at, it will reset back to 255.
The last line is holding the value at 0 once it reaches 0 (effectively negating the --0.05)
Thanks, Damien...
ReplyDeleteI guess, technically, you could change that line to
if (ledValue >=-1) {ledValue = 0;}
Right? It can get down to -0.95 before the error occurs? Not trying to be a smart #@# but just pointing out how funny code can be... if the flip doesn't happen until -1 and you're decrementing by 0.05... :)
Ooops....
ReplyDeleteif (ledValue < -0.95) {ledValue = 0;}
Only one way to find out! :)
ReplyDelete