loading

Step 3: 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!




Could some one please help me out with this problem I have with this project? <br>How do you run loop.pl. in the terminal window? I'm using a Ubuntu 12.04 and I did evrything in the perl that it told me to do. Do I download anything in the Text Editor? <br>I'm getting stuck at step 6, #3. Thank you for your help.
Cool idea! I will have to try it out
When I try to run the Perl code in Command Prompt I get this problem. Any Ideas? <br>C:\Users\Logan&gt;C:\Users\Logan\Desktop\clock\loop.pl <br>Can't locate Unix/Processors.pm in @INC (@INC contains: C:/Perl64/site/lib C:/Pe <br>rl64/lib .) at C:\Users\Logan\Desktop\clock\loop.pl line 4. <br>BEGIN failed--compilation aborted at C:\Users\Logan\Desktop\clock\loop.pl line 4 <br>. <br>
Due to what your paths look like, I'll assume you're using Windows. The interpreter is having issues with the Unix:Processors module, because, well, Windows is not Unix, and it actually isn't very similar to Unix. After doing a bit of research, I found the Win32::SystemInfo::CpuUsage, so I would recommend looking that up.
i run into a couple of problems. While trying to run loop.pl i get this &quot;Expected end of line, etc. but found &acirc;€œwhile&acirc;€.&quot; <br> <br>while trying to run clockandsend.pl, i get this &quot;Expected end of line, etc. but found identifier.&quot; <br> <br>any help?
I don't really know... <br>What version of perl are you using?
What version of Perl did you use, Strawberry or ActiveState?
I run Linux, so I didn't need either, I could just use the standard.
Nice little project! Maybe you should use 10 leds. Then every led is 10% processor load.
Yes, I was considering doing that. I actually don't know why I chose 8 instead...<br>If I have some free time I'll probably update it for ten LEDs, but as for now, I have other projects that I'm working on.
<a href="http://www.1buycart.com" rel="nofollow">Wholesale electronic</a><br><a href="http://www.1buycart.com" rel="nofollow">Wholesale electronics</a><br><br>http://www.1buycart.com<br><br>[URL=http://www.1buycart.com]China Wholesale[/url]
<a href="http://www.1buycart.com" rel="nofollow">Wholesale electronic</a><br><a href="http://www.1buycart.com" rel="nofollow">Wholesale electronics</a><br><br>http://www.1buycart.com<br><br>[URL=http://www.1buycart.com]China Wholesale[/url]

About This Instructable

7,415 views

28 favorites

License:

Bio: I am a human being that enjoys to build things. I also say GNU/Linux instead of just Linux. Yeah, I'm that kind of ...
More by alecnotalex: TouchPass: RFID Password Entry TiltKey- Write by Tilting RFI-DJ: MP3 Playing RFID Thing
Add instructable to: