Robot arm

Hi I need a lot of help with something I hope im in the correct forum but I have built a robotic arm connected to a four wheel base. The arm is running on an arduino uno and the base is on a mega 2560. Im trying to control the arm using the maga as a master slave set up. The arm works fine when I run it via usb straight to the uno. When I control the arm using the mega I can control up to three servos before the arm starts glitching and stops responding to the commands. I know it has enough power because it works fine with a usb to the uno and I added the pull up resistors to the two communication lines but I still get the same outcome here are the codes:

//Master code
#include <Wire.h>

void setup()
{
  Serial.begin(9600); //serial library

  Wire.begin();
}
//___________________________________________________________
void loop()
//******************************Wire Commandes**********************************
{
  while(Serial.available())
  {
    char key = Serial.read();//whatever is typed in serial will be read
    
    if(key == 't')
    {
      Wire.beginTransmission(5);  //address of device to talk to and be any # between 1-127 (5 is for slave code)
      Wire.write('t');
      Wire.endTransmission();    //end communication
    }
    
    if(key == 'g')
    {
      Wire.beginTransmission(5);
      Wire.write('g');
      Wire.endTransmission();
    }
    
    if(key == 'y')
    {
      Wire.beginTransmission(5);
      Wire.write('y');
      Wire.endTransmission();
    }
    
    if(key == 'h')
    {
      Wire.beginTransmission(5);
      Wire.write('h');
      Wire.endTransmission();
    }
    
    if(key == 'u')
    {
      Wire.beginTransmission(5);
      Wire.write('u');
      Wire.endTransmission();
    }
    
    if(key == 'j')
    {
      Wire.beginTransmission(5);
      Wire.write('j');
      Wire.endTransmission();
    }
    
    if(key == 'i')
    {
      Wire.beginTransmission(5);
      Wire.write('i');
      Wire.endTransmission();
    }
    
    if(key == 'k')
    {
      Wire.beginTransmission(5);
      Wire.write('k');
      Wire.endTransmission();
    }
    
//    if(key == 'o')
//    {
//      Wire.beginTransmission(5);
//      Wire.write('o');
//      Wire.endTransmission();
//    }
//    
//    if(key == 'l')
//    {
//      Wire.beginTransmission(5);
//      Wire.write('l');
//      Wire.endTransmission();
//    }
  
  }//End of while(Serial.available())
//********************************************************************
}//End of void loop
#include <Wire.h>
#include <Servo.h>
//---  Controls guide  ---
//                       -
//t - claw close         -
//g - claw open          -
//y - wrist up
//h - wrist down
//u - elbow up
//j - elbow down
//i - shoulder up
//k -shoulder down
//o - 
//l - 
//
//-------------------------

//******************************************************** SERVO SET UP

Servo clawservo;
Servo wristservo;
Servo elbowservo;
Servo shoulderservo;
//Servo baseservo;

//********************************************** VARIABLE SET UP

int claw = 20;
int wrist = 75;
int elbow = 169;
int shoulder = 180;
//int base = 100;
//---------------------------------------------------------------------

