I don't know if they're not getting transmitted, or they are, but Arduino seems to interpret them as end of transmission.
I've tried all sorts of things but nothing I do works without the delay. I believe there is some issue with the handshaking, whatever, that doesn't allow Arduino to break out of a serial.read through LF or CR (I think it does that automatically when those are detected).
As you can see, I've taken out the checking for LF CR and it still works, but not without the 5 ms delay, even at higher baud rates.
What I meant before is that yes, the LF or CR is never detected because the print code in that routine never fires. They apparently stop the read on their own.
A 5 ms delay is fine. Even if it's an oddity in Arduino I'd imagine that might be the internal fix so whether you see it or not I suspect that's the amount of time you'd have to wait anyway.
#include <Servo.h>
Servo panServo;
Servo tiltServo;
// SERIAL PORT VARS
char cString[80]; // array that will hold command string (though we'll only need length 3)
int iBufferIndex = 0; // index of buffer characters rec'd
// SERVO VARS
int icount = 0;
long pval = 0; // pan val to send to servo
long tval = 0; // tilt val
int servoPanPin = 9; // Control pin for servo motor
int servoTiltPin = 10; // Control pin for servo motor
void setup() {
Serial.begin(9600); // connect to the serial port
panServo.attach(servoPanPin);
tiltServo.attach(servoTiltPin);
Serial.println("servos_ready");
}
void loop() {
readSerialString(); // read, wait for command
processSerialString(); // process command, either pan or tilt
}
void readSerialString () {
while(Serial.available() > 0) // will do until gets full line
{
int iBuffer = Serial.read(); // if something comes in save it
cString[iBufferIndex] = iBuffer; // add it to string
iBufferIndex++;
cString[iBufferIndex]= '\0'; // make sure our last element is nothing
Serial.print( iBuffer , BYTE); // echo back
delay(5); // for whatever reason, Arduino needs this or read doesn't finish
}
}
void processSerialString() {
if( iBufferIndex > 0) {
if(cString[0] == 'P') // first char should be P or T
{
cString[0] = ' ';
pval = atoi(cString); // pan value is number after P
// Serial.println();
// Serial.print("Value to PAN servo: ");
// Serial.print(pval, DEC);
panServo.write(pval);
}
if(cString[0] == 'T')
{
cString[0] = ' ';
tval = atoi(cString);
Serial.println();
// Serial.println();
// Serial.print("Value to TILT servo: ");
// Serial.print(tval, DEC);
tiltServo.write(tval);
}
Serial.println();
// Re-init
iBufferIndex = 0;
icount = 0;
pval = 0;
tval = 0 ;
} // if string isn't 0
}