serial package to control servos

Hi, i´m trying to implement a system do control several servos using 2 modules, the Tx is sending each ppm channel convert to characters to control the respective servo on the Rx board, so i found mem´s code ( http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247570790 ) about serial packet and i made some changes for using 2 channels of my arduino shield (joystick) and the other side 2 servos on the other arduino board (328).

here is the code for the RX board:

#include <Servo.h>

#define NBR_SERVOS 2 // the number of servos, up to 48 for Mega, 12 for other boards

Servo myServos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards

const int servPins[NBR_SERVOS] = {2,3};

void setup()
{
Serial.begin(9600);
for(int i=0; i < NBR_SERVOS; i++)
myServos_.attach(servPins*); _
_
}_
void loop()
_
{_
_
static int pos = 0;_
_
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_

* else if(ch >= 'a' && ch <= 'a'+ NBR_SERVOS) // is ch a letter for one of our servos?
_
{_
_
myServos[ch - 'a'].write(pos); // yes, save the position in the position array _
_
pos = 0;*_

* }*
* }*
}[/color]
[/td][/tr][/table]
and the code for the Tx board:
#include <Servo.h>

int potpin0 = 0; // analog pin 0 used to connect the potentiometer A
int val0; // variable to read the value from the analog pin 0
int potpin1 = 1; // analog pin 1 used to connect the potentiometer B
int val1; // variable to read the value from the analog pin 1

void setup()
{

* Serial.begin(9600);*

}

void loop()
{
* val0 = analogRead(potpin0); // reads the value of the potentiometer (value between 0 and 1023)*
* val0 = map(val0, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)*

* val1 = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)*
* val1 = map(val1, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)*

* Serial.print(val0,DEC), Serial.print("a "); *
* Serial.println("");*

* Serial.print(val1,DEC), Serial.print("b "); *
* Serial.println("");*

* delay(15); // waits for the servo to get there*
}
i tried to connect the Tx pin on the sender side to the Rx pin on the receiver side and wait for the servos to move but nothing happens.
when i send the command over the pc keyboard i can get the servos to move, any considerations on this problem that i have now ? :o
thanks in advance

i tried to connect the Tx pin on the sender side to the Rx pin on the receiver side

That's one of three connections that need to be made. You also need to connect Rx on the sender to Tx on the receiver, and to connect Gnd on the sender to Gnd on the receiver. Have you done that?

Hi Paul, actually you don´t need the 3 wires, only 2... ground & signal..., because 1 is "talking" and the other is "listening"... and i got it working now, i found the problem was one delay timing on the code on the sender board.

thanks

 char ch = [glow]Serial.read()[/glow];

   if(ch >= '0' && ch <= '9')              // is ch a number?  
     pos = pos * 10 + ch - '0';           // yes, accumulate the value
   
   
   else if(ch >= 'a' && ch <= 'a'+  NBR_SERVOS) // is ch a letter for one of our servos?
   {
     myServos[ch - 'a'].write(pos);         // yes, save the position in the position array  
     pos = 0;
     int channel =  ch - 'a';
     int  angle = myServos[channel].read();
     int pulseWidth = myServos[channel].readMicroseconds();
     [glow]Serial.print[/glow]("Servo on pin ");

So, is this the one that is talking, or the one that is listening?

yeah, you right but i don´t consider it in this case... i´m going to correct the receiver sketch.... consider the Serial.read() only .

thanks

Hi all, at the moment i got this thing working with 2 servos and i would like to ask you experienced people, how would i store the servo values in the eeprom when starting up the circuit.

what is the best way to use the eeprom library without having problems with servo timing when sender is turned off. ::slight_smile:

thanks in advance

how would i store the servo values in the eeprom when starting up the circuit.

what is the best way to use the eeprom library without having problems with servo timing when sender is turned off

Let us ask you what you think you want to do.
Why would you store values in EEPROM during startup - wouldn't you want to store them during shut-down, and read them back during startup?

the thing is that i want to store initial servo positions when starting up the RC radio and for that i´ll be pressing a push button placed on the receiver or on transmitter.

If data comunication is lost between TX and Rx for any reason, the receiver will read the eeprom values and place the servos on the pre-programed positions, obvious... is that i can read them back over again during startup as well... what are your thoughts on this issue ?

thanks again