A Master Writer wich gives the time to a Slave Receiver

Hi guys, I am trying to figure out how to control 2 RC servos using a master/slave architecture. The system I am trying to build is organized as follows:

  • I am using two Arduino Duemilanove boards. One is the Master and gives the time to the other board which is the slave. Time is expressed in number of milliseconds since the first board has started to run his program.
  • Each board control an RC Servo Motor using a sinusoidal function. For this reason, the information about time is necessary and the two board must share the same time.

I attach here a figure and the source code I have written so far:

// Wire Master Writer

#include <Wire.h>
#include <Servo.h>
#include <math.h>

unsigned long time=0;
String tmp;
char message[10];

Servo servo1;
float position_angle;
float amplitude=180;
float period=1000;
float phase=0;
float offset=0;

void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  
  servo1.attach(10);
}


void loop()
{
  time=millis();
  tmp=String(time, DEC);
  tmp.toCharArray(message, 10);
 
  Wire.beginTransmission(4); // transmit to device #4
  Wire.send(message);              // sends one byte  
  Wire.endTransmission();    // stop transmitting
  
  
  position_angle=amplitude*sin(2*PI*time/period+phase)+offset;
  servo1.write(position_angle);
}
// Wire Slave Receiver

#include <Wire.h>
#include <Servo.h>
#include <math.h>

unsigned long time=0;

Servo servo1;
float position_angle;
float amplitude=180;
float period=100;
float phase=0;
float offset=0;

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  
  servo1.attach(10);
}

byte x[4];
void loop()
{

}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  String message;
  char tmp[10];
  while(Wire.available())
  {
    message += Wire.receive(); // receive byte as a character
  }
  message.toCharArray(tmp, 10);
  time=atol(tmp);
  Serial.println(time);
  
  position_angle=amplitude*sin(2*PI*time/period+phase)+offset;
  servo1.write(position_angle);
}

I am having some problems. If the servos are not attached on the board the communication between the master and the slave works fine, but if the servos are connected the slave receives wrong values.
Can someone help me? Is there any other way to make the system work?

Have you got all the grounds connected? How are you powering the servos?

AWOL: Have you got all the grounds connected? How are you powering the servos?

Hi, thanks for your reply. Each servo is powered using the Vin and it is connected to the ground of its own board.

I attach here the scheme.

What exactly is the power supply, and why are you powering the servos from the Arduino?

AWOL: What exactly is the power supply, and whay are you powering the servos from the Arduino?

The power supply is a 12v power adapter for arduino. Do you suggest any other way to power the servos?

Thanks

Your diagram shows 9 volts. Why are you powering the servos from the Arduino?

AWOL:
Your diagram shows 9 volts.
Why are you powering the servos from the Arduino?

I am using the pin Vin of the master board which should be powered by the power adapter. I have done this so that I don’t need to power the slave board. Isn’t this correct?

You said 12volts, the diagram showed 9 volts. That's why I asked. What is supplying power to the Arduino, and what is supplying power to the servo?

AWOL: You said 12volts, the diagram showed 9 volts. That's why I asked. What is supplying power to the Arduino, and what is supplying power to the servo?

Sorry, you are right. The diagram is correct and 9 volts should be enough to power both the servos.

Most servos work on only 6 volts. How much current is your supply providing?

AWOL: Most servos work on only 6 volts. How much current is your supply providing?

I have also tried using 12V. I guess there is something wrong in the source code because, when I power the servos, the slave receives wrong messages from the master. I don't understand.

You seem very confused by current and voltage. Are you supplying the servos from the Arduino's regulated 5V supply?

AWOL: You seem very confused by current and voltage. Are you supplying the servos from the Arduino's regulated 5V supply?

No, I am using the scheme as in the above figure. So, I am using Vin.

VIN. The input voltage to the Arduino board when it's using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin.

So, you're supplying the servo's motor from the Arduino's regulator?

AWOL: So, you're supplying the servo's motor from the Arduino's regulator?

yes, both of them from the regulator of the master board.

And what sort of current do your servos consume?

AWOL: And what sort of current do your servos consume?

I am using these servos: http://www.servodatabase.com/servo/towerpro/mg995

I don't have those servos, so I can't measure the current draw. What do you measure?

AWOL: I don't have those servos, so I can't measure the current draw. What do you measure?

It should be 9.1mA/idle and 450mA no load operating.

And when operating, is it loaded?