Basically I used Visual Studio to send a string data which is something like "1.11" for testing purposes.
How to i make use of strtod to convert it back to double and show it on serial monitor to show that it
is successfully transferred and converted.
#include <studio.h> #include <stdlib.h>
int ledPin = A13;
char x[30];
char **ptr;
double h;
Serial.read() reads one byte from the serial buffer, not the entire contents of the buffer. You need to read the buffer byte by byte whilst data is available and store it in a variable, usually an array, for later processing. As has been pointed out the code above will store one byte beyond the bounds of the 30 element array. The link given will show how to read serial data into an array.
I need to read the value from Visual Studio, I sent it to Arduino as a value "1.11" a string. I am trying to convert the value of Serial.read from string to double. I need an example of strtod with Serial.read(); please help me!
I think you misunderstood my main purpose. I want to receive data from other sources to Arduino by using Serial.read();
The data I want is supposedly a string that is yet to be converted to double. (e.g. "1.1" to 1.1) However, I cannot figure out
the correct way to use them to get my desired outcome.
The Arduino will only read the serial input one byte at a time. You need to assemble the bytes back into a string before doing the conversion. The links given show you how to collect each byte and turn it into a string.
I suggest that you get the code working to read the bytes and create a string then, when you can print the string move on to turning it into a float.
In my first example on that page, you could replace:
// here to process incoming serial data after a terminator received
void process_data (char * data)
{
// for now just display it
Serial.println (data);
} // end of process_data
By:
// here to process incoming serial data after a terminator received
void process_data (char * data)
{
float f = strtof (data);
// do something with the number in f
} // end of process_data
I should point out that on the Arduino double is the same as "float" so you may as well use float.