2017-12-24 7 views
0

私はC++でコーディングする初心者ですが、シリアルからの入力を介してledピンを点滅させようとしています。問題はコードの最下部にあると私は思う。Arduino関数は他の関数を呼び出した後にループを維持します

int ledPin = 13; 
String readString; 

void setup() { 
    Serial.begin(115200); 
    pinMode(ledPin, OUTPUT); 
    Serial.println("Serial Blinker (Input 'on' to blink and 'off' to stop blinking)"); 
} 
void loop() { 
    readString = ""; 
    while (Serial.available()) { 
    delay(3); 
    char c = Serial.read(); 
    readString += c; 
    } 
    readString.trim(); 
    if (readString.length() > 0) { 
    if (readString == "on") { 
     Serial.println("switching on"); 
     next(); 
    } 
    } 
} 
void next() { 
    digitalWrite(ledPin, HIGH); 
    delay(500); 
    digitalWrite(ledPin, LOW); 
    delay(500); 
    digitalWrite(ledPin, HIGH); 
    delay(500); 
    digitalWrite(ledPin, LOW); 
    delay(500); 
    readString = ""; 
    while (Serial.available()) { 
    delay(3); 
    char c = Serial.read(); 
    readString += c; 
    } 
    readString.trim(); 
    if (readString.length() > 0) { 
    if (readString == "off") { 
     Serial.println("switching off"); 
     digitalWrite(ledPin, LOW); 
    } 
    } 
    next(); 
} 

答えて

0

int ledPin = 13; 
String readString; 

void setup() { 
    Serial.begin(115200); 
    pinMode(ledPin, OUTPUT); 
    Serial.println("Serial Blinker (Input 'on' to blink and 'off' to stop blinking)"); 
} 
void loop() { 
    readString = ""; 
    while (Serial.available()) { 
    delay(3); 
    char c = Serial.read(); 
    readString += c; 
    } 
    readString.trim(); 
    if (readString.length() > 0) { 
    if (readString == "on") { 
     Serial.println("switching on"); 
     next(); 
    } 
    else if(readString == "off"){ 
     digitalWrite(ledPin, LOW); 
    } 
    } 
} 
void next() { 
    digitalWrite(ledPin, HIGH); 
    delay(500); 
    digitalWrite(ledPin, LOW); 
    delay(500); 
    digitalWrite(ledPin, HIGH); 
    delay(500); 
    digitalWrite(ledPin, LOW); 
    delay(500); 
    readString = ""; 
    while (Serial.available()) { 
    delay(3); 
    char c = Serial.read(); 
    readString += c; 
    } 
    readString.trim(); 

    if (readString == "off") { 
     Serial.print("off"); 
     digitalWrite(ledPin, LOW); 
     off(); 
    } 
    else { 
      next(); 
    } 

} 

void off(){ 
    Serial.print("in off"); 
    digitalWrite(ledPin, LOW); 

} 
このコードを試してみてください
関連する問題