Data Input demo sketch

Newcomers are sometimes unsure how to receive data into an Arduino sketch from the Serial Monitor or from a program running on a PC. I thought it would be helpful to write a demo sketch that illustrates a few useful techniques.

The sketch attached to this Post includes 5 self contained functions that illustrate different ways to deal with incoming data.

readSingleChar() reads and saves a single character and its byte value
readSingleDigit(); reads and saves as a number any of the digits 0-9
readSeveralChars(); reads several characters and saves them as a string in a character array
readOneFloat(); reads several numeric characters and saves them as a floating point value
readCSV(); reads a string, an integer and a float separated by commas
for example testing , 123 , 456.78

Newcomers should not be put off by the length of the total sketch. They should just concentrate on one function at a time. The comments will make more sense if the functions are studied in order.

To try (for example) the readSingleDigit() function it is only necessary to comment out all the other functions in loop() as follows

//  readSingleChar();
  readSingleDigit();
//  readSeveralChars();
//  readOneFloat();
//  readCSV();

One thing that these simple examples do NOT have is the ability to identify the correct start and end to a message - it is possible for part of a message to be missed. This is unlikely in this demo because of the 1 second delay between iterations of loop() but it is a real risk in a working program that runs quickly. The demo sketch that I included in the first post of this Thread shows how to deal with this problem. Its technique can be combined with the techniques here.

...R

DemoDataInput.ino (6.88 KB)

Thanks for sharing,

readOneFloat() has problems, in that:
11) is assumes that all the data for the float is in the serial buffer
2) that nothing else is in the buffer except that float

readCSV() assumes that the string contained something, so that strtok() returns a pointer. Further, it assumes the the string contains two commas. It would be far better to test that strtok() returned a pointer before dereferencing that pointer. The function, too assumes that the input is in a specific format, AND that the whole packet is ready to be read and the nothing but the packet is ready to be read.

As examples, I would have problems recommending that people adopt these functions.

Thanks Paul,

This sort of thing is always a balance between completeness and simplicity. I have opted for simplicity. To the best of my knowledge the examples work no matter what rubbish you throw at them. I have deliberately not tried to make universally applicable functions as all the extra code makes it difficult for a newcomer to see the principal message. In that sense I am not expecting anyone to recommend their adoption without reservations.

At the end of my post I referred to the potential problem that the data may be incomplete and provided a link to an example that deals with that problem. I believe it would be counterproductive to deal with that issue in this demo.

Unfortunately as you add (necessary) bells and whistles the examples become more and more specific to a particular case. All I want to achieve with this demo is to point people in the appropriate direction without having to repeat the same stuff over and over in reply to other Threads.

Feel free to add some examples and explanations of your own. It would be convenient to have them all in the same place.

...R

Robin2:
Newcomers are sometimes unsure how to receive data into an Arduino sketch from the Serial Monitor or from a program running on a PC. I thought it would be helpful to write a demo sketch that illustrates a few useful techniques.

The sketch attached to this Post includes 5 self contained functions that illustrate different ways to deal with incoming data.

readSingleChar() reads and saves a single character and its byte value
readSingleDigit(); reads and saves as a number any of the digits 0-9
readSeveralChars(); reads several characters and saves them as a string in a character array
readOneFloat(); reads several numeric characters and saves them as a floating point value
readCSV(); reads a string, an integer and a float separated by commas
for example testing , 123 , 456.78

Newcomers should not be put off by the length of the total sketch. They should just concentrate on one function at a time. The comments will make more sense if the functions are studied in order.

To try (for example) the readSingleDigit() function it is only necessary to comment out all the other functions in loop() as follows

//  readSingleChar();

readSingleDigit();
//  readSeveralChars();
//  readOneFloat();
//  readCSV();




One thing that these simple examples do NOT have is the ability to identify the correct start and end to a message - it is possible for part of a message to be missed. This is unlikely in this demo because of the 1 second delay between iterations of loop() but it is a real risk in a working program that runs quickly. The demo sketch that I included in the first post of [this Thread ](http://forum.arduino.cc/index.php?topic=225329.0)shows how to deal with this problem. Its technique can be combined with the techniques here.

...R

Hello Robin,
After searching for some relevant threads, I though to ask here as it seems to be relevant to the problem at hand!

Problem statement: I want to read a csv file sent from python and do the operation on the Arduino like blinking LED / LED cube!

So , can we use readCSV function for IRIS dataset as it has integers, floats and species strings in the data..
The only thing I see is the string is printed at last coloum in the csv file...if we change the string colum to first to get a handshake for your function, then I thing this function can be called?Right?

MachineLearner:
Hello Robin,
After searching for some relevant threads, I though to ask here as it seems to be relevant to the problem at hand!

This is so old I had forgotten I had written it.

Just start your own Thread with a clear description of your problem and include the program you are having difficulty with.

This Python - Arduino demo may help. Also see Serial Input Basics - simple reliable ways to receive data..

Please don't reply here - the Thread is too old and is not specific to your problem, Just start your own Topic (thread).

...R

Well readCSV. I had no idea about atof, strtok, etc. They don't appear in the Arduino ref. Thanks.

I wonder if this can be easily adapted to use String, Serial.readString(),.parseFloat() etc.

cometmace:
Well readCSV. I had no idea about atof, strtok, etc. They don't appear in the Arduino ref. Thanks.

I wonder if this can be easily adapted to use String, Serial.readString(),.parseFloat() etc.

This is a very old Thread. Have a look at Serial Input Basics

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

If you have any more questions I suggest you start your own Thread (Topic).

...R