简体   繁体   English

如何在Arduino串行读取()上将char转换为int?

[英]How do I convert char to int on Arduino serial read()?

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

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.当您收到一个 char 并更改为一个 int 时,您将获得 ASCII 中的 char 等效项。 The value of '0' is actually 48 so you will need to handle that. '0' 的值实际上是 48,因此您需要处理它。 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.然后对于每个连续的字符,您需要将结果右移一个空格(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);
      }


    }

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

I'll do it in another way.我会用另一种方式来做。 First I concat the char to a string and then convert the string into a number, like this:首先,我将字符连接到字符串,然后将字符串转换为数字,如下所示:

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();

is this a correct procedure?这是正确的程序吗? It work for me (I simplified the source code to explain the procedure adopted, so there may be some errors)它对我有用(我简化了源代码来解释所采用的程序,所以可能会有一些错误)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM