Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将另一个数字后的数字转换为2 algarisms数字?(1然后1=11)-Arduino_Arduino_Numbers_Calculator_Infrared - Fatal编程技术网

将另一个数字后的数字转换为2 algarisms数字?(1然后1=11)-Arduino

将另一个数字后的数字转换为2 algarisms数字?(1然后1=11)-Arduino,arduino,numbers,calculator,infrared,Arduino,Numbers,Calculator,Infrared,所以,我想用Arduino建立一种计算器。我已经设法从红外遥控器读取数据,并将其显示在LCD 16x2显示屏上。例如,如果我键入“1”,屏幕上会显示“1”,但我如何使程序理解,如果在“回车”按钮之前按下两个或多个数字,这是一个具有多个字母的数字?就像按1,那么2和3等于123 我可以使用很多if语句来执行类似“If1在现有的1之后被按下,而不是变量==11”之类的操作,但这并不有用 不管怎样,我该怎么做?或者你能告诉我这类函数/算法的名称,让我期待 谢谢。这里有两种方法。1st将简单地连接一个字

所以,我想用Arduino建立一种计算器。我已经设法从红外遥控器读取数据,并将其显示在LCD 16x2显示屏上。例如,如果我键入“1”,屏幕上会显示“1”,但我如何使程序理解,如果在“回车”按钮之前按下两个或多个数字,这是一个具有多个字母的数字?就像按1,那么2和3等于123

我可以使用很多if语句来执行类似“If1在现有的1之后被按下,而不是变量==11”之类的操作,但这并不有用

不管怎样,我该怎么做?或者你能告诉我这类函数/算法的名称,让我期待


谢谢。

这里有两种方法。1st将简单地连接一个字符串类型,并在回车结束时将其转换为整数

#define _CR 13
String readString;

void setup() {
  Serial.begin(9600);
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if ((readString.length() >0) || ( c == CR_)) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // do whatever you want
    // ...
    //
    //

    readString=""; //empty for next input
  } 
}

另一种方法是使用字符间超时。请注意,通常

while (!Serial.available()) {
  // wait for Serial input
};
inkey = Serial.read();
这是阻塞。下面使用char数组的指针来构建输入,最多5位为一个int16_t(即65535)的长度。嗯,它只处理积极的方面。但是你可以调整它来得到负片,还有其他的命令,比如“+”,“-”,等等

我之所以使用ICT方法,是因为Arduino的IDE串行监视器实用程序默认为无LF/CR。它只会立即发送输入,而不发送任何LF/CR

int16_t last_ms_char; // milliseconds of last recieved character from Serial port.
int8_t buffer_pos; // next position to recieve character from Serial port.
char buffer[6]; // 0-35K+null
int16_t fn_index;
int16_t Serial_Input_Number;

void setup() {

  Serial.begin(115200);
  last_ms_char = millis(); // stroke the inter character timeout.
  buffer_pos = 0; // start the command string at zero length.

}

void loop() {

  char inByte;
  if (Serial.available() > 0) {
    inByte = Serial.read();
    if (isDigit(inByte)) { // macro for ((inByte >= '0') && (inByte <= '9'))
      // else if it is a number, add it to the string
      buffer[buffer_pos++] = inByte;
    } else {
      // input char is a letter command
      buffer_pos = 0;
      parse_menu(inByte);
    }
    buffer[buffer_pos] = 0; // update end of line
    last_ms_char = millis(); // stroke the inter character timeout.

  } else if ((millis() - last_ms_char) > 500 && ( buffer_pos > 0 )) {
    // ICT expired and have something
    if (buffer_pos == 1) {
      // look for single byte (non-number) menu commands
      parse_menu(buffer[buffer_pos - 1]);

    } else if (buffer_pos > 5) {
      // dump if entered command is greater then uint16_t
      Serial.println(F("Ignored, Number is Too Big!"));

    } else {
      // otherwise its a number, scan through files looking for matching index.
      Serial_Input_Number = atoi(buffer);

      //
      //
      // Do something with "Serial_Input_Number"
      // one time here. Or set flag and do something out of this big if
      // ...
      //
      //


    }

    //reset buffer to start over
    buffer_pos = 0;
    buffer[buffer_pos] = 0; // delimit

    //
    //
    // do other stuff repeatedly between new characters
    // ...
    //
    //
  }
int16最后一个字符;//上次从串行端口接收字符的毫秒数。
int8\u t buffer\u pos;//从串行端口接收字符的下一个位置。
字符缓冲区[6];//0-35K+null
国际16日指数;
int16\u t序列号\u输入\u编号;
无效设置(){
序列号开始(115200);
last_ms_char=millis();//笔划字符间超时。
buffer_pos=0;//以零长度启动命令字符串。
}
void循环(){
半核细胞;
如果(Serial.available()>0){
inByte=Serial.read();
if(isDigit(inByte)){//宏用于((inByte>='0')&&(inByte 500&&(buffer_pos>0)){
//我要吃点东西
如果(缓冲区位置==1){
//查找单字节(非数字)菜单命令
解析菜单(缓冲区[缓冲区位置-1]);
}否则,如果(缓冲器位置>5){
//如果输入的命令大于uint16\u t,则转储
Serial.println(F(“忽略,数字太大!”);
}否则{
//否则它是一个数字,扫描文件寻找匹配的索引。
串行输入号=atoi(缓冲区);
//
//
//使用“序列号输入号”执行操作
//有一次在这里。或者设置旗帜,做一些这样大的事情,如果
// ...
//
//
}
//重置缓冲区以重新开始
缓冲区位置=0;
缓冲区[buffer_pos]=0;//定界
//
//
//在新角色之间重复做其他事情
// ...
//
//
}

无法保证准确的代码,因为它是从更大的示例中剪下来的,可以工作。

虽然它并不完全是我需要的,但它帮助我找到了一种方法,即“+=”运算符。谢谢。