在Arduino中将ASCII转换为int

在Arduino中将ASCII转换为int,arduino,type-conversion,ascii,Arduino,Type Conversion,Ascii,我试图从串行监视器获取用户输入,以根据输入转动步进电机。但是,我的代码返回的是ASCII值,而不是原始输入 #include <Stepper.h> Stepper small_stepper(steps_per_motor_revolution, 8, 10, 9, 11); void setup() { // Put your setup code here, to run once: Serial.begin(9600); Serial.println("Rea

我试图从串行监视器获取用户输入,以根据输入转动步进电机。但是,我的代码返回的是ASCII值,而不是原始输入

#include <Stepper.h>

Stepper small_stepper(steps_per_motor_revolution, 8, 10, 9, 11);

void setup() {
  // Put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Ready");
}


void loop() {

  // Put your main code here, to run repeatedly:
  int Steps2Take = Serial.read();
  Serial.println(Steps2Take); // Printing
  if (Steps2Take == -1)
    Steps2Take = 0;
  else {
    small_stepper.setSpeed(1000); // Setting speed
    if (Steps2Take > 0)
      small_stepper.step(Steps2Take * 32);
    else
      small_stepper.step(-Steps2Take * 32);
    delay(2);
  }
}
#包括
步进小型步进电机(每转电机步数,8、10、9、11);
无效设置(){
//将安装代码放在此处,以便运行一次:
Serial.begin(9600);
Serial.println(“就绪”);
}
void循环(){
//将主代码放在此处,以便重复运行:
int Steps2Take=Serial.read();
Serial.println(Steps2Take);//打印
如果(步骤s2take==-1)
步骤2=0;
否则{
小型步进电机。设定速度(1000);//设定速度
如果(步骤2大于0)
小型步进机。步骤(步骤2*32);
其他的
小步进机步进(-Steps2Take*32);
延迟(2);
}
}
如果这只是一个“类型转换”问题,您可以使用以下方法:

int a_as_int = (int)'a';

#包括
int num=atoi(“23”)//atoi=ascii到整数
正如有人指出的那样

它解决问题了吗?

只需使用.toInt()函数即可

您应该从序列号中读取字符串,然后将其转换为整数

Serial.print(Serial.readString().toInt());

你可以用三种方法做到这一点!注意,如果数字大于65535,则必须使用长变量。对于小数,使用浮点变量

  • 可以使用toInt()或toFloat(),它们需要字符串类型变量。请注意,toFloat()非常耗时

       // CODE:
       String _int = "00254";
       String _float = "002.54"; 
       int value1 = _int.toInt();
       float value2 = _float.toFloat();
       Serial.println(value1);
       Serial.println(value2);
    
       // OUTPUT:
       254
       2.54
    
  • 你可以用atoi。函数接受字符数组,然后将其转换为整数

       // CODE:
       // For some reason you have to have +1 your final size; if you don't you will get zeros.
       char output[5] = {'1', '.', '2', '3'}; 
       int value1 = atoi(output);
       float value2 = atof(output);
       Serial.print(value1);
       Serial.print(value2);
    
       // OUTPUT:
       1
       1.23
    
  • 如果你有一个字符数组,想把它转换成一个字符串,因为你不知道它的长度…比如消息缓冲区之类的,我不知道。您可以使用下面的命令将其更改为字符串,然后实现toInt()或toFloat()

    //代码:
    char_int[8];
    字符串数据=”;
    对于(int i=0;i<8;i++){
    数据+=(字符)_int[i];
    }
    char buf[data.length()+1];
    data.toCharArray(buf,data.length()+1);
    

  • 块,这在您的演示案例中很好,但在真实场景中会干扰…因为您是新手,我想说的是,如果您愿意,答案是可以接受的。有一个小“提示”将变为绿色:)
       // CODE:
       // For some reason you have to have +1 your final size; if you don't you will get zeros.
       char output[5] = {'1', '.', '2', '3'}; 
       int value1 = atoi(output);
       float value2 = atof(output);
       Serial.print(value1);
       Serial.print(value2);
    
       // OUTPUT:
       1
       1.23
    
      // CODE:
      char _int[8];
      String data = "";
      for(int i = 0; i < 8; i++){
        data += (char)_int[i];
      }
    
      char buf[data.length()+1];
      data.toCharArray(buf, data.length()+1);