Serial.write-ing different things on same piece of code?

Hey all,
I'm working on a project with arudino and a servo. Basically, the arduino turns the servo, and sends a signal back to the PC when it's done turning. I'm using this adaptation of the servo library: GitHub - netlabtoolkit/VarSpeedServo: Arduino library for servos that extends the standard servo.h library with the ability to set speed, and wait for position to complete.

The weird thing is I seem to be getting different responses from the Arduino for no apparent reason, and me plus some coworkers I talked with are kinda baffled.

The piece of arduino code responsible is this (nb. myservo is a class of VarSpeedServo, not entire code is included):

while (Serial.available() <= 0) {

  serialInput = Serial.read();
  if (serialInput >= 0 && serialInput <= 180){ 
          // if legit number for Servo, make turns
          myservo.write(serialInput, 10); // serialInput is degrees to turn servo, 10 is the speed.
          delay(15);
          myservo.wait(); // wait for servo to finish turning
          Serial.write(45); // send back a random number, I just picked 45
  }
}

On the PC (running octave) I test this with a simple command followed by a while loop waiting for responses (nb. Servo is a class with methods for turning the servo, and properties about the servo):

response = []; 
Servo.Turn(); 
while isempty(response) 
    response = srl_read(Servo.port,1) % read 1 byte and display in commandline
end

I get different responses:

  • Servo starts to turn, octave commandline is filled with response = [], because nothing is read. After servo is done, it displays response = 45. This should be the correct response
  • Same as above, but instead of 45, i get response = 52. Happened on several occasions now.
  • Same as above, but there never is a response = 45. Commandline keeps filling with response = []. writing (on Arduino) or reading (in octave) seems to fail?
  • Immediately as the servo starts turning response = 45 is displayed. As if the myservo.wait is skipped. This seems to be persistent and if this happens, it keeps happening until octave is restarted.

I don't see a correlation between anything I do on the PC, except that restarting octave seems to change something. I also looked at the serial-monitor, to see if this responds differently from the srl_read in octave, but this behaves the same as srl_read-ing in octave.

I am already reworking my setup to not include this (time constraints), but still I want to know what is going. It almost feels like the Arduino is just messing with me.

Any thoughts or remarks are greatly appreciated.

while (Serial.available() <= 0) {
  serialInput = Serial.read();

I am not sure why you would only read from the serial input buffer while the serial input buffer is empty.

and me plus some coworkers I talked with are kinda baffled.

Well, me are baffled at your fractured English.

And your code, for the reason that groundFungus pointed out.

@reinderreind, please post a complete program.

...R

groundFungus:
I am not sure why you would only read from the serial input buffer while the serial input buffer is empty.

Heh... I totally missed that.
But that makes it all the more weird it does read serial code normally... But it's something to check! ..first thing Monday that is.