Instructables

Visual Computer Stress Meter- Arduino

Featured

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:

#!/usr/bin/perl

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->databits(8);
$arduino->baudrate(9600); #Should be your Arduino's baud rate
$arduino->parity("none");
$arduino->stopbits(1);

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:

#!/usr/bin/perl
while (1)
{
    system("/path/clockandsend.pl");
}

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

That's it for the perl!




 
Remove these adsRemove these ads by Signing Up
yanney2 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)  yanney2 years ago
I don't really know...
What version of perl are you using?
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!