Skip to content

Instantly share code, notes, and snippets.

@yjiro0403
Created June 19, 2016 07:50
Show Gist options
  • Save yjiro0403/e944551b8389d2b4f1e0e90e70a14eb3 to your computer and use it in GitHub Desktop.
Save yjiro0403/e944551b8389d2b4f1e0e90e70a14eb3 to your computer and use it in GitHub Desktop.
Arduinoで文字列を数値に変換 ref: http://qiita.com/yjiro0403/items/02ba1b3214c9dcfc443d
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);
}
}
}
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