6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

arduinoでsprintf

Posted at

概要

arduinoでフォーマット付きのserial出力が無いので、sprintfを試してみた。

環境

arduino ide ver 1.6.3
arduino uno

写真

sprintf.JPG

サンプルコード

# include <stdio.h>

void setup()
{
	Serial.begin(115200);
	Serial.println("start");
	char buf[44];
	int i = 5;
	float f = 3.0;
	char c = 'a';
	sprintf(buf, "String %s", "Ok");
	Serial.println(buf);
	f = i;
	sprintf(buf, "Float %4.1f", f);
	Serial.println(buf);
	i = f;
	sprintf(buf, "Integer %d", i);
	Serial.println(buf);
	i = f / 2;
	sprintf(buf, "Integer %d",i);
	Serial.println(buf);
	i = (int) f / 2;
	sprintf(buf, "Integer after cast %d", i);
	Serial.println(buf);
	i = 5;
	f = i / 2;
	sprintf(buf, "Float f after divide %4.1f", f);
	Serial.println(buf);
	f = (float) i / 2;
	sprintf(buf, "Float after cast and divide %4.1f", f);
	Serial.println(buf);
	i = c;
	sprintf(buf, "Integer corresponding to a is %d", i);
	Serial.println(buf);
	c = i;
	sprintf(buf, "Char %c", c);
	Serial.println(buf);
	c = (char) i;
	sprintf(buf, "Char after cast %c", c);
	Serial.println(buf);
	f = 98.0;
	c = f;
	sprintf(buf, "Char %c", c);
	Serial.println(buf);
	c = (int) f;
	sprintf(buf, "Char after weird cast %c", c);
	Serial.println(buf);
	c = (char) f;
	sprintf(buf, "Char after cast %c", c);
	Serial.println(buf);
	sprintf(buf, "Integer %d", f / 2 + f);
	Serial.println(buf);
	sprintf(buf, "Integer %d", (int) (f / 2 + f));
	Serial.println(buf);
	i = 255;
	sprintf(buf, "Integer to HEX %X", i);
	Serial.println(buf);
	//sprintf(buf, "Weird Float %5.2f", i / 10);
	//Serial.println(buf);
	//sprintf(buf, "Float %5.2f", (float) (i / 10));
	//Serial.println(buf);
	//sprintf(buf, "Float %5.2f", (float) i / 10);
	//Serial.println(buf);
	//f = 2.5;
	//sprintf(buf, "Float %5.0f", f);
	//Serial.println(buf);
	//i = 2.5;
	//f = i;
	//sprintf(buf, "Float %5.2f", f);
	//Serial.println(buf);
	//f = (float) i;
	//sprintf(buf, "Float %5.2f", f);
	//Serial.println(buf);
	Serial.println("end");
}
void loop()
{
}


6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?