Skip to content

Instantly share code, notes, and snippets.

@wpcarro
Created November 5, 2025 02:50
Show Gist options
  • Select an option

  • Save wpcarro/253cd3c4774a38c1d02023db5166bed4 to your computer and use it in GitHub Desktop.

Select an option

Save wpcarro/253cd3c4774a38c1d02023db5166bed4 to your computer and use it in GitHub Desktop.
Serial server for toggle pins hi/lo Arduino Leonardo
static const int BLUE = 7;
static const int GREEN = 8;
void setup() {
Serial.begin(115200);
pinMode(BLUE, OUTPUT);
pinMode(GREEN, OUTPUT);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
}
bool cmd_valid(String cmd) {
return (
cmd.length() == 2 &&
(cmd[0] == 'B' || cmd[0] == 'G') &&
(cmd[1] == '0' || cmd[1] == '1')
);
}
void loop() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if (cmd_valid(cmd)) {
// Serial.printf("Got command: \"%s\"\n", cmd);
int value = cmd[1] == '0' ? LOW : HIGH;
int pin = cmd[0] == 'B' ? BLUE : GREEN;
digitalWrite(pin, value);
} else {
// Serial.printf("Unsupported cmd: \"%s\". Ignoring...\n", cmd);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment