Go Down

Topic: Linux Analog load meter, (Read 866 times) previous topic - next topic

atm0s

Hello,

I got my first project up and running this weekend please excuse the code below this is my first stab at it... the hardware  is analog liunx load meter, the hardware consists of a analog gauge with an LED backlight, 3 more LEDs red, yellow, green & a LM19 temp sensor. I use a bash script on a Linux client to send the load from cat /proc/loadavg > /dev/ttyUSB0 in the format "Sx.xx" I also need to read the LM19 in from the serial port from the Linux client at the same time as I send the load value from the Linux client to the Ardunio board. My question is really how do you send & recieve to the serial port at the same time  ?? Any advice code tips would be appreciated  


[code

// Output
int backlightPin   = 5;  
int redPin   = 8;  
int yellowPin = 7;  
int greenPin  = 6;  
int gaugePin  = 11;
// Input
int tempPin  = 2;

// Program variables
int gaugeVal   = 0;
int block = 0;
int tempVal   = 0;
long randNumber;
int incomingByte =0;

int i = 0;     // Loop counter    
int wait = 20; // 50ms (.05 second) delay; shorten for faster
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial

void setup()
{
 pinMode(redPin,   OUTPUT);   // sets the pins as output
 pinMode(yellowPin, OUTPUT);  
 pinMode(greenPin,  OUTPUT);
 pinMode(backlightPin,  OUTPUT);
 if (DEBUG) {           // If we want to see the pin values for debugging...
   Serial.begin(9600);  // ...set up the serial ouput on 0004 style
 }
}

// Main program
void loop()
{

 digitalWrite(backlightPin, HIGH);
 analogWrite(gaugePin,   gaugeVal);
 tempVal = analogRead(tempPin);  
 // gaugeVal   = random(0,130);
 incomingByte = Serial.read();
 

 if (incomingByte == 'S')
{
gaugeVal = 0;
 block = 0;
}
if ((incomingByte == '0')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 0;
 digitalWrite(redPin, LOW);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, HIGH);
}
if ((incomingByte == '1')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 145.2;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}
if ((incomingByte == '2')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 145.2;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}
if ((incomingByte == '3')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 145.2;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}
if ((incomingByte == '4')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 145.2;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}
if ((incomingByte == '5')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 145.2;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}
if ((incomingByte == '6')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 145.2;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}
if ((incomingByte == '7')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 145.2;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}
if ((incomingByte == '8')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 145.2;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}
if ((incomingByte == '9')&&(block == 0)&&(gaugeVal < 132))
{
 gaugeVal   = 145.2;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}
if ((incomingByte == '.')&&(block == 0)&&(gaugeVal < 132))
{
 block  = 1;
}
 if ((incomingByte == '0')&&(block == 1)&&(gaugeVal < 132))
{
 gaugeVal   = 0;
 digitalWrite(redPin, LOW);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, HIGH);
}
if ((incomingByte == '1')&&(block == 1)&&(gaugeVal < 132))
{
 gaugeVal   = 13.2;
 digitalWrite(redPin, LOW);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, HIGH);
}


if ((incomingByte == '9')&&(block == 1)&&(gaugeVal < 132))
{
 gaugeVal   = 132;
 digitalWrite(redPin, HIGH);
 digitalWrite(yellowPin, LOW);
 digitalWrite(greenPin, LOW);
}




 if (DEBUG) { // If we want to read the output
   DEBUG += 1;     // Increment the DEBUG counter
   if (DEBUG > 10) // Print every 10 loops
   {
     DEBUG = 1;     // Reset the counter



    //Serial.print(i);       // Serial commands in 0004 style
    // Serial.print("\t");    // Print a tab
    //Serial.print("temp:");    // Indicate that output is red value
     Serial.println(tempVal);  // Print  value
     //Serial.print("\t");    // Print a tab
     //Serial.print(gaugeVal); // println, to end with a carriage return
     //Serial.print("\t");    // Print a tab
     //Serial.print("I received: ");
     //Serial.print(incomingByte, BYTE);
     //Serial.print("\t");    // Print a tab
     //Serial.print("block: ");
     //Serial.println(block);
   }
 }
 delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}[/code]

macsimski

so if i read your setup correct, you have a sensor, a linux computer and a arduino. info passes from the sensor via the computer to the arduino and back. sensor measures, and the arduino sets the leds on the sensor.

am I right?

would it not be much simpler if you would connect teh leds directly to the arduino? then you could use just one way transfers via serial.
--
"We're all in this together..."

Go Up