Go Down

Topic: Arduino Serial Comm Issues (Read 4166 times) previous topic - next topic

SuperSteve

Having issues returning output to a terminal emulator (puTTY) from a Arduino Uno, we are trying to set up a oil pumping and monitoring system. It's returning output when running through the Arduino IDE however when running through puTTY no output is generated and the program seems to not be running. When printing any data in the main loop function it seems to print fine to both puTTY and Arduino IDE.

Very confusing  :~ Any Ideas?

Code: [Select]

int serialInput[9];
int a = 0;

int tempInt1, tempInt2, tempInt3, tempInt4, tempInt5, tempInt6, tempInt7, tempInt8, tempInt9;
int litres, pulses, flowPIN, solPIN, AwesPIN;
boolean pumping;
int buttonState = 0;
int lastButtonState = 1;
int buttonPushCounter;
float output = 0.000;

unsigned long time;
boolean setTimer = false;

void setup() {
  //Set up Serial Session at 9600 Baud
  Serial.begin(9600);
}

void loop () {
  if ( Serial.available() > 0) {
    setTimer = true;
    while(a < 9)
    {
    serialInput[a] = Serial.read();
    a=a+1;
    delay(10);
    }
   
    a = 0;
    tempInt1 = serialInput[0] - 48;
    tempInt2 = serialInput[1] - 48;
    tempInt3 = serialInput[2] - 48;
    tempInt4 = serialInput[3] - 48;
    tempInt5 = serialInput[4] - 48;
    tempInt6 = serialInput[5] - 48;
    tempInt7 = serialInput[6] - 48;
    tempInt8 = serialInput[7] - 48;
    tempInt9 = serialInput[8] - 48;

    litres = (tempInt1 * 10) + tempInt2;
    pulses = (tempInt3 * 100) + (tempInt4 * 10) + tempInt5;
    flowPIN = (tempInt6 * 10) + tempInt7;
    solPIN = (tempInt8 * 10) + tempInt9;
   
    pinMode(flowPIN, OUTPUT);
    if (pumping == false) {
      AwesPIN = solPIN;
      pinMode(AwesPIN, OUTPUT);
    }
   
    if (litres == 0 && pulses == 0 && flowPIN == 0 && solPIN == 0) {
     digitalWrite(AwesPIN, LOW);
     pumping = false;
    }
   
   if (solPIN > 0) {
    pinMode(solPIN, OUTPUT);
    digitalWrite(solPIN, HIGH);
    pumping = true;
    buttonPushCounter = 0;
   }
  }
  if ((millis() - time) == 600000) {
  pumping = false;
  digitalWrite(AwesPIN, LOW);
  Serial.println("Timeout");
  } 
 
  if (pumping == true) {
    if (setTimer == true) {
      time = millis();
      setTimer = false;
    }
   buttonState = digitalRead(flowPIN);
      if (buttonState != lastButtonState) {
        if (buttonState == HIGH) {
          buttonPushCounter++;
           output = (float)buttonPushCounter/(float)pulses;
           Serial.println(output);
           delay(100);
        }
Serial.println(buttonState);
Serial.println(lastButtonState);
Serial.println("--");
      }
       lastButtonState = buttonState; 
  }
 
  if (buttonPushCounter == litres * pulses) {
   pumping = false;
   digitalWrite(solPIN, LOW);
  }
}

PaulS

Quote
It's returning output when running through the Arduino IDE

I doubt that. The Arduino may be writing to the Serial Monitor, but that is a separate application from the IDE.

Code: [Select]
int tempInt1, tempInt2, tempInt3, tempInt4, tempInt5, tempInt6, tempInt7, tempInt8, tempInt9;
Wouldn't an array make more sense?

