So I'm using this movement()
function inside the slave Arduino Uno, which basically handles two stepper motors and one servo motors, when I call this function through the serial read of the Arduino Uno it works perfectly, both the steppers and the servo.
But I have a master Arduino Mega interfaced with the Uno (mentioned above) when I send a wire message from the mega to Arduino to run the movement function in the Uno, the steppers work fine, but the servo does not work, although again I mention that when the function is called upon using the serial read of the Uno it works absolutely fine.
I'm using an Adafruit motor shield on the Arduino Uno as well.
Ok so this is the Uno (slave code)
void setup()
{
servo1.attach(9);
Serial.begin(9600);
Wire.begin(5);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop()
{
//other stuff
}
void receiveEvent(int howMany)
{
while(Wire.available())
{
char c = Wire.read();
int From = Wire.read();
int To = Wire.read();
int x = 0;
movement(From,To);
}
}
int movement(int from,int to)
{
int X_From = from / 10;
int X_To = to/10;
int Y_From = from % 10;
int Y_To = to % 10;
/* First we have to get the motor position and move the motor to the from position */
/* === START === */
int diff_X = X_From - motor_X;
int diff_Y = Y_From - motor_Y;
int Y_Value = abs(diff_Y * Y_Steps);
servoDown();
if(diff_X != 0)
{
if(diff_X > 0)
{
/* Positive value means the X_Axis motor will move in the forward direction */
int X_Value = abs(diff_X * X_Steps);
X_Forward(X_Value);
}
else
if(diff_X < 0)
{
/* Negative value means the X_Axis motor will move in the backward direction */
int X_Value = abs(diff_X * 400);
X_Backwards(X_Value);
}
}
//Serial.println(X_Value); Serial.println(Y_Value);
if(diff_Y != 0)
{
if(diff_Y > 0)
{
Y_Forward(Y_Value);
}
else
if(diff_Y < 0)
{
Y_Backwards(Y_Value);
}
}
Serial.println("Motor movement done");
/* === END === */
motor_X = X_From;
motor_Y = Y_From;
int diff_X_2 = X_To - motor_X;
int diff_Y_2 = Y_To - motor_Y;
int Y_Value2 = abs(diff_Y_2 * Y_Steps);
servoUp();
if(diff_X_2 != 0)
{
if(diff_X_2 > 0)
{
/* Positive value means the X_Axis motor will move in the forward direction */
int X_Value2 = abs(diff_X_2 * X_Steps);
X_Forward(X_Value2);
}
else
if(diff_X_2 < 0)
{
/* Negative value means the X_Axis motor will move in the backward direction */
int X_Value2 = abs(diff_X_2 * 400);
X_Backwards(X_Value2);
}
}
if(diff_Y_2 != 0)
{
if(diff_Y_2 > 0)
{
Y_Forward(Y_Value2);
}
else
if(diff_Y_2 < 0)
{
Y_Backwards(Y_Value2);
}
}
//Serial.println(X_Value2); Serial.println(Y_Value2);
motor_X = X_To;
motor_Y = Y_To;
Serial.println("piece movement done");
return 1;
}
void servoUp()
{
for (i = 100; i >= 0; i--)
{
servo1.write(i);
// delay(20);
}
}
void servoDown()
{
for (i = 0; i < 100; i++)
{
servo1.write(i);
//delay(20);
}
}
This is the master Mega code
void setup()
{
Wire.begin();
}
void loop()
{
//check if a condition appeared from sensor and then send the data to uno to run the movement function through sendData()
sendData(From,To);
}
void sendData(int From,int To)
{
Wire.beginTransmission(5);
Wire.write('f');
Wire.write(From);
Wire.write(To);
Wire.endTransmission();
}
Any suggestions?