Arduino and Perl

Published 10th December 2012 by TBSliver

So just started learning Perl (because, well, why not?), and in trying to find something to do with it, I found a module on CPAN for using serial with an Arduino. Now if you have followed this blog at all, you will know that the Arduino is the first device I go to when I need some hardware controlling, so learning a new programming language to then control said hardware was not much of a massive leap. So with that, I pulled out an Arduino, booted up gedit, and set to work!

The first thing to do was to create the Arduino program. This one was quite simple, just accepts characters on the serial port, and then turns on or off its LED depending on what it was sent.

Now, this bit is also very simple, at the moment. After calling the module (Device::SerialPort::Arduino) and initialising it, the ‘do, while’ loop just takes and input, and only sends it if it is 2 characters long (a character, and a newline).

Using these two, if I send an ‘a’, the light turns on, and if I send a ‘b’ the light turns off! and if I type in ‘q’ it ends the program. Now, as I’ve said, this is quite a simple piece. So to extend this, I’m going to create a ‘proper’ interface (read: a gui that turns it on and off!). Tune in next time!