我必须使用Android设备通过蓝牙模块来控制Arduino Uno。
该程序正在运行,但是我有一个问题。
如何使每个命令的代码重复多次?
例如,我想重复执行“ walk”命令,直到给出命令“ stop”?
当我输入“行走”命令时,它将继续行走。
这是代码:
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial blue(4, 3); //TX, RX pins of arduino respetively
String command;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
void setup()
{
blue.begin(9600);
Serial.begin(9600);
Serial.println("Ready");
myservo1.attach(9, 600, 2300); // (pin, min, max)
myservo2.attach(10, 600, 2300);
myservo3.attach(11, 600, 2300);
myservo4.attach(12, 600, 2300);
myservo1.write(30); //right leg (0 point)
myservo2.write(170); //lift leg (0 point)
myservo3.write(170); //left hand (0)
myservo4.write(30); //right hand (0)
}
void loop()
{
while (blue.available()) { //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = blue.read(); //Conduct a serial read
command += c; //build the string.
}
if (command.length() > 0) {
Serial.println(command);
if (command == "walk") {
myservo1.write(30);
myservo2.write(160);
myservo4.write(100);
delay(1000);
myservo4.write(30);
delay(1000);
myservo1.write(40);
myservo2.write(170);
myservo3.write(100);
delay(1000);
myservo3.write(170);
delay(1000);
}
if (command == "raise your hands") {
myservo4.write(180);
myservo3.write(20);
}
if (command == "raise your left hand") {
myservo3.write(20);
delay(2000);
myservo3.write(170);
delay(1000);
}
if (command == "raise your right hand") {
myservo4.write(180);
delay(2000);
myservo4.write(30);
delay(1000);
}
if (command == "stop") {
myservo1.write(30);
myservo2.write(170);
myservo3.write(170);
myservo4.write(30);
}
command = "";
}
}