How to use strtod to convert a Serial.Read() data?

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;

void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(56700);
}

void loop()
{

if(Serial.available())
{
x[30] = Serial.read();
Serial.println(x);
h = strtod(x,ptr);

if (h == 1.1)
{
digitalWrite(ledPin,HIGH);
delay(1000);

}
else
if( h == 5.1)
{
digitalWrite(ledPin,LOW);
delay(1000);
}
}
}

How to use this forum

Code tags, please.

This is wrong:

char x[30];
...
    x[30] = Serial.read();

An array of 30 elements has them numbered 0 to 29, so the 30th is out of range. Also it's unclear why you are doing this. Try reading:

 x[30] = Serial.read();

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!

Did my link not help?

That was for numbers without a decimal point, but modifying it should be pretty easy.

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.

paperdestiny:
I think you misunderstood my main purpose. I want to receive data from other sources to Arduino by using Serial.read();

I understand you, which is why I wrote that web page. Did you read it?

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.

Do you really need floating point? 'float' is VERY slow for a microcontroller. You will tire out the poor little micro-gerbils running inside the chip :slight_smile:

Yes but they might get bored if they didn't have something to do. :wink: