使用I2C调用时伺服电机不工作

时间:2017-01-18 10:30:07

标签: arduino arduino-uno i2c

所以我在奴隶Arduino Uno中使用这个movement()功能,它基本上处理两个步进电机和一个伺服电机,当我通过串行读取Arduino Uno调用此功能时,它完美地工作,步进器和伺服器。

但是我有一个主人Arduino Mega与Uno接口(如上所述)当我从大型发送线路消息到Arduino运行Uno中的移动功能时,步进器工作正常,但伺服不起作用,虽然我再次提到,当使用Uno的串行读取调用该函数时,它工作得非常好。

我也在Arduino Uno上使用Adafruit电机屏蔽。

好的,这是Uno(奴隶码)

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);
      }
}

这是主Mega代码

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();
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

看起来伺服运动不依赖于传输的数据。您是否检查了实际收到的预期值?要记住一件事:read()读取单个字节。您是否尝试传输大于255的值?