Introduction: Visual Computer Stress Meter- Arduino

About: 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 person.

Have you ever wanted to, without going out of your way to clock your processor, see how much stress your computer is under? With this project you will have a simple bar graph that constantly shows how much stress your computer is under. If the graph reads 0, your computer isn't working too much. If it reads 8, the highest, your computer is working very hard, and you probably should give it any more of a load. This tutorial is remotely easy, meaning a beginner should be able to follow it fairly well. After all, I am  beginner myself.

This tutorial is interned for Unix users. If I am correct, small tweaking should allow it to work on Windows. Maybe some day I'll re-write it for Windows users. 

Step 1: How It Works

You may be wondering how it works at this point, before you get started. All of the real work is done with a perl script.

1. A perl script clocks the processor.
2. The script uses that number to come up with a number from 1-9.
3. The script throws that number to the Arduino, using Serial connection.
4. The script get repeated over and over, for an infinite amount of time.
5. Each time the Arduino receives ten numbers, it averages them, and shows the average on the bar graph.

It's as easy as that!

Step 2: What You'll Need

Here is the list of supplies:
An Arduino
A breadboard
8 leds                                                    Or an actual bar graph unit
8 330 ohm resistors
Unix::Processors                                A perl module
Device::SerialPort                              Also a perl module
Math::Round                                       Yet another perl module

All perl modules can be downloaded from cpan.

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

That's it for the perl!

Step 4: Program the Arduino

The code for the Arduino is fairly simple. Begin with:

int led1=13, led2=12,led3=11, led4=10, led5=9, led6 = 8, led7 = 7, led8 = 6;
//Defines all the pins.

As said in the comment, it defines the pins. Now add setup ():

void setup()
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);

This just sets the pin-modes, and begins the Serial connection. Remember, the baud rate must be consistent with the perl script.
More code:

int loops = 0, overall = 0, avrg = 0;

These are three variables that are need. Loops is how many times it has looped. Overall is the past ten inputs added together. Avrg is overall / loops.
Add this:

void loop()
  int input;
  if (Serial.available() > 0)
    input = - '0'; //Takes input
  if (input != 0) loops++; //makes sure that input is not 0. If it is, it will not be counted
  overall += input; //Adds the input to overall
  avrg = overall/loops; //Averages it.

The comments really tell it all

  if (loops == 10)
    avrg--; //Decreases avereage by one. Remember in the perl script how we increase it by one?
    if (avrg > 0) {digitalWrite(led1, HIGH);}
    else {digitalWrite(led1, LOW);}
    if (avrg > 1) {digitalWrite(led2, HIGH);}
    else {digitalWrite(led2, LOW);}
    if (avrg > 2) {digitalWrite(led3, HIGH);}
    else {digitalWrite(led3, LOW);}
    if (avrg > 3) {digitalWrite(led4, HIGH);}
    else {digitalWrite(led4, LOW);}
    if (avrg > 4) {digitalWrite(led5, HIGH);}
    else {digitalWrite(led5, LOW);}
    if (avrg > 5) {digitalWrite(led6, HIGH);}
    else {digitalWrite(led6, LOW);}
    if (avrg > 6) {digitalWrite(led7, HIGH);}
    else {digitalWrite(led7, LOW);}
    if (avrg > 7) {digitalWrite(led8, HIGH);}
    else {digitalWrite(led8, LOW);} //Make the graph
    loops = 0;
    overall = 0;

This code simply says "Every ten loops, display the average, and reset everything." The comments should explain it.
Finally, add:

Serial.print("Trying to write: ");

This is included for trouble-shooting purposes. If nothing is lighting up, you can see if it's even trying to light up.

You're done with the Arduino code!

Step 5: Set Up the Arduino

Obviously, we're going to need to set up the Arduino. The image, created using fritzing, should explain it. The blue things are wires, the red things are leds, and the resistors are 330 ohm.

Step 6: Making It All Work

Now that you have all these things, you may be wondering how they all come together. Here it is:

1. Upload the code to your Arduino.
2. Open your Serial Monitor. It shouldn't say anything.
3. Use 'cd' in terminal to get to the directory that is in.
4. Run, from the terminal.
5. It should be working!

Remember, everything must be done in that order. If it's not, it will not work.

Feel free to post any questions you have in the comments, I would love to answer them!

Arduino Challenge

Participated in the
Arduino Challenge