Hi everyone, this is my first attempt at any sort of coding with an arduino so with that in mind I am open to any and all help to get me headed in the right direction.
My project is simple, I am using 10 led's to represent the 100% of cpu usage for every 10 percent an led is lit and for each single percent past that the next led is lit but at a corresponding percent. My current problem is when I set me inCPU variable I only get the first number. If I send '54' inCPU stores '5'. Here is my full code hopefully someone can debug it for me.
// Matthew Maciejewski
// July 26 2010
// Simple script to received cpu usage percentage via com port
// and light a 10 led strip to represent percentage gauge
// Declare our LED variables
int ledWhite = 2;
int ledWhite2 = 3;
int ledBlue = 4;
int ledBlue2 = 5;
int ledGreen = 6;
int ledGreen2 = 7;
int ledYellow = 8;
int ledYellow2 = 9;
int ledRed = 10;
int ledRed2 = 11;
// Declare led pin number array
int arLEDnum[] = {2,3,4,5,6,7,8,9,10,11};
// Declare a variable to move through our array
int i = 0;
// Variable to store raw incoming cpu data
byte inCPU = 0;
// Declare variables for parsed cpu data
int majCPU = 0; // Variable to store major tick marks (x10) of cpu percent
int minCPU = 0; // Variable to store minor tick marks (x1)of cpu percent
void setup() {
// Set our LED pinmodes
pinMode(ledWhite, OUTPUT);
pinMode(ledWhite2, OUTPUT);
pinMode(ledBlue, OUTPUT);
pinMode(ledBlue2, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledGreen2, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledYellow2, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(ledRed2, OUTPUT);
// Open our serial port and set the data rate to 9600 baud
Serial.begin(9600);
}
void loop() {
// Reset our variables
inCPU = 0;
majCPU = 0;
minCPU = 0;
i = 0;
// Check to see if we have any data stored in the serial buffer
if (Serial.available() > 0)
{
// Read the data and store in inCPU variable
inCPU = Serial.read();
Serial.write(inCPU);
// Set majCPU and minCPU
majCPU = inCPU % 10;
minCPU = 10 * (inCPU - (majCPU * 10));
// Begin Output
// Display major ticks
while(majCPU > 0)
{
analogWrite(arLEDnum[i], 100);
i++;
majCPU -= 1;
}
// Display minor ticks
analogWrite(arLEDnum[i],minCPU);
// End Output
// Clear our serial buffer
Serial.flush();
}
// Simple delay
delay(10);
}