Created
January 14, 2017 18:33
-
-
Save bashkirtsevich/4e0c834e9e9220e2aa2f033edac84303 to your computer and use it in GitHub Desktop.
Arduino strings
This file contains hidden or 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
| #define led 13 | |
| String input_string = ""; | |
| const String Led_off = "switch led off"; | |
| const String Led_on = "switch led on"; | |
| bool led_running; | |
| void setup() { | |
| Serial.begin(9600); | |
| } | |
| void loop() { | |
| while (Serial.available() > 0) { | |
| char c = Serial.read(); | |
| if (c == '\n') { | |
| Serial.print("Input_string is: "); | |
| Serial.println(input_string); | |
| switch ( parse(input_string, Led_off, Led_on) ) { | |
| case 10: | |
| led_running=false; | |
| Serial.println("Switching off is done"); | |
| break; | |
| case 11: | |
| led_running=true; | |
| Serial.println("Switching on is done"); | |
| break; | |
| case 0: | |
| Serial.println("invalid String"); | |
| break; | |
| } | |
| input_string = ""; | |
| digitalWrite(led, led_running); | |
| } else { | |
| input_string += c; | |
| } | |
| } | |
| } | |
| byte parse(String input_string, const String Led_off, const String Led_on) { | |
| if (input_string.equals(Led_off) == true) { | |
| return 10; | |
| } | |
| else if (input_string.equals(Led_on) == true) { | |
| return 11; | |
| } | |
| else return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment