Serial communication between 2 Arduino

Greetz arduino community!

Here's the setup;

-2 arduinos mega board with 2 wires ;
Board1 TX----­>Board2 RX
Board1 RX----­>Board2 TX
and they are sharing ground too
and they are able to communicate (=D)

the problem is:
I want to send 3 pot values from the remote Board to the receiving board.
It has to be continuous since the receiving board is using those values to set 3 servos to these specified values.

I tried this:
-Reading the pot (0-1024)
-Converting it into a byte(0-255)
-Sending over serial XXX
-Sending the byte
-Sending YYY
and so on

and on the receiver end i had a code that when it had received 3 times letter X would then put the next read value into the x variable for the servo.

I know people done that before but i'm no coding guru and i kinda lack the part to make it work :stuck_out_tongue:

basicly,any reference or code or way to send complex data over serial and then decode it for usage would work here.

If you got any of these,I'd be more than happy to be enlighted by your wisdom ^^

thanks!

You probably don't need an error-detection protocol for this, so I suggest something really simple:

  1. Send a linefeed (0x0a).
  2. Send each pot value as a 3-digit hex string.
  3. Send a CR (0x0d).

If you're worried about possible transmission errors, this method increases the odds of detecting them because you only have 18 valid characters (0-9, a-f, CR, and LF) out of a possible 256, and you know you'll get exactly 9 hex digits between each LF/CR pair.

This simple protocol also displays nicely in the serial monitor or a terminal program, making it easier to debug your code.

wow,this look so simple....
still...i don't understand it...any tuto out there to make me understand or something...?

thanks

Also,my application do need to be the more efficient possible in order to minimise data loss or bad data since this could result in a 2000$ loss =P

Something SOMEWHAT along the lines of what you are doing is covered in… Keyboard to microcontroller interface- SC4smka

Ok,I tried hard,like real hard and this is what i’ve been able to do for now:

(Lot of code isn’t used yet but it’s gonna be after so don’t mind it)

The Remote part:

//Software Remote Version 1.0
//Créé le 1 mars 2010
//Révisé le 2 mars 2010
//par Jérôme Gauthier (418-576-4641)
//


int tx=0;  //declaration axe x branche sur analog 0
int ty=1;  //declaration axe y branche sur analog 1
int tz=2;  //declaration axe z branche sur analog 2
int tr=3;  //declaration axe r branche sur analog 3
int xvalue=0;   //variable qui maintient la valeur de l'axe x
int yvalue=0;   //variable qui maintient la valeur de l'axe y
int zvalue=0;   //variable qui maintient la valeur de l'axe z
int rvalue=0;   //variable qui maintient la valeur de l'axe r
byte xdata;            //variable byte qui contient la valeur de l'axe x
byte ydata;            //variable byte qui contient la valeur de l'axe y
byte zdata;            //variable byte qui contient la valeur de l'axe z
byte rdata;            //variable byte qui contient la valeur de l'axe r
int Bmode=12;   //declaration bouton mode branche sur digital 12
int Bready=11;   //declaration bouton ready branche sur digital 11
int readyvalue=0;   //variable qui maintient la valeur du bouton ready
int modevalue=0;   //variable qui maintient la valeur du bouton mode
char buffer;

void setup() 
{ 
  pinMode(tx,INPUT);    //declaration de l'entre x en mode entree
  pinMode(ty,INPUT);    //declaration de l'entre y en mode entree
  pinMode(tz,INPUT);    //declaration de l'entre z en mode entree
  pinMode(tr,INPUT);    //declaration de l'entre r en mode entree
  pinMode(Bmode,INPUT);    //declaration de l'entre bouton mode  en mode entree
  pinMode(Bready,INPUT);    //declaration de l'entre bouton ready  en mode entree
  Serial.begin(9600);    //Amorce la communication serielle
} 

void loop() 
{ 
  lirecapteur();     //effectue la lecture de l'ensemble des capteurs
  packeting();   //fonction de formatage et d'envoi des donnees 
  sending();
  delay(10);
  //incoming();
} 


