Arduino - 将int值附加到串联变量的字符串末尾

时间:2014-05-23 18:55:03

标签: c++ string arduino int concatenation

我想扩展Arduino Button和ForLoop教程,通过顺序输入来检查它们的状态,并在按下任何一个LED时点亮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.

提前致谢!

2 个答案:

答案 0 :(得分:4)

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

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

将输出:

Button1
Button2
...

据我所知,你想做的事情是这样的:

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-Pin的状态仅更改为最后一次按钮状态读取。

答案 1 :(得分:0)

知道了。这非常有效。感谢。

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