Hi Group,
I am working on a simple XY steering that receives data tru the serial portfrom a PC in a
simple format like x deg,y deg. => 180.0,180.0 Here's the complete sketch:
#include "Arduino.h"
#include <Servo.h>
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
#define DEBUG 1
#ifdef DEBUG
#include <Arduino_DebugUtils.h>
#endif
#define BAUDRATE 19200
#define ledPin 13
String PROG_NAME = "ArduKServo V0.0";
String VERSION = "Version 01s 8";
Servo servoXaxis; // create servo object to control a servo
Servo servoYaxis; // create servo object to control a servo
int posXaxis = 90; // variable to store the servo position
int posYaxis = 90; // variable to store the servo position
int index = 0; // Used in the commandProcessing routine
int incomingByte = 0; // for incoming serial data
boolean valueX = false;
boolean valueY = false;
char strValue[7];
char xStrBuffer[8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
char yStrBuffer[8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
String axisValueString ;
String content = "";
int newXAxis = 0; // Holds the new value where the dish should point to
int newYAxis = 0;
boolean isDecimal = false;
void setup() {
Serial.begin(BAUDRATE); // opens serial port, sets data rate to 115200 bps
Serial.println(PROG_NAME);
#ifdef DEBUG
#endif
servoXaxis.attach(9); // attaches the servo on pin 9 to the servo object
servoYaxis.attach(10); // attaches the servo on pin 9 to the servo object
Serial.println("READY");
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
readData(Serial.read());
} else {
if (valueX == true && valueY == true) {
Serial << "==Move Servo==" << endl;
#ifdef DEBUG
Serial << "New position deg.=>" << posXaxis << "/" << posYaxis << endl;
Serial << "New position msec=>" << degree2ms(posXaxis/10) << "/" << degree2ms(newYAxis/10) << endl;
#endif
servoXaxis.writeMicroseconds(degree2ms(posXaxis/10));
servoYaxis.writeMicroseconds(degree2ms(posYaxis/10));
valueX = false;
valueY = false;
}
}
}
/**===========================================
This routine wil decode a received string
in this format "123.4,567.8\n" into two
numbers to control a servo
===========================================**/
void readData(char ch)
{
static unsigned int value = 0;
if (ch >= '0' && ch <= '9')
value = (value * 10) + (ch - '0');
if (ch == ',')
{
posXaxis = value;
#ifdef DEBUG
Serial << "X=>" << posXaxis << "/" << value << "=>" << degree2ms(posXaxis/10) << endl;
#endif
value = 0;
valueX = true;
}
if (ch == '\n')
{
posYaxis = value;
#ifdef DEBUG
Serial << "X=>" << posYaxis << "/" << value << "=>" << degree2ms(posYaxis/10) << endl;
#endif
value = 0;
valueY = true;
}
}
unsigned int degree2ms(float degrees)
{
// return 1000 + degrees * 150 / 27;
return 1000 + degrees * 150 / 27;
}
The input /output true the terminal is this:
ArduKServo V0.0
READY
X=>0/0=>1000
X=>0/0=>1000
==Move Servo==
New position deg.=>0/0
New position msec=>1000/1000
X=>900/900=>1500
X=>900/900=>1500
==Move Servo==
New position deg.=>900/900
New position msec=>1500/1000
X=>1800/1800=>2000
X=>1800/1800=>2000
==Move Servo==
New position deg.=>1800/1800
New position msec=>2000/1000
The X servo runs to the correct pos. But the Y servo doenst move at all, but in the redaData() function its OK but after that posYaxis becomes zero.. Any suggestions/remarks??