int lirecapteur()
{
  xvalue=analogRead(tx);  //lecture axe X
  xvalue = map(xvalue, 0, 1023, 0, 255);    // mise a niveau des valeurs de l'axe X
  yvalue=analogRead(ty);  //lecture axe Y
  yvalue = map(yvalue, 0, 1023, 0, 255);    // mise a niveau des valeurs de l'axe Y
  zvalue=analogRead(tz);  //lecture axe Z
  zvalue = map(zvalue, 0, 1023, 0, 255);    // mise a niveau des valeurs de l'axe Z
  rvalue=analogRead(tr);  //lecture axe R
  rvalue = map(rvalue, 0, 1023, 0, 255);    // mise a niveau des valeurs de l'axe R
}

int packeting()
{
  xdata=byte(xvalue);      //convertit la valeur x en byte pour le transfert
  ydata=byte(yvalue);      //convertit la valeur y en byte pour le transfert
  zdata=byte(zvalue);      //convertit la valeur z en byte pour le transfert
  rdata=byte(rvalue);      //convertit la valeur r en byte pour le transfert
}

int sending()
{
  Serial.write('A'); 
  Serial.write(xdata); 
  Serial.write('4'); 
 Serial.write('5'); 
   Serial.write('B');
  /*
Serial.print("RRR");             //envoi le trigger de l'axe R
   Serial.print(rvalue); 
   Serial.print("MMM");             //envoi le trigger du bouton Mode
   Serial.print(modevalue); 
   if (readyvalue==HIGH)
   {
   Serial.print("RRR");             //envoi le trigger du bouton Ready si le bouton est enclanché
   } 
   */
}


int incoming()
{
  if (Serial.available() > 0) {
    buffer = Serial.read();

    if (buffer == 65){ 
    }
  }
}

The receiving part:

//Software Tete Version 1.0
//Créé le 2 mars 2010
//Révisé le 2 mars 2010
//par Jérôme Gauthier (418-576-4641)


#include <Servo.h>

Servo servox;
Servo servoy;
Servo servoz;
int servoxval;
int servoyval;
int servozval;
int motorrval;
int buffer;
int x;
int y;
int z;
int counterx;
int countery;
int counterz;
int counterr;
byte bufferx;
byte buffery;
byte bufferz;
byte bufferr;
int pos;
int bob;

void setup() 
{ 
  servox.attach(9);
  // servoy.attach(8);
  // servoz.attach(7);
  Serial.begin(9600);
} 


void loop() 
{
  com();
  //unpacketing();
  //application();
  Serial.println(bufferx);
  Serial.println(buffery);
  Serial.println(bufferz);

}




void com(){
  int mstart=0;
  int incombuffer=0;
  int i=0;


  while(mstart==0 && Serial.available()>0){
    incombuffer=Serial.read();

    if(incombuffer=65){
      mstart=1;
  
    }

  }
  while(Serial.available()>0 && mstart==1){
    i++;
    incombuffer=Serial.read();
    if(i==1){
      bufferx=incombuffer;
    }

    if(i==2){
      buffery=incombuffer;
    }


    if(i==3){
      bufferz=incombuffer;
    }

    if((incombuffer==66) && (i==4)){
      i=0;
    }

  }
}






void unpacketing()
{
  servoxval=int(bufferx);
  servoxval=map(servoxval,0,255,0,165);
  servoyval=int(buffery);
  servoyval=map(servoyval,0,255,0,165);
  servozval=int(bufferz);
  servozval=map(servozval,0,255,0,165);
  motorrval=int(bufferr);
  motorrval=map(motorrval,0,255,-100,100);

}



void application()
{

  // x=(servoX.read()+servoXval);
  servox.write(servoxval);
  Serial.println(servoxval);



  //y=(servoY.read()+servoYval);
  //servoY.write(y);

  //z=(servoZ.read()+servoZval);
  //servoZ.write(z);

}

it seem that even if i don’t send the A,the 2,3,4 bytes are placed inside bufferx,y,z.so it’s kinda weird.

any advices would be appreciated,thanks

nevermind,got it working:

error was an = instead of an == -_-

thanks for your support

Hi, i´m pretty much interested in this project,..., just a question, where did you made your last correction , remote or receiver code ? do you have any better and stable code working by now ?thanks

gmv

i got some final code but in the end we changed it for a all in one remote solution.I am willing to hand you this piece of software but won't be able till the end of the summer. email me for any more question

jordan2_delta@hotmail.com