Question about serial monitor

Hi!

I was wondering about how to send a decimal number to COM.
I tried Serial.read(incomingByte);, but it doesn't work.

I don't know what function to use so please give me some help.

Board: Arduino Nano
Port: COM4

I was wondering about how to send a decimal number to COM.

To send data from the Arduino to some other device, you do not use read(). You use print(), println(), or write().

To read data coming into the Arduino, you use read(). But, you need to read what was sent. If the sender sent binary data, you use read() to read the byte, and use it. If the sender sent ASCII data, you need to read, using read(), and store the digits (in a NULL terminated char array) until you have read the last one (which can be hard to tell), and then convert the stored digits to an int or float, as appropriate, and then use the value.

Serial.read(incomingByte);

Under some circumstances this will read a byte of data from the serial interface. A byte can hold an integer with a value between 0 and 255. Under no circumstances can it hold a float.

Hi!

Thanks for answering.
I understood your replys, but the problem is when I open serial monitor, it doesn't work as it should.
This program is supposed to calculate the circumference of a circle :slight_smile: .

void setup()
{
	Serial.begin(9600);
}

int incomingByte = 0;

void loop()
{

    if (Serial.available() > 0) {
	int circumference(int incomingByte) ; {
  	float Pi = 3.14159265359; 
  	incomingByte = Serial.read();
  	int number = incomingByte - 48; // if you type 1, you get 49 so this function gives you real number
 	Serial.println(Pi*2*number, 5);


}
}
}

But when I open serial monitor, this happens:

(I send number 2.2 to COM)

2.212.56637
-12.56637
12.56637

Maybe the problem is in [number] function.
How should I change the code to work properly?

arduinonano123

I know incomingByte can't hold a float, but I don't know any other functions I could use.

it doesn't work as it should.

It does. What it doesn't do is work as you expected. All that that means is that your expectations are wrong.

but I don't know any other functions I could use.

How about parseInt() and/or parseFloat()? Or, how about expecting characters, and reading and store the characters in a NULL terminated array of chars, and then using atoi() or atof()?

I didn't use parseInt(), parseFloat(), atoi() or atof() so far.

But it's ok.
I can look for examples on internet and copy the code.

arduinonano123

arduinonano123:
I can look for examples on internet and copy the code.

Or even better, you could read the online documentation.

Making a program to calculate a circumference of a circle is easy.
But if radius is a decimal number or a number higher than 9, there come troubles.
incomingByte function is able to handle only one byte. I don't know how to replace a "incomingByte function.
Is there a function that is able to handle more than a single byte?

Please see reply #5, or read Robin2's serial handling basics thread.

After some browsing on the internet and reading code examples I made this code:

void setup()
{
	Serial.begin(9600);
}
void loop()
{
	char buf[200];
    if (Serial.available() > 0) {
    int circumference(int number);
    float Pi = 3.141592;
    float number = Serial.readBytes(buf,139);
    Serial.println(             );
    Serial.println(Pi*2*number, 6);

}
}

Serial.readBytes function is able to handle more bytes, but the calculations for circumference are always the same :frowning: .

And all those functions you recomended me, i don't know how to use :frowning:

I can't see where you're converting the ASCII characters in the buffer to a floating-point value.

Yes, I made this code with no experiences at all.