user27816: i have a need to use 3 buttons a a simple option/variable selector at sketch start up ...
Pretty much at startup , script must wait for button press ( one of 3 ) , when button is pressed , use that buttons predefined string value as a variable for the loop script further down .. if wanting to change the "variable" then a reboot is required .. 
i know how to digital read in the buttons , but not sure how to 
1) wait for one of the buttons to be pressed to then jump to main loop 
2) Associate a "string" variable to each button 
thinking about it i suppose a If/Else loop will be required for the Input 'wait" and then just if the digital input (button) is pushed then continue .. am i correct ? 
Ideas or better way to do it ? 
Thx
Greg

user7508: When I am searching for something inside a loop, I find that the
simplest thing to do is to just break out of the loop once I found it.
This was frowned upon in the days when structured programming was a hot
thing, but as long as it makes things simpler, you should not be ashamed
to break:
const int button_pins[] = {2, 3, 4};
const char * const urls[] = {"foo.com", "bar.org", "baz.net"};
const char * url;  // the selected URL

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

    // Configure input pins.
    for (int i = 0; i < 3; i++)
        pinMode(button_pins[i], INPUT_PULLUP);

    // Find first button pressed.
    int i;  // button index
    for (i = 0; ; i = i==2 ? 0 : i+1) {
        if (digitalRead(button_pins[i]) == LOW)
            break;
    }

    // The button index selects the URL.
    url = urls[i];

    Serial.print("Selected URL: ");
    Serial.println(url);
}

void loop() {}

The for loop above is written as an endless loop (no end condition).
The expression i = i==2 ? 0 : i+1 means: “if i is 2, make it 0,
otherwise make it i+1”. In other words, we are endlessly looping i
over 0, 1 and 2.
Now, it may well happen that you actually do not care about which button
was pressed once you have selected the proper URL. In this case, you can
use the URL pointer as a “flag” that tells you when you can end the
loop:
// First button pressed selects the URL.
for (int i = 0; !url; i = i==2 ? 0 : i+1) {
    if (digitalRead(button_pins[i]) == LOW)
        url = urls[i];
}

Where !url means “the URL has not been found”. This works because the
C runtime initializes url to zero, which is interpreted as false in
boolean context.
Addendum: If you are likely to add new button/URL pairs, you can
make this easier by structuring the list as an array of structs:
const struct {
    int pin;          // pin number
    const char *url;  // associated URL
} buttons[] = {
    {2, "foo.com"},
    {3, "bar.org"},
    {4, "baz.net"}
    // etc. just add to this list as needed.
};
const size_t button_count = sizeof buttons / sizeof buttons[0];

Then the search becomes:
for (int i = 0; !url; i = i==button_count-1 ? 0 : i+1) {
    if (digitalRead(buttons[i].pin) == LOW)
        url = buttons[i].url;
}


user27817: 1) One solution is to create a function that loops repeatedly, eg while(1), until one of the buttons is pressed. It then returns the button "number", eg 0,1 or 2.
2) In C you would usually remember the index into a string table. So one solution is to store the button number in a global variable and use it to index a "string array", something like char * buttonStrings["B1","B2","B3"]; and to get the string for a button use buttonStrings[buttonNumber];

27816: <p>i have a need to use 3 buttons a a simple option/variable selector at sketch start up ...</p>

<p>Pretty much at startup , script must wait for button press ( one of 3 ) , when button is pressed , use that buttons predefined string value as a variable for the loop script further down .. if wanting to change the "variable" then a reboot is required .. </p>

<p>i know how to digital read in the buttons , but not sure how to </p>

<p>1) wait for one of the buttons to be pressed to then jump to main loop 
2) Associate a "string" variable to each button </p>

<p>thinking about it i suppose a If/Else loop will be required for the Input 'wait" and then just if the digital input (button) is pushed then continue .. am i correct ? </p>

<p>Ideas or better way to do it ? </p>

<p>Thx
Greg</p>
 7508: <p>When I am searching for something inside a loop, I find that the
simplest thing to do is to just <code>break</code> out of the loop once I found it.
This was frowned upon in the days when structured programming was a hot
thing, but as long as it makes things simpler, you should not be ashamed
to <code>break</code>:</p>



<pre class="lang-c++ prettyprint-override"><code>const int button_pins[] = {2, 3, 4};
const char * const urls[] = {"foo.com", "bar.org", "baz.net"};
const char * url;  // the selected URL

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

    // Configure input pins.
    for (int i = 0; i &lt; 3; i++)
        pinMode(button_pins[i], INPUT_PULLUP);

    // Find first button pressed.
    int i;  // button index
    for (i = 0; ; i = i==2 ? 0 : i+1) {
        if (digitalRead(button_pins[i]) == LOW)
            break;
    }

    // The button index selects the URL.
    url = urls[i];

    Serial.print("Selected URL: ");
    Serial.println(url);
}

void loop() {}
</code></pre>

<p>The <code>for</code> loop above is written as an endless loop (no end condition).
The expression <code>i = i==2 ? 0 : i+1</code> means: “if <code>i</code> is <code>2</code>, make it <code>0</code>,
otherwise make it <code>i+1</code>”. In other words, we are endlessly looping <code>i</code>
over 0, 1 and 2.</p>

<p>Now, it may well happen that you actually do not care about which button
was pressed once you have selected the proper URL. In this case, you can
use the URL pointer as a “flag” that tells you when you can end the
loop:</p>

<pre class="lang-c++ prettyprint-override"><code>// First button pressed selects the URL.
for (int i = 0; !url; i = i==2 ? 0 : i+1) {
    if (digitalRead(button_pins[i]) == LOW)
        url = urls[i];
}
</code></pre>

<p>Where <code>!url</code> means “the URL has not been found”. This works because the
C runtime initializes <code>url</code> to zero, which is interpreted as <code>false</code> in
boolean context.</p>

<p><strong>Addendum</strong>: If you are likely to add new button/URL pairs, you can
make this easier by structuring the list as an array of structs:</p>

<pre class="lang-c++ prettyprint-override"><code>const struct {
    int pin;          // pin number
    const char *url;  // associated URL
} buttons[] = {
    {2, "foo.com"},
    {3, "bar.org"},
    {4, "baz.net"}
    // etc. just add to this list as needed.
};
const size_t button_count = sizeof buttons / sizeof buttons[0];
</code></pre>

<p>Then the search becomes:</p>

<pre class="lang-c++ prettyprint-override"><code>for (int i = 0; !url; i = i==button_count-1 ? 0 : i+1) {
    if (digitalRead(buttons[i].pin) == LOW)
        url = buttons[i].url;
}
</code></pre>
 27817: <p>1) One solution is to create a function that loops repeatedly, eg <code>while(1)</code>, until one of the buttons is pressed. It then returns the button "number", eg 0,1 or 2.</p>

<p>2) In C you would usually remember the index into a string table. So one solution is to store the button number in a global variable and use it to index a "string array", something like <code>char * buttonStrings["B1","B2","B3"];</code> and to get the string for a button use <code>buttonStrings[buttonNumber];</code></p>
