如何在串行监视器中移动 2 个独立的伺服系统
How to move 2 separate servos in Serial monitor
#include <Servo.h>
Servo servo1;
Servo servo2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo1.attach(6);
servo1.write(0);
servo2.attach(7);
servo2.write(0);
}
void loop() {
// put your main code here, to run repeatedly:
String readString;
String Q;
String V1Val;
int v1;
//-------------------------------Check Serial Port---------------------------------------
while (Serial.available()) {
delay(1); //delay to allow buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
if (isControl(c)) {
//'Serial.println("it's a control character");
break;
}
readString += c; //makes the string readString
}
}
Q = readString;
//--------Checking Serial Read----------
if(Q.substring(0,1)=="S"){
V1Val = Q.substring(1);
v1 = V1Val.toInt();
delay(100);
servo1.write(v1);
servo2.write(v1);
}
}
我希望此代码与 2 个伺服器一起使用,但在串行监视器中单独使用
示例:S100 将 1 个舵机移动到 100 度
和 Q50 将舵机编号 2 移动到 50 度
我从别人那里得到了这个代码,所以我不知道如何让它分别用于 2 个伺服系统
您可以将相同的想法从 servo1 复制到 servo2:
if (Q.substring(0, 1) == "S") {
V1Val = Q.substring(1);
v1 = V1Val.toInt();
delay(100);
servo1.write(v1);
}
if (Q.substring(0, 1) == "Q") {
V1Val = Q.substring(1);
v1 = V1Val.toInt();
delay(100);
servo2.write(v1);
}
#include <Servo.h>
Servo servo1;
Servo servo2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo1.attach(6);
servo1.write(0);
servo2.attach(7);
servo2.write(0);
}
void loop() {
// put your main code here, to run repeatedly:
String readString;
String Q;
String V1Val;
int v1;
//-------------------------------Check Serial Port---------------------------------------
while (Serial.available()) {
delay(1); //delay to allow buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
if (isControl(c)) {
//'Serial.println("it's a control character");
break;
}
readString += c; //makes the string readString
}
}
Q = readString;
//--------Checking Serial Read----------
if(Q.substring(0,1)=="S"){
V1Val = Q.substring(1);
v1 = V1Val.toInt();
delay(100);
servo1.write(v1);
servo2.write(v1);
}
}
我希望此代码与 2 个伺服器一起使用,但在串行监视器中单独使用 示例:S100 将 1 个舵机移动到 100 度 和 Q50 将舵机编号 2 移动到 50 度 我从别人那里得到了这个代码,所以我不知道如何让它分别用于 2 个伺服系统
您可以将相同的想法从 servo1 复制到 servo2:
if (Q.substring(0, 1) == "S") {
V1Val = Q.substring(1);
v1 = V1Val.toInt();
delay(100);
servo1.write(v1);
}
if (Q.substring(0, 1) == "Q") {
V1Val = Q.substring(1);
v1 = V1Val.toInt();
delay(100);
servo2.write(v1);
}