Skip to content

Instantly share code, notes, and snippets.

@KeitetsuWorks
Created February 13, 2021 17:07
Show Gist options
  • Select an option

  • Save KeitetsuWorks/97befd69e85df423bb2e71f31f77a2e0 to your computer and use it in GitHub Desktop.

Select an option

Save KeitetsuWorks/97befd69e85df423bb2e71f31f77a2e0 to your computer and use it in GitHub Desktop.
String to Int Function
/**
* @file qiita_02ba1b3214c9dcfc443d.ino
* @brief String to Int Function
* @author Keitetsu
* @date 2021/02/13
* @copyright Copyright (c) 2021 Keitetsu
* @par License
* This software is released under the MIT License.
*/
/**
* @brief セットアップ関数
*/
void setup()
{
Serial.begin(9600);
}
/**
* @brief ループ関数
*/
void loop()
{
String line; // 受信文字列
int line_len; // 受信文字列の長さ
long num; // 受信整数
// シリアル通信で1行(改行コードまで)読み込む
line = Serial.readStringUntil('\n');
// 文字列の長さを取得する
line_len = line.length();
// 文字列の長さが1文字以上の場合
if (line_len > 0) {
// 文字列を整数に変換する
num = line.toInt();
// 整数を2倍にする
num *= 2;
// シリアル通信で2倍にした整数を送信する
Serial.println(num);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment