String Manipulation

I am new to Arduino and C and need a little help.
So far the examples have been pretty straight forward.
Can someone give me a little guidance?
Suppose I am sending a serial command via the serial port,
"write2 1" where write2 is DIO2 following the space can be 1 or 0.
How do I pick apart "write2 1" into 2 strings?
I am not really familiar with C.
Thanks in advance!

First, use C-style null-terminated strings, not Strings. Explore this page, which links to a slew of functions, one of which is for string handling.

You can do things like test for character sequences, convert text to integer form, etc.

And don't forget to specify a buffer of sufficient size to hold the largest anticipated string (plus account for the terminator).

See also: Robin2’s serial input basics

Well, some people get all shook up at the mention of Strings, but if your program is fairly simple, the String functions might be easier for you to use. The String and c-string logic flow in a program are similar, so you can always convert to c-string function if String functions give you trouble.

My Opinion is:

invest time once to learn how to code with PStrings and Array of chars to be secure that strings will NEVER EVER cause unknown memory-overflow making your code act strange.

instead of using Strings and investing time and time and time again to find after a very long time that memory-overflow was the problem.

here is a basic introduction
the-evils-of-arduino-strings

best regards

Stefan

1. This is the command: "write2 1" (for example) that you are sending from the InputBox (Fig-1) of Serial Monitor. Assume that you have chosen 'Newline' in the 'Line ending tab' of Serial Monitor.


Figure-1:

2. The command string of Step-1 travels towards UNO as: 'w', 'r', 'i', 't', 'e', '2', ' ', and '1'; where each character is (in fact) in its ASCII codes (Fig-2): 0x77 for w, 0x72 for r and so on.


Figure-2:

3. There is a START bit at the beginning and a STOP bit at the end of every character; as a result, we have a 10-bit frame for each character transmission known as asynchronous frame (Fig-3). Now, we have 8 frames for 8 characters of the command string which is transmitted from the Serial Monitor one after another. After the transmission of the charcaters, the frame for Newline character ('\n' = 0x0A) is transmitted. Considering Bd = 9600, each frame takes about 1/9600*10 = 1.04 ms time to reach to UNO.
asyncFrame.png
Figure-3:

4. When a character arrives at the UNO, the UNO is interrupted; it goes to ISR (interrupt sub routine) and saves the ASCII code of the character into a 64-byte wide FIFO (first-in first-out) type BUFFer (Fig-4).
uartblk.png
Figure-4:

5. We may execute the following codes at the UNO side to receive the charcaters that are coming from Serial Monitor and save them in an array named char myData[20].

char myData[20] = ""; //declare in the global area
byte n = Serial.available();  //check if a character has arrived and got stored in BUFFer
if(n != 0)
{
   byte m = Serial.readBytesUntil('\n', myData, 20); //save characters until Newline character (\n) 
   myData[m] = '\0';    //insert null-charcater in array.
   Serial.print(myData);  //check that Serial Monitor shows: write2 1
}

6. The mapping of the myData[] array is shown below in Fig-5.
arrayMap.png
Figure-5:

7. Based on Fig-5, the following code could be placed at the appropriate place of the sketch to write HIGH (1) at DPin-2 of UNO.

//digitalwrite(DPin, Value);
digitalWrite((myData[5] - 0x30), myData[7]);  //0x32 - 0x30 = 0x02 = 2 = DPin-2/code]

8. The complete sketch.

void setup()
{

}

void loop()
{

}

Please, populate the above 2 functions with codes and post the sketch.

asyncFrame.png

uartblk.png

arrayMap.png