이 스케치에서 Arduino는 Android 앱에 연결하고 블루투스 연결을 통해 데이터를 수신합니다. 의도 한대로 작동하지만 블루투스가 연결되어 있지 않고 스케치가 직렬 데이터를 수신하지 않으면 스케치가 while 루프에서 멈추게됩니다. 블루투스가 연결되어 있고 직렬 데이터를 수신하고 있다면 제대로 작동하지만 버튼 상태를 읽을 수 있도록 스케치를 while 루프에서 중단시키는 방법을 알 수는 없습니다. while 루프를 중단하여 버튼 상태를 읽는 스케치가 필요합니다. 아무도 아이디어가 없나요? 수신 된 데이터는 다음과 같습니다. L178 : R215 : R-115 : 항상 L 또는 R 다음에 - 기호 또는 빼기 기호가오고 그 뒤에 -255 - 255 숫자가옵니다 항상 기준 :while loop while read button
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);
}
, 특히'else'과'다른 if'. 귀하의 스케치에 관해서는 Arduino를 프로그래밍 한 적이 없지만 루프에서 블루투스 연결 상태를 확인하는 것이 좋습니다. – Sam