C++ 如何在Arduino串行读取()上将char转换为int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26811300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert char to int on Arduino serial read()?
提问by MohammadReza.Bahmani
I send string value from my Android device to Arduino, but I can not convert input serial.read()
to a real integer value.
我将字符串值从我的 Android 设备发送到 Arduino,但我无法将输入转换serial.read()
为真正的整数值。
How can I get an integer number between 1..180 (to control a servo motor)?
如何获得 1..180 之间的整数(控制伺服电机)?
void setup()
{
myservo.attach(9);
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
int c = Serial.read();
if (c == '0')
{
myservo.write(0);
}
else
{
// Servo write also int number
myservo.write(c);
}
}
}
回答by djUniversal
Your issue is a bit more nuanced than you have laid out. Since Serial.read() will give you each character one at a time, if you type "180" in the serial monitor you will get '1' then '8' then '0'.
你的问题比你提出的要微妙一些。由于 Serial.read() 一次会给你一个字符,如果你在串行监视器中输入“180”,你会得到“1”然后是“8”然后是“0”。
When you receive a char and change to an int you will get the char equivalent in ASCII. The value of '0' is actually 48 so you will need to handle that. Then with each successive char you will need to shift the result right by one space (power of 10) and insert the new value in the 1's column to reassemble the angle typed in.
当您收到一个 char 并更改为一个 int 时,您将获得 ASCII 中的 char 等效项。'0' 的值实际上是 48,因此您需要处理它。然后对于每个连续的字符,您需要将结果右移一个空格(10 的幂)并在 1 的列中插入新值以重新组合输入的角度。
Here is some code which should work:
这是一些应该可以工作的代码:
#include <Servo.h>
Servo myservo;
void setup()
{
myservo.attach(9);
Serial.begin(9600);
myservo.write(0); //or another starting value
}
void loop()
{
//reset the values each loop but only reprint
//them to the servo if you hit the while loop
int angle = 0;
while(Serial.available() > 0){
//Get char in and convert to int
char a = Serial.read();
int c = (int)a - 48;
//values will come in one character at a time
//so you need to increment by a power of 10 for
//each character that is read
angle *= 10;
//then add the 1's column
angle += c;
//then write the angle inside the loop
//if you do it outside you will keep writing 0
Serial.println(angle);//TODO this is a check. comment out when you are happy
myservo.write(angle);
}
}
回答by Arton Dorneles
In short, in your case it is more appropriate to use Serial.parseInt()
:
简而言之,在您的情况下,使用更合适Serial.parseInt()
:
void loop() {
if (Serial.available()) {
int c = Serial.parseInt();
myservo.write(c);
}
}
回答by Pat Reardon
What value are you trying to read?
你试图读取什么价值?
Let's say you have a light sensor. It would look like this:
假设您有一个光传感器。它看起来像这样:
int photocellPin = 0;
int photocellReading;
void setup() {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
photcellReading = analogRead(photocellPin);
myservo.write(photocellReading);
}