Code: [Select]
  if ( Serial.available() > 0) {
    setTimer = true;
    while(a < 9)
    {
    serialInput[a] = Serial.read();
    a=a+1;
    delay(10);
    }

If there is one character available to read, read all 9 of them, hoping that the others show up before the read is attempted. This is a recipe for disaster. Any robust communication mechanism will use, at a minimum start- and end-of-packet markers, so you KNOW when to start reading AND when to stop.

Code: [Select]
    tempInt1 = serialInput[0] - 48;
Wrong!!!!
    tempInt1 = serialInput[0] - '0';

Code: [Select]
    pinMode(flowPIN, OUTPUT);
Why are you setting the pin mode in loop()? Are you using the pin for both input and output?

Code: [Select]
   buttonState = digitalRead(flowPIN);
      if (buttonState != lastButtonState) {
        if (buttonState == HIGH) {
          buttonPushCounter++;
           output = (float)buttonPushCounter/(float)pulses;
           Serial.println(output);
           delay(100);
        }
Serial.println(buttonState);
Serial.println(lastButtonState);
Serial.println("--");
      }
       lastButtonState = buttonState; 
  }

Each { belongs on it's own line. The Tools + Auto Format menu option would properly indent all of your code, to make it more readable.

Quote
When printing any data in the main loop function it seems to print fine to both puTTY and Arduino IDE.

You don't have any print statements in setup(), and you don't have any other functions, so I fail to see what the problem is.
The art of getting good answers lies in asking good questions.

mromani

Quote
Quote
When printing any data in the main loop function it seems to print fine to both puTTY and Arduino IDE.

You don't have any print statements in setup(), and you don't have any other functions, so I fail to see what the problem is.


I think by "main loop function" he meant "loop() function". There are print(ln) statements in there.

PaulS

Quote
I think by "main loop function" he meant "loop() function". There are print(ln) statements in there.

There are, but he says those work, and it is the others that don't. There being no others, I don't understand what doesn't work.
The art of getting good answers lies in asking good questions.

mromani

Code: [Select]
if ((millis() - time) == 600000)
It's very difficul that you catch that exact value. In other words, use >=, not ==.

SuperSteve

Hey, Thanks for the feedback. I've made some changes to my code but the problem is still present.

Quote
Why are you setting the pin mode in loop()? Are you using the pin for both input and output?


This is because the Solenoid PIN and the Flow Meter PIN are defined via Serial Input

Code: [Select]
int serialInput[9];
int a = 0;

int litres, pulses, flowPIN, solPIN, flowPIN2;
boolean pumping;
int buttonState = 0;
int lastButtonState = 1;
int buttonPushCounter;
float output = 0.000;

unsigned long time;
boolean setTimer = false;

void setup() {
  Serial.begin(9600);
}

void loop () {
  if ( Serial.available() > 0) {
    setTimer = true;
    while(a < 9)
    {
      serialInput[a] = Serial.read();
      a=a+1;
      delay(10);
    }

    a = 0;
    serialInput[0] = serialInput[0] - '0';
    serialInput[1] = serialInput[1] - '0';
    serialInput[2] = serialInput[2] - '0';
    serialInput[3] = serialInput[3] - '0';
    serialInput[4] = serialInput[4] - '0';
    serialInput[5] = serialInput[5] - '0';
    serialInput[6] = serialInput[6] - '0';
    serialInput[7] = serialInput[7] - '0';
    serialInput[8] = serialInput[8] - '0';

    litres = (serialInput[0] * 10) + serialInput[1];
    pulses = (serialInput[2] * 100) + (serialInput[3] * 10) + serialInput[4];
    flowPIN = (serialInput[5] * 10) + serialInput[6];
    solPIN = (serialInput[7] * 10) + serialInput[8];
    Serial.print("Litres = ");
    Serial.println(litres);
    Serial.print("Pulses = ");
    Serial.println(pulses);
    Serial.print("Flow Meter PIN = ");
    Serial.println(flowPIN);
    Serial.print("Solenoid PIN = ");
    Serial.println(solPIN);
    Serial.println();
    Serial.println("Litres Pumped:");

      pinMode(flowPIN, OUTPUT);
    if (pumping == false) {
      flowPIN2 = solPIN;
      pinMode(flowPIN2, OUTPUT);
    }

    if (litres == 0 && pulses == 0 && flowPIN == 0 && solPIN == 0) {
      digitalWrite(flowPIN2, LOW);
      pumping = false;
    }

    if (solPIN > 0) {
      pinMode(solPIN, OUTPUT);
      digitalWrite(solPIN, HIGH);
      pumping = true;
      buttonPushCounter = 0;
    }
  }
  if ((millis() - time) >= 600000) {
    pumping = false;
    digitalWrite(flowPIN2, LOW);
  } 

  if (pumping == true) {
    if (setTimer == true) {
      time = millis();
      setTimer = false;
    }
    buttonState = digitalRead(flowPIN);
    if (buttonState != lastButtonState) {
      if (buttonState == HIGH) {
        buttonPushCounter++;
        output = (float)buttonPushCounter/(float)pulses;
        Serial.println(output);
      }
    }
    lastButtonState = buttonState; 
  }

  if (buttonPushCounter == litres * pulses) {
    pumping = false;
    digitalWrite(solPIN, LOW);
  }
}



Output from Arduino Serial Monitor, Which is Correct
Code: [Select]
Litres = 5
Pulses = 5
Flow Meter PIN = 2
Solenoid PIN = 3

Litres Pumped:
0.20
0.40
0.60
0.80
1.00
1.20
1.40
1.60
1.80
2.00
2.20
2.40
2.60
2.80
3.00
3.20
3.40
3.60
3.80
4.00
4.20
4.40
4.60
4.80
5.00


Output from PuTTY
Code: [Select]

Litres = 2

Pulses = 5

Flow Meter PIN = 2

Solenoid PIN = 3



Litres Pumped:

Litres = -399

Pulses = -5439

Flow Meter PIN = -539

Solenoid PIN = -539



Litres Pumped:

-0.00



PaulS

Quote
Output from Arduino Serial Monitor, Which is Correct

Quote
Output from PuTTY

which is all hosed up.

So, how is this an Arduino problem? Clearly, the Arduino is sending data correctly. The PuTTY application, on the other hand, is not processing it correctly. Looks like a configuration issue with PuTTY to me.
The art of getting good answers lies in asking good questions.

PaulS

Quote
This is because the Solenoid PIN and the Flow Meter PIN are defined via Serial Input

The pin to set HIGH or LOW might be coming from the serial port, but the fact that certain pins have hardware associated with them doesn't change, while the Arduino is running, does it?

The pin mode should be set in setup(). The setting of the pin HIGH or LOW should be done in loop(), based on the serial data.
The art of getting good answers lies in asking good questions.

mromani


Quote
Output from Arduino Serial Monitor, Which is Correct

Quote
Output from PuTTY

which is all hosed up.

So, how is this an Arduino problem? Clearly, the Arduino is sending data correctly. The PuTTY application, on the other hand, is not processing it correctly. Looks like a configuration issue with PuTTY to me.


A screenshot of putty configuration might help diagnose the problem. Maybe the option "Implicit CR on every LF" under "Terminal" has something to do with those blank lines in the output ?

PaulS

Hey mromani - Congrat's on the promotion to deity!
The art of getting good answers lies in asking good questions.

mromani

Huh ?

Ah, yes, I suppose it's because of the 500th post. Thanks :-)

dxw00d

#11
Feb 15, 2012, 03:49 pm Last Edit: Feb 15, 2012, 04:37 pm by dxw00d Reason: 1
Personally, I think it's a daft title, particularly as my partner thinks it's funny. Presumably, we're stuck with it until 1000 posts.

mromani

Are you saying I should start a stealth spam campaign to find out what's after god ? :-P

PaulS

Quote
Are you saying I should start a stealth spam campaign to find out what's after god ?

No need. Edison comes next.  8)
The art of getting good answers lies in asking good questions.

mromani

(self-censored comment to stop this way too far OT)

:smiley-zipper:

Go Up