Skip to main content
deleted 742 characters in body
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18
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 Mastermaster 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();
}
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();
}
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);
    }
  }
  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);
    }
  }
  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);
  }
} 

void servoDown() {
  for (i = 0; i < 100; i++) {
    servo1.write(i);
  }
}

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();
}
Improved the English.
Source Link
Greenonline
  • 3.2k
  • 7
  • 37
  • 49

So I'm using this movement()movement() function inside the slave Arduino Uno, which basically handles two stepper motors and one servo motorsmotor. When I call this function through the serial.ReadSerial.read() of the Arduino Uno, it works perfectly, both the steppers and the servo.

I have a master Arduino megaMega interfaced with the Uno (mentioned above) when I send a wire message from the megaMega to Arduino to run the movementmovement() 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 readSerial.read() of the Uno, it works absolutely fine.

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.

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.

So I'm using this movement() function inside the slave Arduino Uno, which basically handles two stepper motors and one servo motor. When I call this function through the Serial.read() of the Arduino Uno, it works perfectly, both the steppers and the servo.

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.

Servo motors not working when called upon using i2cI2C

So i'mI'm using this movement() function inside the slave arduino unoArduino Uno, which basically handles two stepper motors and one servo motors, when i. When I call this function through the serial.readRead of the arduino unoArduino Uno it works perfectly, both the steppers and the servo.

But iI have a master arduinoArduino mega interfaced with the unoUno (mentioned above) when iI send a wire message from the mega to arduinoArduino to run the movement function in the unoUno, the steppers work fine, but the servo doesnotdoes not work. Although, although again iI mention that when the function is called upon using the serial read of the unoUno it works absolutely fine.

ImI'm using an adafruitAdafruit motor shield on the arduino unoArduino Uno as well.

OkOK so this is the unoUno (slave code)

void setup()
{
Wire.begin();
}

void loop()
{
//check if a condition appeared from sensor and then send the data to unoUno 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();
}

Servo motors not working when called upon using i2c

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 doesnot work, although again i mention that when the function is called upon using the serial read of the uno it works absolutely fine.

Im using an adafruit motor shield on the arduino uno as well.

Ok so this is the uno(slave 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();
}

Servo motors not working when called upon using I2C

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.

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()
{
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();
}
Source Link
Loading