【问题标题】:Arduino - Append int value to the end of a string for concatenated variablesArduino - 将 int 值附加到连接变量的字符串末尾
【发布时间】:2014-05-23 18:55:02
【问题描述】:

我想通过顺序输入来扩展 Arduino 按钮和 ForLoop 教程,以检查它们的状态并在按下任何一个时点亮 LED。最终,我只想在一切开始之前对输入进行一次单次扫描,并且任何关闭(或短路)的东西都将从主程序的旋转中取出。

如果引脚是连续的,我会从第一个引脚开始执行 buttonIn++。不幸的是,输入引脚不是连续的,但名称是连续的。

我只想将 int "1" 添加到 char buttonIn = "myButton" 的末尾并 ++ 字符串中的数字。这似乎不像我想象的那么容易。

现在,我可以很容易地用 PHP 做到这一点

<?php

$myButton1="7";
$myButton2="15";
$myButton3="3";
$myButton4="11";
$myButton5="8";


for ($i=0;$i<=5;$i++) {
        $buttonIn="myButton".$i;
        echo $buttonIn." = ".$$buttonIn."\n";
}

?>

然后输出:

myButton1 = 7
myButton2 = 15
myButton3 = 3
myButton4 = 11
myButton5 = 8

完美,我可以得到变量名和它的值。

但是,这不适用于 C。注释掉的行是我迄今为止尝试过的。希望其他人有更好的想法来做到这一点,而不必在预运行循环中指定每个引脚,从而节省空间和时间。

const int myButton1 = 7;
const int myButton2 = 15;
const int myButton3 = 3;

const int ledPin = 13;

int buttonState = 0;
void setup() {
  pinMode(myButton1, INPUT);
  pinMode(myButton2, INPUT);
  pinMode(myButton3, INPUT);
  pinMode(ledPin, OUTPUT);  
}
void loop() {
 char buttonIn[13];
 for (int x=1;x<=5;x++) {
// char buttonIn = "OSD1button",x;
// char buttonIn[13]="OSD1button",x;
// int sprintf(str, "OSD1button%d",x);
// sprintf(buttonIn,"OSD1button%d",x);
// strncat(buttonIn,x,2);
// char nameIn[12]="OSD1button";
//buttonIn=nameIn + x;
// sprintf(buttonIn, "%d", x);
char OSD="OSD1button";
// buttonIn=OSD+itoa(x,OSD,13);
strncpy(buttonIn,OSD,x);
buttonState = digitalRead(buttonIn);

  if (buttonState == HIGH) {
     digitalWrite(ledPin, HIGH);
  } else {
     digitalWrite(ledPin,LOW);
  }
 } 
}

这是当前的错误信息:

Arduino: 1.5.6-r2 (Windows 7), Board: "Arduino Due (Programming Port)"

OSD_Test.ino: In function 'void loop()':
OSD_Test:67: error: invalid conversion from 'const char*' to 'char'
OSD_Test:69: error: invalid conversion from 'char' to 'const char*'
OSD_Test:69: error: initializing argument 2 of 'char* strncpy(char*, const char*, size_t)'
OSD_Test:70: error: invalid conversion from 'char*' to 'uint32_t'
OSD_Test:70: error: initializing argument 1 of 'int digitalRead(uint32_t)'

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

提前致谢!

【问题讨论】:

    标签: c++ string arduino int concatenation


    【解决方案1】:

    由于 Arduino 实际上提供了 StringAdditonOperator http://arduino.cc/en/Tutorial/StringAdditionOperator 您可以使用:

    for(int x = 1; x <=5; x++) {
        String desiredString = "Button"+x;
        Serial.println(desiredString);
    }
    

    将输出:

    Button1
    Button2
    ...
    

    据我从 cmets 了解到,您想做这样的事情:

    int buttonArray[3] = {7,15,3}; //Or on whatever pins your buttons are
    
    // Setup code and anything else you need goes here
    
    void loop() {
        for(int x = 0; x <= 3; x++) {
             int buttonState = digitalRead(buttonArray[x]);
             digitalWrite(ledPin,buttonState);
        }
    }
    

    但请注意,这只会将 LED 引脚的状态更改为读取的最后一个按钮状态。

    【讨论】:

    • 这将完成字符串名称。但是,您现在如何获取刚刚输出的名称的变量值?基本上,使用 for 循环生成名称(Button1、Button2 等),然后从这些变量名称(7、15 等)中提取值。我需要将该输出传递给 digitalRead 之类的东西。 buttonState = digitalRead(buttonIn); 出了哪些错误:OSD_Test.ino: In function 'void loop()': OSD_Test:73: error: cannot convert 'String' to 'uint32_t' for argument '1' to 'int digitalRead(uint32_t)'
    • 你不能这样做,因为数字读取需要一个整数。但我认为你想要做的只是调用:digitalRead(myButton1) 或者我不理解你的意思吗?
    • 没错。我想自动化一个 digitalRead(myButton#) 其中 # 是一个变化的数字。
    • 那么最好使用整数数组并遍历它
    【解决方案2】:

    知道了。这很好用。谢谢。

    const int myButton1 = 7;
    const int myButton2 = 15;
    const int myButton3 = 3;
    const int myButton4 = 27;
    const int myButton5 = 22;
    const int myButton6 = 18;
    const int myButton7 = 23;
    const int myButton8 = 11;
    
    const int myOutput1 = 8;
    const int myOutput2 = 16;
    const int myOutput3 = 4;
    const int myOutput4 = 28;
    const int myOutput5 = 24;
    const int myOutput6 = 19;
    const int myOutput7 = 25;
    const int myOutput8 = 12;
    

    变成

    int myButton[8]={7,15,3,27,22,18,23,11};
    int myOutput[8]={8,16,4,28,24,19,25,12};
    

    然后将其添加到设置中

     for (int i=0;i<8;i++) {
      pinMode(myButton[i], INPUT);
      pinMode(myOutput[i], OUTPUT);
     }
    

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 2020-07-11
      • 2022-12-11
      • 2013-08-24
      • 2020-08-19
      • 2014-03-06
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多