C++ 如何在Arduino串行读取()中将字符转换为int?

C++ 如何在Arduino串行读取()中将字符转换为int?,c++,char,arduino,C++,Char,Arduino,我从Android设备向Arduino发送字符串值,但无法将输入serial.read()转换为实际整数值 如何获得1到180之间的整数(用于控制伺服电机) 你想读什么值 假设你有一个光传感器。它看起来是这样的: int photocellPin = 0; int photocellReading; void setup() { Serial.begin(9600); myservo.attach(9); } void loop() { photcellReading

我从Android设备向Arduino发送字符串值,但无法将输入
serial.read()
转换为实际整数值

如何获得1到180之间的整数(用于控制伺服电机)


你想读什么值

假设你有一个光传感器。它看起来是这样的:

int photocellPin = 0;
int photocellReading;

void setup() {
    Serial.begin(9600);
    myservo.attach(9);
}

void loop() {
    photcellReading = analogRead(photocellPin);
    myservo.write(photocellReading);
}
String num_string="";
byte col=0;
boolean cicle= true;
while(cicle){
 
    char c;


    lcd.setCursor(col,2);
    c=keypad.waitForKey();
    lcd.print(c);
    num_string=num_string+c;
    col++;
    
    if(keypad.waitForKey()=='*'){
      cicle=false;
    }
}
unsigned long num = num_string.toInt();

你的问题比你所说的要微妙得多。由于Serial.read()将一次为您提供一个字符,因此,如果在串行监视器中键入“180”,您将获得“1”,然后是“8”,然后是“0”

当您收到一个字符并更改为int时,您将得到ASCII格式的字符等价物。“0”的值实际上是48,因此您需要处理它。然后,对于每个连续字符,您需要将结果右移一个空格(10的幂),并在1的列中插入新值,以重新组合键入的角度

以下是一些应该有效的代码:

#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);
      }


    }
#包括
伺服myservo;
无效设置()
{ 
附件(9);
Serial.begin(9600);
myservo.write(0);//或其他起始值
} 
void循环()
{ 
//重置每个循环的值,但仅重新打印
//如果你打了while循环,就把它们转移到伺服上
内倾角=0;
while(Serial.available()>0){
//获取字符并转换为int
char a=Serial.read();
int c=(int)a-48;
//值一次只能输入一个字符
//所以你需要以10的幂递增
//读取的每个字符
角度*=10;
//然后添加1的列
角度+=c;
//然后在循环中写入角度
//如果你在外面做,你会继续写0
Serial.println(angle);//TODO这是一个检查。高兴时注释掉
myservo.write(角度);
}
}

简而言之,在您的情况下,更适合使用:


我会用另一种方式做的。首先,我将字符转换为字符串,然后将字符串转换为数字,如下所示:

int photocellPin = 0;
int photocellReading;

void setup() {
    Serial.begin(9600);
    myservo.attach(9);
}

void loop() {
    photcellReading = analogRead(photocellPin);
    myservo.write(photocellReading);
}
String num_string="";
byte col=0;
boolean cicle= true;
while(cicle){
 
    char c;


    lcd.setCursor(col,2);
    c=keypad.waitForKey();
    lcd.print(c);
    num_string=num_string+c;
    col++;
    
    if(keypad.waitForKey()=='*'){
      cicle=false;
    }
}
unsigned long num = num_string.toInt();
这是正确的程序吗?这对我有用
(我简化了源代码以解释所采用的程序,因此可能会有一些错误)

此代码可以很好地用于在串行监视器中输入。当然,如果使用来自另一个设备的串行流,并且它的传输速度足够快,那么肯定会有问题,但这将是从那里快速修复错误。对于这样的简单实现,不需要结束字符,因为串行缓冲区将为空。如果读取代码,角度将设置为1,然后设置为18,然后设置为180。它发生的速度足够快,不会对伺服造成影响。尝试一下。此外,这种方法不需要解析终止字符的代码,除非需要更高级的用例。如果在串行监视器中键入“180”,除非可以在按enter键的几微秒内键入另一个数字,否则将退出while循环以检查串行,并再次启动void循环()以清除该值。它测试了这段代码,正如我所概述的那样工作。他的主要问题是将char转换为int,这就是我演示的内容