LoginSignup
3
1

More than 1 year has passed since last update.

Arduino Unoでバイナリ出力する

Last updated at Posted at 2021-10-27

概要

テスト用にArduino Unoからバイナリデータを出力させる必要があったので実装した。
1000回ごとにダミーデータとして0xFF 0xFFを出力するようにしている。
出力データは1000回のカウンタをそのまま使っている。

環境

  • Arduino Uno
  • Platform IO

ソースコード

#include <Arduino.h>

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

  Serial.println( "Hello, world!");
}

void loop() {
  int cnt=0;

  typedef union {
    int val;
    byte binary[2];
  } u;

  u u1;

  while(cnt<1000){
    u1.val = cnt;
    Serial.write(u1.binary, 2);
    cnt++;
  }
  u1.val = 65535;
  Serial.write(u1.binary, 2);
}

参考

Arduino の Serial.write とうまく付き合う方法 (BLESerial2 編)
共用体unionで書くのはスマートでわかりやすかった。
Tera termで16進数表示
デバッグはTeraTermのデバッグモードを利用。

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