Skip to content

Instantly share code, notes, and snippets.

@kittaakos
Created July 31, 2020 13:44
Show Gist options
  • Save kittaakos/678f83005c814d790c02d80472c273fb to your computer and use it in GitHub Desktop.
Save kittaakos/678f83005c814d790c02d80472c273fb to your computer and use it in GitHub Desktop.
turn LED on/off via serial-monitor
String inputString = "";
String copy = "";
bool stringComplete = false;
void setup() {
Serial.begin(9600);
inputString.reserve(200);
copy.reserve(200);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (stringComplete) {
copy = inputString;
copy.trim();
copy.toLowerCase();
if (copy.equals("off")) {
digitalWrite(LED_BUILTIN, LOW);
} else if (copy.equals("on")) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
Serial.println(inputString);
}
inputString = "";
copy = inputString;
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == '\n') {
stringComplete = true;
} else {
inputString += inChar;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment