Created
June 19, 2016 07:50
-
-
Save yjiro0403/e944551b8389d2b4f1e0e90e70a14eb3 to your computer and use it in GitHub Desktop.
Arduinoで文字列を数値に変換 ref: http://qiita.com/yjiro0403/items/02ba1b3214c9dcfc443d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
if(Serial.available()){ | |
byte var = Serial.read(); | |
int val = var - 0x30; | |
if (val >= 0 && val < 9) { | |
Serial.println(val); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int getNum() { | |
byte var = 'b'; | |
int num=0; | |
// 'a' is for debug | |
while (var != '\t' && var != '\0' && var != 'a') { | |
if(Serial.available()){ | |
var = Serial.read(); | |
int i = var - 0x30; | |
if (i > 0 && i < 9){ | |
num = (num*10) + i; | |
} | |
//Serial.println(num); | |
} | |
} | |
return num; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment