diff --git a/external_control/external_control.ino b/external_control/external_control.ino index d15fa2f..45922b5 100644 --- a/external_control/external_control.ino +++ b/external_control/external_control.ino @@ -108,6 +108,7 @@ void runCommand(){ String mode; uint8_t pin; String state; + uint8_t pwmState; switch ((char)command) { case 'R': // run setup() again Serial.print("R"); @@ -194,35 +195,72 @@ void runCommand(){ Serial.print(analogRead(analogInputToDigitalPin(analogPin))); Serial.print("s"); return; - case 'r': // read digital value - /*bug: - The following "r" (or any other text you may put there) will never get printed despite this code branch being executed. Don’t ask me why or how. - Tested on Linux Mint 20.1, Arduino IDE version 2:1.0.5+dfsg2-4.1 - Tested with Arduino Uno and Nano 328P - */ - Serial.print("r"); - pin = waitForSerialInput(2).toInt(); - if (pin >= NUM_DIGITAL_PINS) Serial.print("e"); - else { - Serial.print(digitalRead(pin)); - Serial.print("s"); - } - return; - case 'w': // write digital pin - Serial.print("w"); - pin = waitForSerialInput(2).toInt(); - state = waitForSerialInput(1); - if (pin >= NUM_DIGITAL_PINS) Serial.print("e"); - else if (state == "h") { - digitalWrite(pin, HIGH); - Serial.print("s"); - } else if (state == "l") { - digitalWrite(pin, LOW); - Serial.print("s"); - } else { - Serial.print("e"); - } - return; + case 'r': // read digital value + /*bug: + The following "r" (or any other text you may put there) will never get printed despite this code branch being executed. Don’t ask me why or how. + Tested on Linux Mint 20.1, Arduino IDE version 2:1.0.5+dfsg2-4.1 + Tested with Arduino Uno and Nano 328P + */ + Serial.print("r"); + pin = waitForSerialInput(2).toInt(); + if (pin >= NUM_DIGITAL_PINS) Serial.print("e"); + else { + Serial.print(digitalRead(pin)); + Serial.print("s"); + } + return; + case 'w': // write digital pin + Serial.print("w"); + pin = waitForSerialInput(2).toInt(); + state = waitForSerialInput(1); + if (pin >= NUM_DIGITAL_PINS) Serial.print("e"); + else if (state == "h") { + digitalWrite(pin, HIGH); + Serial.print("s"); + } else if (state == "l") { + digitalWrite(pin, LOW); + Serial.print("s"); + } else { + Serial.print("e"); + } + return; + case 'p': // set a pwm pin + Serial.print("p"); + pin = waitForSerialInput(2).toInt(); + pwmState = waitForSerialInput(3).toInt(); + if (digitalPinHasPWM(pin)) { + analogWrite(pin, pwmState); + Serial.print("s"); + } else { + Serial.print("e"); + } + return; + case 't': // toggle a pin + Serial.print("t"); + if (toggleState(waitForSerialInput(2).toInt())==UNKNOWN_PIN) Serial.print("e"); + else Serial.print("s"); + return; + case 'l': + Serial.print("l"); + state = waitForSerialInput(1); + if (state=="h") { + digitalWrite(LED_BUILTIN, HIGH); + Serial.print("s"); + } else if (state=="l") { + digitalWrite(LED_BUILTIN, LOW); + Serial.print("s"); + } else if (state=="t") { + toggleState(LED_BUILTIN); + Serial.print("s"); + } else { + Serial.print("e"); + } + return; + case 'q': + Serial.print("qs"); + while (true) { + } + return; default: Serial.print("e"); } @@ -247,7 +285,6 @@ void setup(){ Serial.read(); } Serial.print("s\r\n"); - // testing } void loop(){