Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Tuesday, 11 March 2014

Motion Triggered Camera

I just got a bunch of PIR motion sensors of Ebay - they're about £1 each and basically do all the hard work for you. Of course the first thing I did was hook them up to an Arduino - power, earth and signal, then wrote a Sniff program to test it:

make pir digital input 2
make led digital output 13

when start
.forever
..set led to pir


That didn't take long. After a little tweaking on the trim pots on the sensor to adjust its sensitivity, it was firing nicely whenever there was any movement around my desk.

So what next... I'll probably attach one to a battery and a buzzer and put it inside the biscuit jar - we have a seven year old biscuit burglar, but that doesn't actually need code. Then I remembered the new "system" device that lets you run shell commands on the Pi. If I had a camera hooked up...

make pir digital input 17
make system device
make command string

when start
.forever
..if pir
...set command to "raspistill -o image.jpg"
...tell system to "run"
... wait 10 secs

Of course this overwrites the image each time so:

make i2c device
make clock device
make clockValue string

make system device
make command string

make pir digital input 17
make filename string

when start
.forever
..if pir
...tell clock to "getTime"
...set filename to join clockValue ".jpg"
...set command to "raspistill -o "
...set command to join command filename
...tell system to "run"
...wait 10 secs

Now we use the clock device to get the real time in hours/min/secs (timer just gives us time since the program started - clock actually knows the time of day), and use that to make a filename.

Of course it doesn't  have to be a PIR - anything that the Pi can read could be used, and it doesn't have to be a camera command... 

No comments:

Post a comment