Go Down

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

PeterH

Perhaps I have misunderstood your problem, then. Serial.available() tells you how many bytes are waiting to be received. These would be bytes send from your Processing application to the Arduino which the sketch has not read yet.

You can only have one thing writing to and reading from each end of the Serial connection, so if you have a Processing application accessing it, you must not use the Arduino serial monitor at the same time.
I only provide help via the forum - please do not contact me for private consultancy.

hilukasz


Perhaps I have misunderstood your problem, then. Serial.available() tells you how many bytes are waiting to be received. These would be bytes send from your Processing application to the Arduino which the sketch has not read yet.

You can only have one thing writing to and reading from each end of the Serial connection, so if you have a Processing application accessing it, you must not use the Arduino serial monitor at the same time.


ok sure. So the problem persists. Been at it for some time now. It seems like arduino just isn't receiving the data correctly, which makes me think I am sending it to arduino wrong. If I open a serial port to receive data in processing, would I have to open a new one to send it? or can I just use same port.
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

PeterH

You should be able to send and receive on the same serial port.

In your code you only seem to be trying to read from the Serial port when the distance reading is in range. I don't know why you're doing that - is it deliberate? Do you know that the value actually is in range? You need to know what's going on inside your sketch, and to understand that you really need to know what it's outputting to the Serial port and what your Processing application is writing to it.

For simplicity while testing, you could set the Processing application aside and use the Arduino serial monitor to send input to the Arduino and display whatever the Arduino outputs.
I only provide help via the forum - please do not contact me for private consultancy.

Nick Gammon

Your code doesn't compile:

Code: [Select]

sketch_oct22b.cpp: In function 'void loop()':
sketch_oct22b:54: error: expected `}' at end of input


Please note that, at present, the String library has bugs as discussed here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

PaulS

Code: [Select]
  float distance = analogRead(1);
The analogRead() function returns an int. Why are you storing that in a float?

Code: [Select]
  //map float to an int
  int x = (int) distance;

There's no "mapping" going on here.

Code: [Select]
            data[counter] = String(serialDataIn);
The serialDataIn variable is already a String. There is no reason to make another String instance, and then invoke the copy constructor on that instance, and then delete that instance. That is just uselessly gambling with an already dodgy class.


Go Up