1

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!

2 Answers 2

4

Since Arduino actually provides a StringAdditonOperator http://arduino.cc/en/Tutorial/StringAdditionOperator you could use:

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

Which will output:

Button1
Button2
...

As far as I understand from the comments you want to do something like this:

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

But be aware that this will change the state of the LED-Pin only to the last button state read.

Sign up to request clarification or add additional context in comments.

4 Comments

That will get the string names done. However, how do you now get the variable's value of the name that was just output? Basically, use for loop to generate the names (Button1, Button2,etc) then pull the values from those variable names (7,15,etc). I need to pass that output to something like digitalRead. buttonState = digitalRead(buttonIn); Which errors out: 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)'
well you cannot do that, because digital read takes an integer. But I think what you want to do is just call: digitalRead(myButton1) Or am I not understanding you right?
That's correct. I wanted to automate a digitalRead(myButton#) where # is a number that changes.
then it would be better to use an integer array and iterate thorugh it
0

Got it. This works great. Thanks.

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;

Becomes

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

Then add this to the setup

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.