0
0

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 3 years have passed since last update.

ArduinoのSerialモニタから2桁以上の数値を受け取るテンプレート

Last updated at Posted at 2021-06-05

概要

タイトル通り
あると便利だが、ネットに便利なテンプレートが見当たらなかったので

参考:https://qiita.com/hikoalpha/items/633c677f5cd12d536dcc

使用上の注意

image.png

において改行を”LF"のみにしてください。
LFは10進で"10"です。

注意:4桁以上になるとpow関数の誤差が大きくなるので
int(pow(10,3))=999

コード

byte NUMs[100];    //データ保存用
int digit_inv=0;   //保存桁番号逆順

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

void loop() {
  
  if(Serial.available()>0){

    byte data = Serial.read();
    
    if(data != 10){                    //改行で分岐
      if(data >=48 and data<=57){      //受け取ったのが整数なら保存
        digit_inv++;
        NUMs[digit_inv]=data-48;
      }
    }else{
      //整数化
      int hoge = 0;
      int digit_all = digit_inv;
      while(true){
        hoge += NUMs[digit_inv]*int(pow(10,digit_all - digit_inv));
        digit_inv --;
        if(digit_inv < 0){
          break;
        }
      }


      //hoge に結果が保管されている
      Serial.println(hoge);
    }
  }

追記、一応動きますが
digit_invの動きがちょっとおかしいかもしれません
NUMs[0]使ってません

サーボを使うのに使用した例

数値入力だけでなく、
"a"で+10
"s"で+1
"d"で-1
"f"で-10
の機能を追加した結果

# include <Servo.h>
Servo myservo;

# define sw_s A0
# define servo 10

byte NUMs[100];    //データ保存用
int digit_inv = 0; //保存桁番号逆順
int result = 0;
boolean shortcut_flag = false;  //ショートカットキーを使った後ならLFを無視する

int angle;

void setup() {
  pinMode(sw_s, INPUT);
  pinMode(servo, OUTPUT);
  myservo.attach(servo);
  Serial.begin(250000);
  angle = 90;
}

void loop() {

  if (Serial.available() > 0) {

    byte data = Serial.read();

    if (data != 10) {                  //改行で分岐
      if (data >= 48 and data <= 57) { //受け取ったのが整数なら保存
        NUMs[digit_inv] = data - 48;
        digit_inv++;
        shortcut_flag = false;

      } else if (data == 97) {          // a
        result += 10;
        shortcut_flag = true;
        
      } else if (data == 115) {          // s
        result += 1;
        shortcut_flag = true;
        
      } else if (data == 100) {          // d
        result -= 1;
        shortcut_flag = true;
        
      } else if (data == 102) {          // f
        result -= 10;
        shortcut_flag = true;
      }
    } else {
      if (shortcut_flag == false) {
        //整数化
        int hoge = 0;
        digit_inv --;
        int digit_all = digit_inv ;
        while (true) {
          hoge += NUMs[digit_inv] * int(pow(10, digit_all - digit_inv));
          digit_inv --;
          if (digit_inv < 0) {
            digit_inv = 0;
            break;
          }
        }
        result = hoge;

      } else {
        digit_inv = 0;                  //とりあえず初期化
      }
    }
  }

  angle = result;
  if (angle < 0) {
    angle = 0;
  } else if (angle > 180) {
    angle = 180;
  }
  myservo.write(angle);
  Serial.print(result);
  Serial.print(" ");
  Serial.println(angle);
  delay(50);

}

0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?