Step 3: Set up the perl script

Picture of Set up the perl script
Here is where you will create the main perl script. Let's begin:


This would be the location of your perl interpreter. More code:

use Unix::Processors;
use Device::SerialPort;
use Math::Round;

All of these are libraries you need. The first allows you to clock the processor. The next allows you to write to the Serial port, and the last allows you to round numbers.
Now, add:

my $arduino = Device::SerialPort->new("/dev/ttyACM0"); #Should be the port your Arduino is on
$arduino->baudrate(9600); #Should be your Arduino's baud rate

All of these things initialize the connection with the Arduino. You should change what needs to be changed to make it fit your needs.

my $processor = new Unix::Processors; #Your processor
$overallspeed = $processor->max_clock; #Clocks it
$send = ((($overallspeed - 800) / 175.125) + 1); # 800 = minimum clock speed. 175.125 = max clock speed / number of leds

This chunk of code clocks the processor and puts it on a scale between 1 and 7. Some things need to be changed, such as your processors minimum clock speed, and your max divided by the number of leds. For this project, there is 8 leds.
Finally, add:

$arduino->write(round($send)); #Rounds the number, and sends it to your Arduino

This code is in charge of sending it to the Arduino.
Save all that code as clockandsend.pl
Now, this will only clock and send one number. You want to create another script to run this one over, and over again. For example:

while (1)

Just change "/path/" to whatever the path actually is.
I would save that as loop.pl.

That's it for the perl!

yanney3 years ago
i run into a couple of problems. While trying to run loop.pl i get this "Expected end of line, etc. but found “while”."

while trying to run clockandsend.pl, i get this "Expected end of line, etc. but found identifier."

any help?
alecnotalex (author)  yanney3 years ago
I don't really know...
What version of perl are you using?