在此草图中,Arduino连接到Android应用程序并通过蓝牙连接接收数据。它按预期工作,但如果没有连接蓝牙且草图没有接收到串行数据,草图就会卡在while循环中。如果连接蓝牙,它正在接收串行数据,它工作正常,但我无法弄清楚如何使草图断开while循环,以便它可以读取按钮状态。我需要草图来打破while循环来读取按钮状态。有人有什么想法吗?收到的数据看起来像这些例子: L178: R215: L-125: R-115: 总是有一个L或R后跟一个 - 符号或没有减号后跟数字-255到255后跟始终是:
char myChar = 'a';
String string;
char LorR;
int M2INA = 8; //left
int M2INB = 7;
int M2PWM = 9;
int M1INA = 11; //right
int M1INB = 12;
int M1PWM = 10;
int ledB = A2;
int ledR = A3;
int button = 2;
int buttonState;
int val;
int mode = 0;
void setup() {
pinMode(M1INA, OUTPUT);
pinMode(M1INB, OUTPUT);
pinMode(M2INA, OUTPUT);
pinMode(M2INB, OUTPUT);
pinMode(M1PWM, OUTPUT);
pinMode(M2PWM, OUTPUT);
pinMode(ledB, OUTPUT);
pinMode(ledR, OUTPUT);
Serial.begin(57600); //230400 460800 921600
buttonState = digitalRead(button); // read the initial state
}
void loop() {
val = digitalRead(button); // read input value and store it in val
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (mode == 0) { // light is off
mode = 1; // light is on!
} else {
mode = 0; // light is on!
}
}
}
buttonState = val; // save the new state in our variable
if (mode == 0) {
btMode();
}
if (mode == 1) {
followMode();
}
}
void followMode() {
digitalWrite (ledR, HIGH);
digitalWrite (ledB, LOW);
}
void btMode() {
digitalWrite (ledR, LOW);
digitalWrite (ledB, HIGH);
string = "";
//Add to string
while (true) {
myChar = Serial.read();
if (32 <= myChar && myChar <= 127) {
string += myChar;
}
if (myChar == ':'){
break;
}// While End
}
//Analyse string
if (string != "off:" && string != "on:") {
if (string != "L0:" && string != "R0:") {
//code for setting motor left or right
if (string[0] == 'L') {
LorR = 'L';
} else {
LorR = 'R';
}
string.remove(0, 1);
//code for putting it in reverse
if (string[0] == '-') {
string.remove(0, 1);
string.remove((string.length() - 1), 1);
//Serial.println(string);
if (LorR == 'L') {
//digitalWrite(M2EN, HIGH);
digitalWrite (M2INA, LOW);
digitalWrite (M2INB, HIGH);
analogWrite(M2PWM, string.toInt());
} else { //Reverse left
//digitalWrite(M1EN, HIGH);
digitalWrite (M1INA, LOW);
digitalWrite (M1INB, HIGH);
analogWrite(M1PWM, string.toInt());
}
} else {
string.remove((string.length() - 1), 1);
if (LorR == 'L') {
//digitalWrite(M2EN, HIGH);
digitalWrite (M2INA, HIGH);
digitalWrite (M2INB, LOW);
analogWrite(M2PWM, string.toInt());
} else {
digitalWrite (M1INA, HIGH);
digitalWrite (M1INB, LOW);
analogWrite(M1PWM, string.toInt());
}
}
} else {
allStop();
}
}//End of long if statment
if (string == "on:") {
}
if (string == "off:") {
}
}
void allStop () { //coast
analogWrite(M1PWM, 0);
analogWrite(M2PWM, 0);
digitalWrite(M1INA, HIGH);
digitalWrite(M2INA, HIGH);
digitalWrite(M1INB, HIGH);
digitalWrite(M2INB, HIGH);
}
答案 0 :(得分:0)
问题是我无法打破while循环来读取按钮,但这现在有效。我改变了这个
while (true) {
到
while (mode == 0) {
readButton();
并清理了草图。这是决赛:
char myChar = 'a';
String string;
char LorR;
int M2INA = 8; //left
int M2INB = 7;
int M2PWM = 9;
int M1INA = 11; //right
int M1INB = 12;
int M1PWM = 10;
int ledB = A2;
int ledR = A3;
int button = 2;
int buttonState;
int val;
int mode = 0;
unsigned long previousButtonMillis=0;
const int buttonInterval = 50; // number of millisecs between button reading
unsigned long previousSerialReadMillis=0;
const int serialReadInterval = 10; // number of millisecs between button reading
void setup() {
pinMode(M1INA, OUTPUT);
pinMode(M1INB, OUTPUT);
pinMode(M2INA, OUTPUT);
pinMode(M2INB, OUTPUT);
pinMode(M1PWM, OUTPUT);
pinMode(M2PWM, OUTPUT);
pinMode(ledB, OUTPUT);
pinMode(ledR, OUTPUT);
Serial.begin(57600); //230400 460800 921600
buttonState = digitalRead(button); // read the initial state
}
void loop() {
readButton();
if (mode == 0) {
btMode();
}
if (mode == 1) {
followMode();
}
}
void readButton() {
if (millis() - previousButtonMillis >= buttonInterval) {
val = digitalRead(button); // read input value and store it in val
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (mode == 0) {
mode = 1;
} else {
mode = 0;
}
}
}
buttonState = val; // save the new state in our variable
previousButtonMillis += buttonInterval;
}
}
void followMode() {
digitalWrite (ledR, HIGH);
digitalWrite (ledB, LOW);
}
void btMode() {
digitalWrite (ledR, LOW);
digitalWrite (ledB, HIGH);
if (millis() - previousSerialReadMillis >= serialReadInterval){
string = "";
while (mode == 0) {
readButton();
myChar = Serial.read();
if (32 <= myChar && myChar <= 127) {
string += myChar;
}
if (myChar == ':') {
break;
}
}
previousSerialReadMillis += serialReadInterval;
} // While End
analyseString();
}
void analyseString() {
if (string != "L0:" && string != "R0:") {
//code for setting motor left or right
if (string[0] == 'L') {
LorR = 'L';
} else {
LorR = 'R';
}
string.remove(0, 1);
//code for putting it in reverse
if (string[0] == '-') {
string.remove(0, 1);
string.remove((string.length() - 1), 1);
if (LorR == 'L') {
digitalWrite (M2INA, LOW);
digitalWrite (M2INB, HIGH);
int leftPWM = string.toInt();
if (leftPWM > 80) {
leftPWM = 80;
}
analogWrite(M2PWM, leftPWM);
} else { //Reverse left
digitalWrite (M1INA, LOW);
digitalWrite (M1INB, HIGH);
int rightPWM = string.toInt();
if (rightPWM > 80) {
rightPWM = 80;
}
analogWrite(M1PWM, rightPWM);
}
} else {
string.remove((string.length() - 1), 1);
if (LorR == 'L') {
digitalWrite (M2INA, HIGH);
digitalWrite (M2INB, LOW);
analogWrite(M2PWM, string.toInt());
} else {
digitalWrite (M1INA, HIGH);
digitalWrite (M1INB, LOW);
analogWrite(M1PWM, string.toInt());
}
}
} else {
allStop();
}
//End of long if statment
}
void allStop () {
analogWrite(M1PWM, 125);
analogWrite(M2PWM, 125);
digitalWrite(M1INA, HIGH);
digitalWrite(M2INA, HIGH);
digitalWrite(M1INB, HIGH);
digitalWrite(M2INB, HIGH);
}