Pages: [1] 2   Go Down
Author Topic: Serial.available acting funny  (Read 1872 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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); 
  }
}
« Last Edit: October 22, 2012, 07:03:47 pm by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...?
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 21, 2012, 07:42:52 pm by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18809
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code doesn't compile:

Code:
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.).
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  //map float to an int
  int x = (int) distance;
There's no "mapping" going on here.

Code:
            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.

Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code doesn't compile:

Code:
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.).


it compiles with no errors on my end. I will look into String thing, but I think its more of a format issue. I am receiving data in bytes and need to convert it to an int by subtracting 0. Will try both solutions tonight.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it compiles with no errors on my end.

That may well be true, but the code you have posted doesn't.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think its more of a format issue.

If you don't want to fix the problems being pointed out then it seems pointless taking the effort to try to help you find them.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

NL
Offline Offline
Newbie
*
Karma: 1
Posts: 33
Tech Tinkerer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Couple of remarks, hope they can help.

First, the line '  //}' seems weird, with the comment marks it does NOT compile. Taking out the '//' compiles ok.

Next, does it actually print the "length" string? If x is not inside your range, nothing is done, whatever you receive.

Then, the line 'float distance = analogRead(1);', again hmm, analogRead returns a int, not a float. Not critical, but not clean.

But, most critical, you are not looping yourself, but using the Arduino background code to repeatedly call loop(). This means you are not in full control over what the system is doing. Even in the Arduino the microcontroller 'rule' the main function never exits is still a good one to follow.

Now, i am not sure, but it looks like the serial buffer is reset upon entering loop().

If i place the main block in a while(true) { .. } loop, it seems to work; at least it sees the incoming data etc.

Oh, and finally, the lines
        // say what you got:
        Serial.print("I received: ");
        Serial.println(incomingByte, DEC);
make no sense, inComingByte is initialise to 0 and never set.


HTH,
Guido
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18809
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

String data[3];

...
            data[counter] = String(serialDataIn);
            serialDataIn = String("");
            counter = counter + 1;

counter is never reset to zero, it will exceed 2 soon enough, and overwrite memory.
Logged


Pages: [1] 2   Go Up
Jump to: