Hello
I'm Wim and I'm from Holland.
I started learning Arduino a couple of weeks ago, and its going quite well.
But now I need some help.
Problem is I want to read a serial data packet from a sim program.
This is wat it sends:
X~a01~Y~a02~
Where X and Y are axis and send as ascii, no problem reading that.
But ~a01~ and ~a02~ are variables from 0-255
I can read this as well, except when setting my baud speed higher or the refresh time of the program under 2ms.
The program does not support hardware handshaking.
I want to know what the most reliable way of reading the data-packet is.
My code so far.
#include <Servo.h>
#include <LiquidCrystal.h>
int servoPanPin = 11; // Control pin for servo motor
int servoTiltPin = 12; // Control pin for servo motor
int pos = 0;
int newpos =0; // position
int pos2 = 0;
char axis;
Servo panServo;
Servo tiltServo;
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
panServo.attach(servoPanPin);
tiltServo.attach(servoTiltPin);
}
void loop()
{
if ( Serial.available())
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9')
{ // is ch a number?
pos = pos * 10 + ch - '0'; // yes, accumulate the value
newpos = map(pos,0,255,0,180);
}
else if(ch == 'X' || ch == 'x') // pan
{
panServo.write(newpos);
Update_lcd('x',newpos);
pos = 0;
}
else if(ch == 'Y' || ch == 'y') // tilt
{
tiltServo.write(newpos);
Update_lcd('y',newpos);
pos = 0;
}
else if(ch == 'C' || ch == 'c') // clear
{
Update_lcd('c',newpos);
}
else if(ch == 't' || ch == 'T') // clear
{
set_servo();
}
}
}
void Update_lcd(char axis, int pos2)
{
if(axis == 'x')
{
lcd.print("X:");
lcd.print(pos2);
lcd.print(" ");
}
else if(axis == 'y')
{
lcd.print("Y:");
lcd.print(pos2);
lcd.print(" ");
}
else if(axis == 'c')
{
lcd.clear();
}
}
void set_servo()
{
tiltServo.write(90);
panServo.write(90);
}
Excuse me for the way I program, I'm still learning.
Thanks in advance.