Created
July 31, 2020 13:44
-
-
Save kittaakos/678f83005c814d790c02d80472c273fb to your computer and use it in GitHub Desktop.
turn LED on/off via serial-monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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