I wanted to expand on the Arduino Button and ForLoop tutorials by going through sequential inputs to check their status and illuminate the LED if any of them are pressed. Ultimately, I just want to do a single shot scan of the inputs before everything starts and anything that is closed (or shorted out) will be taken out of the rotation in the main program.
If the pins were sequential, I'd just do buttonIn++ starting at the first pin. Unfortunately, the input pins are not sequential but the names are.
I want to just add the int "1" to the end of char buttonIn = "myButton" and ++ the number in the string. That doesn't seem to be as easy as I had thought.
Now, I can do this with PHP easily
<?php
$myButton1="7";
$myButton2="15";
$myButton3="3";
$myButton4="11";
$myButton5="8";
for ($i=0;$i<=5;$i++) {
$buttonIn="myButton".$i;
echo $buttonIn." = ".$$buttonIn."\n";
}
?>
Which then outputs:
myButton1 = 7
myButton2 = 15
myButton3 = 3
myButton4 = 11
myButton5 = 8
Perfect, I can get both the variable name and its value.
However, this doesn't work with C. The commented out lines are what I've tried so far. Hopefully someone else has a better idea to do this without having to specify every single pin in the pre-run loop thus saving space and time.
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);
}
}
}
Here's the current error message:
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.
Thanks in advance!