3
2

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 > Link > myprintf() > printf()の書式指定子を使用 > +TimeLibで時刻表示

Last updated at Posted at 2016-06-19
動作確認
ESP-WROOM-02

時計情報を0パディングで表示したい。

参考 http://playground.arduino.cc/Main/Printf
を見ていると独自関数の実装例がある (Creating a printf() wrapperの節)

# include <stdarg.h>
void p(char *fmt, ... ){
        char buf[128]; // resulting string limited to 128 chars
        va_list args;
        va_start (args, fmt );
        vsnprintf(buf, 128, fmt, args);
        va_end (args);
        Serial.print(buf);
}

va_XXX()という関数は可変引数を扱うようだ。初めて見た。

以下のように実装した。
http://qiita.com/exabugs/items/fe46ec45ff2ffd6c5777
を参考にTimeライブラリをインポート済。

esp8266_160619_timeLib.ino
# include <TimeLib.h>
# include <stdarg.h>

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

void myPrintf(char *fmt, ...) 
{
  char buf[128];
  va_list args;
  va_start(args, fmt);
  vsnprintf(buf, 128, fmt, args);
  va_end(args);
  Serial.print(buf);  
}

void loop() {
  myPrintf("%02d:%02d:%02d", hour(), minute(), second() );
  myPrintf(",%02d/%02d/%04d", day(), month(), year() );
  Serial.println(); 

  delay(3000);
}
結果
00:01:58,01/01/1970
00:02:01,01/01/1970
00:02:04,01/01/1970
00:02:07,01/01/1970
00:02:10,01/01/1970
00:02:13,01/01/1970
00:02:16,01/01/1970
00:02:19,01/01/1970
00:02:22,01/01/1970
00:02:25,01/01/1970
00:02:28,01/01/1970
00:02:31,01/01/1970
00:02:34,01/01/1970
00:02:37,01/01/1970
00:02:40,01/01/1970

時刻表示ができるようになった。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?