void setup(){
  Wire.begin(5);
  Wire.onReceive(receiveEvent);
//-----------------------TEST------------------------------  
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
//  pinMode(11, OUTPUT);
 
//---------------------------------------------------------
  
  clawservo.attach(3); 
  wristservo.attach(5);
  elbowservo.attach(7);
  shoulderservo.attach(9);
//  baseservo.attach(11);

  
  Serial.begin(9600);
  Serial.println("Ready");
  //------------------------------------------------------------------------------

}

  void loop(){}
  
  void receiveEvent(int howMany)
  {
   while(Wire.available()){ 
//------------------TEST------------------------------

    char key = Wire.read();
    
    if(key == 'H')
    {
      digitalWrite(13,HIGH);
    }
    else if(key == 'L')
    {
      digitalWrite(13,LOW);
    }

//-----------------------------------------------------
     
//******************Home Position**************************
    
    clawservo.write(claw);
    wristservo.write(wrist);
    elbowservo.write(elbow);
    shoulderservo.write(shoulder);
//    baseservo.write(base);
    
//**********************************************************
    
claw = clawservo.read();
wrist = wristservo.read();
elbow = elbowservo.read();
shoulder = shoulderservo.read();
//base = baseservo.read();

//**********************************************************
    
      
//      char key = Wire.read;
//      key = Serial.read();
      
//xxxxxxxxxxxxxxxxxx  Claw Controls  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(key == 't'){
     Serial.print("Current claw value is "); 
     Serial.println(claw);
     if(claw >= 179){
       claw = 179;
       Serial.println("Claw Maxed out");
     }
     else{
     claw = claw + 1;
     clawservo.write(claw);
     }
          
    }
      if(key == 'g'){
     Serial.print("Current claw value is "); 
     Serial.println(claw);
     if(claw <= 1){
       claw = 1;
       Serial.println("Claw Mined out");
     }
     else{
     claw = claw - 1;
     clawservo.write(claw);
     }
          
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxx Wrist Controls xxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(key == 'y'){
     Serial.print("Current Wrist value is "); 
     Serial.println(wrist);
     if(wrist >= 179){
       wrist = 179;
       Serial.println("Wrist Maxed out");
     }
     else{
     wrist = wrist + 1;
     wristservo.write(wrist);
     }
          
    }
      if(key == 'h'){
     Serial.print("Current Wrist value is "); 
     Serial.println(wrist);
     if(wrist <= 1){
       wrist = 1;
       Serial.println("Wrist Mined out");
     }
     else{
     wrist = wrist - 1;
     wristservo.write(wrist);
     }
          
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxx Elbow Controls xxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(key == 'u'){
     Serial.print("Current Elbow value is "); 
     Serial.println(elbow);
     if(elbow >= 179){
       elbow = 179;
       Serial.println("Elbow Maxed out");
     }
     else{
     elbow = elbow + 1;
     elbowservo.write(elbow);
     }
          
    }
      if(key == 'j'){
     Serial.print("Current Elbow value is "); 
     Serial.println(elbow);
     if(elbow <= 1){
       elbow = 1;
       Serial.println("Elbow Mined out");
     }
     else{
     elbow = elbow - 1;
     elbowservo.write(elbow);
     }
          
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxx Shoulder Controls xxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(key == 'i'){
     Serial.print("Current Shoulder value is "); 
     Serial.println(shoulder);
     if(shoulder >= 180){
       shoulder = 180;
       Serial.println("Shoulder Maxed out");
     }
     else{
     shoulder = shoulder + 1;
     shoulderservo.write(shoulder);
     }
          
    }
      if(key == 'k'){
     Serial.print("Current Shoulder value is "); 
     Serial.println(shoulder);
     if(shoulder <= 1){
       shoulder = 1;
       Serial.println("Shoulder Mined out");
     }
     else{
     shoulder = shoulder - 1;
     shoulderservo.write(shoulder);
     }
          
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxx Base Controls xxxxxxxxxxxxxxxxxxxxxxxxxxx
//    if(key == 'o'){
//     Serial.print("Current Base value is "); 
//     Serial.println(base);
//     if(base >= 180){
//       base = 180;
//       Serial.println("Base Maxed out");
//     }
//     else{
//     base = base + 1;
//     baseservo.write(base);
//     }
//          
//    }
//      if(key == 'l'){
//     Serial.print("Current Base value is "); 
//     Serial.println(base);
//     if(base <= 1){
//       base = 1;
//       Serial.println("Base Mined out");
//     }
//     else{
//     base = base - 1;
//     baseservo.write(base);
//     }
//          
//    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    
    }// if(serial.available) end
}//end of void receiveEvent

thanks in advance for any help.

“The I2C bus is not released until the handler returns, so it is probably wise to ensure that the handler is relatively quick to complete. Note, however, that “relatively quick” means there’s a lot of room for the ATmega to work, as on most Arduinos it runs at 16MHz and the I2C communication is only at 100kHz.”

Perhaps your Serial.print() calls are slowing it down too much. Try a higher baud rate on the serial port. If that improves things but doesn’t fix the problem completely, try removing (or at least reducing) the Serial output.

Just to save your eyes how about making this a function?

      Wire.beginTransmission(5);
      Wire.write('i');
      Wire.endTransmission();

Like:

void sendCommand (char which)
  {
      Wire.beginTransmission(5);
      Wire.write(which);
      Wire.endTransmission();
  }  // end of sendCommand

Then replace those batches of 3 function calls with something like:

sendCommand ('i');

 void receiveEvent(int howMany)
  {
...

    if(key == 't'){
     Serial.print("Current claw value is "); 
     Serial.println(claw);
     if(claw >= 179){
       claw = 179;
       Serial.println("Claw Maxed out");
     }
...

You can't do Serial prints inside an ISR (successfully anyway).

Man I can’t thank you enough for your help. Your a life saver you just help me prove my whole project. everything worked down to the base on i2c communication. It’s a bit glitchy at times but overall it works. Im not really familiar with the arduino libraries but I have some experience with C and C++. I was reading that the arduino is defaulted to 7 bit address but can run on 10 bit address in i2c would increasing this help my case. Also, once all the serial.println() were taken out I got everything running so if I switch the baud rate to 57600 would it smooth out some of the glitches or is there some other method that would help based on what has helped this far? Here is my modified code:

//Master code
#include <Wire.h>

void setup()
{
  Serial.begin(19200); //serial library

  Wire.begin();
}
//___________________________________________________________
void loop()
//******************************Wire Commandes**********************************
{
  while(Serial.available())
  {
    char key = Serial.read();//whatever is typed in serial will be read
    
    if(key == 't')
    {
      Wire.beginTransmission(5);  //address of device to talk to and be any # between 1-127 (5 is for slave code)
      Wire.write('t');
      Wire.endTransmission();    //end communication
    }
    
    if(key == 'g')
    {
      Wire.beginTransmission(5);
      Wire.write('g');
      Wire.endTransmission();
    }
    
    if(key == 'y')
    {
      Wire.beginTransmission(5);
      Wire.write('y');
      Wire.endTransmission();
    }
    
    if(key == 'h')
    {
      Wire.beginTransmission(5);
      Wire.write('h');
      Wire.endTransmission();
    }
    
    if(key == 'u')
    {
      Wire.beginTransmission(5);
      Wire.write('u');
      Wire.endTransmission();
    }
    
    if(key == 'j')
    {
      Wire.beginTransmission(5);
      Wire.write('j');
      Wire.endTransmission();
    }
    
    if(key == 'i')
    {
      Wire.beginTransmission(5);
      Wire.write('i');
      Wire.endTransmission();
    }
    
    if(key == 'k')
    {
      Wire.beginTransmission(5);
      Wire.write('k');
      Wire.endTransmission();
    }
    
    if(key == 'o')
    {
      Wire.beginTransmission(5);
      Wire.write('o');
      Wire.endTransmission();
    }
    
    if(key == 'l')
    {
      Wire.beginTransmission(5);
      Wire.write('l');
      Wire.endTransmission();
    }
  
  }//End of while(Serial.available())
//********************************************************************
}//End of void loop
//Slave code
#include <Wire.h>
#include <Servo.h>
//---  Controls guide  ---
//                       -
//t - claw close         -
//g - claw open          -
//y - wrist up
//h - wrist down
//u - elbow up
//j - elbow down
//i - shoulder up
//k -shoulder down
//o - 
//l - 
//
//-------------------------

//******************************************************** SERVO SET UP

Servo clawservo;
Servo wristservo;
Servo elbowservo;
Servo shoulderservo;
Servo baseservo;

//********************************************** VARIABLE SET UP

int claw = 20;
int wrist = 75;
int elbow = 169;
int shoulder = 180;
int base = 100;
//---------------------------------------------------------------------

void setup(){
  Wire.begin(5);
  Wire.onReceive(receiveEvent);
//-----------------------TEST------------------------------  
//  pinMode(13,OUTPUT);
//  digitalWrite(13,LOW);
  
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
 
//---------------------------------------------------------
  
  clawservo.attach(3); 
  wristservo.attach(5);
  elbowservo.attach(7);
  shoulderservo.attach(9);
  baseservo.attach(11);

  
  Serial.begin(19200);
//  Serial.println("Ready");
  //------------------------------------------------------------------------------

}

  void loop(){}
  
  void receiveEvent(int howMany)
  {
   while(Wire.available()){ 
//------------------TEST------------------------------

    char key = Wire.read();
    
//    if(key == 'H')
//    {
//      digitalWrite(13,HIGH);
//    }
//    else if(key == 'L')
//    {
//      digitalWrite(13,LOW);
//    }

//-----------------------------------------------------
     
//******************Home Position**************************
    
    clawservo.write(claw);
    wristservo.write(wrist);
    elbowservo.write(elbow);
    shoulderservo.write(shoulder);
    baseservo.write(base);
    
//**********************************************************
    
claw = clawservo.read();
wrist = wristservo.read();
elbow = elbowservo.read();
shoulder = shoulderservo.read();
base = baseservo.read();

//**********************************************************
    
      
//      char key = Wire.read;
//      key = Serial.read();
      
//xxxxxxxxxxxxxxxxxx  Claw Controls  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(key == 't'){
//     Serial.print("Current claw value is "); 
//     Serial.println(claw);
     if(claw >= 179){
       claw = 179;
//       Serial.println("Claw Maxed out");
     }
     else{
     claw = claw + 1;
     clawservo.write(claw);
     }
          
    }
      if(key == 'g'){
//     Serial.print("Current claw value is "); 
//     Serial.println(claw);
     if(claw <= 1){
       claw = 1;
//       Serial.println("Claw Mined out");
     }
     else{
     claw = claw - 1;
     clawservo.write(claw);
     }
          
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxx Wrist Controls xxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(key == 'y'){
//     Serial.print("Current Wrist value is "); 
//     Serial.println(wrist);
     if(wrist >= 179){
       wrist = 179;
//       Serial.println("Wrist Maxed out");
     }
     else{
     wrist = wrist + 1;
     wristservo.write(wrist);
     }
          
    }
      if(key == 'h'){
//     Serial.print("Current Wrist value is "); 
//     Serial.println(wrist);
     if(wrist <= 1){
       wrist = 1;
//       Serial.println("Wrist Mined out");
     }
     else{
     wrist = wrist - 1;
     wristservo.write(wrist);
     }
          
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxx Elbow Controls xxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(key == 'u'){
//     Serial.print("Current Elbow value is "); 
//     Serial.println(elbow);
     if(elbow >= 179){
       elbow = 179;
//       Serial.println("Elbow Maxed out");
     }
     else{
     elbow = elbow + 1;
     elbowservo.write(elbow);
     }
          
    }
      if(key == 'j'){
//     Serial.print("Current Elbow value is "); 
//     Serial.println(elbow);
     if(elbow <= 1){
       elbow = 1;
//       Serial.println("Elbow Mined out");
     }
     else{
     elbow = elbow - 1;
     elbowservo.write(elbow);
     }
          
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxx Shoulder Controls xxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(key == 'i'){
//     Serial.print("Current Shoulder value is "); 
//     Serial.println(shoulder);
     if(shoulder >= 180){
       shoulder = 180;
//       Serial.println("Shoulder Maxed out");
     }
     else{
     shoulder = shoulder + 1;
     shoulderservo.write(shoulder);
     }
          
    }
      if(key == 'k'){
//     Serial.print("Current Shoulder value is "); 
//     Serial.println(shoulder);
     if(shoulder <= 1){
       shoulder = 1;
//       Serial.println("Shoulder Mined out");
     }
     else{
     shoulder = shoulder - 1;
     shoulderservo.write(shoulder);
     }
          
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//xxxxxxxxxxxxxxxxxx Base Controls xxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(key == 'o'){
//     Serial.print("Current Base value is "); 
//     Serial.println(base);
     if(base >= 180){
       base = 180;
//       Serial.println("Base Maxed out");
     }
     else{
     base = base + 1;
     baseservo.write(base);
     }
          
    }
      if(key == 'l'){
//     Serial.print("Current Base value is "); 
//     Serial.println(base);
     if(base <= 1){
       base = 1;
//       Serial.println("Base Mined out");
     }
     else{
     base = base - 1;
     baseservo.write(base);
     }
          
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    
    }// if(serial.available) end
}//end of void receiveEvent

I always use 115200 myself. Why go slower? And as for your main loop, wouldn't this be easier?

void loop ()
  {
  if (Serial.available())
    {
    Wire.beginTransmission(5);  //address of device to talk to and be any # between 1-127 (5 is for slave code)
    Wire.write(Serial.read ());
    Wire.endTransmission();    //end communication
    }  // end if available
  } // end of loop

You always send the same letter you read, so why do all the "if" tests?

In your slave you could replace all the "if" tests with a "switch". Have a read up on how that works. It won't make it work better, but it will be easier to read.

Yea i have an idea how to use a switch function I'm gonna try and cut down on the code later tonight I was trying to get everything running first to make debugging a little easier but I do wanted cut down the amount of code into functions and your program is a big help thanks.