📜 ⬆️ ⬇️

We process lines on Arduino

I came to programming in general, and Arduino programming in particular, to complete zero, about two months ago. Now, in the course of my current activities, I felt the need to master the processing of arduino strings. The usual campaign in Google for information is not pleased with the article, where everything is simple and understandable for dummies written. And so I am here to talk about how the parsing of lines from the serial port was implemented and which pitfalls were encountered on the way. I ask those interested under the cat.

So. Here is an approximate algorithm that I followed:

  1. We go to arduino.ru and look in the type column for everything related to the characters.

  2. We decide what form of representation we will use (I stopped at the String class, because I had an unpleasant experience with a mash-up array).
  3. Convulsively trying to write our function with preference and professional mesh
  4. We are looking for a detailed description of the class.
  5. We are looking for the necessary operators.
  6. We write!

And the algorithm of the main body of the program is simple:

  1. Cyclically check whether there is a readable byte in the com port buffer, if there is, read.
  2. If the received byte is a newline character ('\ n'), then we call the self-writing function of parsing, if not, then add the received byte to the created variable of the String type.
  3. Parsim, finally, a string.
    In fact, because the practical use of string parsing is not on my mind, this function is merely demonstrative in nature, comparing the resulting string with the constant string already recorded in memory. So, using the equals operator, we compare the accepted string with the written one, and, if the operator returns true, we exit the handler function, if not, we compare it with the next one. If the strings are equivalent, then again, exit the handler function, returning the result. Well, if even this condition does not work, then we still exit the function and say that the string is not true.
  4. Depending on the received result with the switch case, select the desired one.
  5. Reset the accepted string, then begin to collect it again.

But finally, the code:
#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; } 


So, I did not understand what was going on? Why doesn't the LED light up? Oh yeah, how did I forget, in the void setup you need to add:
')
 pinMode(led, OUTPUT); 



PS: It is important to set the com port monitor in the Arduino IDE to the “New Line” mode, since in any other string, the string sent will not be followed by its terminator '\ n'.

PPS: I don’t intend to participate in the holivar about the fact that Arduino is not going to participate, learning the basics of programming and algorithmization, I didn’t do anything wrong.

PPPS: If the article is accepted adequately, I will write the following about what happened with the improvement of the functionality of the parsing function. Well, good luck! .

Source: https://habr.com/ru/post/357890/


All Articles