I'm acquiring sensor data on my arduino over the serial port. I'm using some for the serial basics tutorial below which writes characters to an array until an end marker is reached.
When I open the terminal I can see the data flowing in.
How could I make the program just take in 5 values and an get an average and then stop running? For my application I have a switch turning on/off the board at set intervals so I just want to turn it on take the measurements and then I'll time how long that takes and I'll then switch it off.
I gather from here that the recvWithEndMarker() reads the data in the serial receive buffer and adds each character to an empty array until the end marker is reached. It then set the Boolean newData to true and when this is true the value is displayed by showNewData().
The sensor has a TTL output. The output is an ASCII capital “R”, followed by four ASCII character digits representing the range in millimeters, followed by a carriage return (ASCII 13). The serial data format is 9600 baud, 8 data bits, no parity, with one stop bit (9600-8-N-1).
This is a screen shot of the data coming in.
How can I figure out how many times to loop to capture just one value.
You need to loop as many times as you need to receive first an 'R' and then loop four times to read the four digits.
If any character isn't a digit, go back to waiting for an 'R'.
The sensor has a TTL output. The output is an ASCII capital "R", followed by four ASCII character digits representing the range in millimeters, followed by a carriage return (ASCII 13). The serial data format is 9600 baud, 8 data bits, no parity, with one stop bit (9600-8-N-1).
On the basis of that information why not use the 3rd example in Serial Input Basics - recvWithStartEndMarkers() - and set the start marker to 'R' and the end marker to '\r'