Hello I want to send Data 1 to 200 using serial port of arduino.
But my data format must be in leading zero like below
if my value is 2 then i have to send 002
if my value is 20 then i have to send 020
if my value is 200 then i have to send 200
with custom function.
What does your sketch look like so far?
If you want to display an integer value on Serial Monitor using three digits:
void PrintThreeDigits(unsigned value)
{
if (value < 100)
Serial.print('0');
if (value < 10)
Serial.print('0');
Serial.print(value);
}
That's a neat function John, I'll have to remember that one.
Yes You r right but suppose i want to do specific string like below in C#
where Temp is 0 to 200
serialPort1.Write("{RSV|" + Temp.ToString("000") + "}")
{RSV|002}
So how can i achieve this with Arduino to Send like below
[RSV|002]
nick_avr:
Yes You r right but suppose i want to do specific string like below in C#
where Temp is 0 to 200
serialPort1.Write("{RSV|" + Temp.ToString("000") + "}")
{RSV|002}
So how can i achieve this with Arduino to Send like below
[RSV|002]
Arduino style
Serial.print("[RSV|")
if (value < 100)
Serial.print('0');
if (value < 10)
Serial.print('0');
Serial.print(value);
Serial.print(']');
or C style:
char s[20];
sprintf(s, "[RSV|%03d]", Temp);
Serial.print(s);
or Arduino String style:
String s = "[RSV|";
if (Temp < 100)
s += '0';
if (Temp < 10)
s += '0';
s += Temp;
s += ']';
Serial.print(s);
CrossRoads:
That's a neat function John, I'll have to remember that one.
Will you remember it or practice it?
johnwasser:
or C style:
char s[20];
sprintf(s, "[RSV|%03d]", Temp);
Serial.print(s);
I would like to say that the above snippet is still an "Arduino Style" as Serial.print() does not get executed in Turbo C++/PC Platform.
"Will you remember it or practice it?"
Just save it for now.