Go Down

Topic: Serial.available acting funny (Read 2 times) previous topic - next topic

hilukasz

Oct 22, 2012, 01:02 am Last Edit: Oct 23, 2012, 02:03 am by hilukasz Reason: 1
for some reason my serial.available is acting really funny, it wont print the line Serial.print("serial avail passed val: "); but it seems to be receiving data...in some way because I can see the serial monitor reacting to processing program when I run it. It seems that serial.available() is just not passing and returning 0 or null. any idea?

Code: [Select]
//SERVO stuff
#include <Servo.h>
Servo myservo;  // create servo object, max 8 servo objects
 
// incoming serial data from processing
String serialDataIn;
String data[3];
int counter;
int inbyte;

void setup() {
  Serial.begin(9600);
  myservo.attach(3);  // server is on pin 3
  counter = 0;
  int pos = 0;
}

void loop() {
  //sensor 1
  float distance = analogRead(1);

  //map float to an int
  int x = (int) distance;

  Serial.print("initial value: "); Serial.println(x);

  // SERVO stuffs
  int incomingByte = 0;
  if( x > 400 && x < 750){
    Serial.write("length");
    if (Serial.available()) {
        Serial.print("serial avail passed val: ");
       
        delay(1);
        inbyte = Serial.read();
        Serial.println(inbyte);
        if(inbyte >= '0' & inbyte <= '9')
            serialDataIn += inbyte;
        if (inbyte == ','){  // Handle delimiter
            data[counter] = String(serialDataIn);
            serialDataIn = String("");
            counter = counter + 1;
        }
        if(inbyte ==  '\r'){  // end of line
                //handle end of line a do something with data
        }       
       
        // say what you got:
        Serial.print("I received: ");
        Serial.println(incomingByte, DEC);
    }
    myservo.write(x);              // tell servo to go to position in variable 'pos'
    delay(25); 
  }
}
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

PeterH


for some reason my serial.available is acting really funny, it wont print the line Serial.print("serial avail passed val: "); but it seems to be receiving data...in some way because I can see the serial monitor reacting to processing program when I run it. It seems that serial.available() is just not passing and returning 0 or null. any idea?


I can't make sense of that, probably because you are mistaken about what Serial.available() is supposed to do. Serial.available() doesn't print anything, does not receive data, does not pass or return anything written to or read from the stream.

What are you trying to do?
I only provide help via the forum - please do not contact me for private consultancy.

hilukasz

ah, I thought it returns number a length of items that where being sent. If nothing is sending then imagined it returned 0 or null and something sent it was greater than 1 hence true and that statement would pass. I've seen that Serial.available() in example files, so I guess I am confused on how to use it then...?
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

PeterH

The Serial port can be used both to send and receive.

Serial.available() tells you how many bytes are available to be received. This is unrelated to how many bytes you have sent.
I only provide help via the forum - please do not contact me for private consultancy.

hilukasz

#4
Oct 22, 2012, 02:40 am Last Edit: Oct 22, 2012, 02:42 am by hilukasz Reason: 1

The Serial port can be used both to send and receive.

Serial.available() tells you how many bytes are available to be received. This is unrelated to how many bytes you have sent.


ok sure, maybe I am misreading the documentation then, it states:

Quote
This is data that's already arrived and stored in the serial receive buffer (which holds 64 bytes)


according to: http://www.arduino.cc/en/Serial/Available
when I say data I have sent, I mean data I have sent from processing to arduino, just to be clear. I send data over to processing no problem, its getting it back after I have modified that data that is the problem. It's hard to monitor too, because Arduino IDE seems to initialize the serial monitor on top of the Processing, so I get double data.
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Go Up