したがって、私はこの機能をArduino Unoのシリアル読み取りで呼び出すと、基本的に2つのステッピングモータと1つのサーボモータを処理するスレーブArduino Uno内でこのmovement()
機能を使用していますステッパーとサーボの両方が完璧に動作します。サーボモータがI2Cを使用して呼び出されたときに動作しない
しかし私はUnoで動き機能を実行するためにメガからArduinoにワイヤーメッセージを送信すると、上記のUnoとインターフェースしたマスターArduino Megaを持っていますが、ステッパーはうまく動作しますが、サーボは機能しません、私は関数がUnoのシリアル読み取りを使用して呼び出されたときにそれは絶対にうまく動作することをもう一度言及しますが。
私はArduino UnoでもAdafruitモーターシールドを使用しています。
[OK]をので、これはこれは、マスターメガコード
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);
}
}
のですか?
ここにクロス投稿:http://arduino.stackexchange.com/review/first-posts/22194 –