Hi all, i tried to move 2 servos using the maxr´s code for the arduino board decoder wich is connected to another arduino board ...with a few changes and all i got was a lot of characters on the screen.
i´m sending S1100 to servo 1 and P2100 to servo 2, S1 and S2 are servo 1 and 2,...100 is the position .
speed is the same @4800, the Tx is connected to receiver board RX pin .
i also tried mem´s code but i couldn´t make it working .
any help would be nice, thanks
gmv
here is the modified code
#include <Servo.h>
Servo panServo;
Servo tiltServo;
// SERIAL PORT VARS
int serIn; // var that will hold the bytes-in read from the serialBuffer
char serInString[80]; // array that will hold command string (though we'll only need length 3)
int serInIndx = 0;
// SERVO VARS
int icount = 0;
long pval = 0;
long tval = 0;
int servo1Pin = 9; // Control pin for servo motor
int servo2Pin = 10; // Control pin for servo motor
void setup() {
Serial.begin(4800); // connect to the serial port
panServo.attach(servo1Pin);
tiltServo.attach(servo2Pin);
}
void loop() {
//read the serial port and create a string out of what you read
readSerialString();
//try to print out collected information. it will do it only if there actually is some info.
printSerialString();
}
void readSerialString () {
while(Serial.available() > 0)
{
byte inByte = Serial.read();
if(inByte == '\n' || inByte == '\r')
break;
// Do something with the character just read...
serInString[serInIndx] = inByte;
serInIndx++;
serInString[serInIndx]= '\0';
//delay(150);
}
}
//print the string all in one time
//this func as well uses global variables
void printSerialString() {
if( serInIndx > 0) {
int serOutIndx;
//Serial.print("Command: ");
//loop through all bytes in the array and print them out
for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++) {
Serial.print( serInString[serOutIndx] ); //print out the byte at the specified index
}delay(500);
if((serInString[0] == 'S') && (serInString[1] == '1'))
{
//Serial.print("Pan!");
serInString[0] = ' ';
serInString[1] = ' ';
pval = atoi(serInString);
Serial.println();
//Serial.print("Value to pan servo: ");
//Serial.print(pval, DEC);
panServo.write(pval);
//delay(1);
}
if((serInString[0] == 'S') && (serInString[1] == '2'))
{
//Serial.print("Tilt!");
serInString[0] = ' ';
serInString[1] = ' ';
tval = atoi(serInString);
Serial.println();
//Serial.print("Value to tilt servo: ");
//Serial.print(tval, DEC);
tiltServo.write(tval);
// delay(1);
}
Serial.println();
// Re-init
serInIndx = 0;
icount = 0;
pval = 0;
tval = 0 ;
}
}