|
大家好,剛開始學Arduino
想詢問一下各位大大們
想藉由COM PORT來輸入值控制兩個STEP一個SERVO馬達轉動來做出類似自動手寫筆的東西
輸入值的格式(__,__,__)
但不知道為什麼
我輸入完值後馬達動做完
靜置一段時間後馬達會送回500,0,0的值
SERVO馬達(SG90)會有滋滋作響的聲音(好似內部有再轉外面沒再轉)
STEP部分就沒問題
程式如下
麻煩各位大大給點TIPS:
#include <Servo.h>
#include<Stepper.h>
const int steps=2048;
//const int stepPerRevolution=2048;
Stepper stepper1(steps,8,10,9,11);
Stepper stepper2(steps,4,6,5,7);
Servo myservo;
void setup()
{
/*set the com port baud rate*/
Serial.begin(9600);
/*set the stepper motor speed*/
stepper1.setSpeed(5);
stepper2.setSpeed(5);
/*set the servo motor and the value from datasheet*/
myservo.attach(3,500,2400);//servo motor control pin13 dataline
myservo.write(90);//let the angle to 90 degree
delay(1000);
Serial.println("please enter the Servoangle:\n");
Serial.println("please enter the xdistance:\n");
Serial.println("please enter the ydistance:\n");
}
void loop()
{
while (Serial.available() > 0) {
int servomoterangle = Serial.parseInt();
int xdistance=Serial.parseInt();
int ydistance=Serial.parseInt();
stepper1.step(xdistance);
delay(500);
stepper2.step(ydistance);
delay(500);
myservo.writeMicroseconds(servomoterangle);
delay(2000);
Serial.print(servomoterangle);
Serial.println("\n");
Serial.print(xdistance);
Serial.println("\n");
Serial.print(ydistance);
Serial.println("\n");
delay(400);
}
}
|
|