user7079: I'm working on a project with 4 inputs and 7 possible outputs on an Arduino Uno.  Based on any combination of those inputs, I need to run a specific "loop" that will repeat until there is a change in ANY of the 4 inputs.  Specifically:

Input 1 = loop 1
Input 2 = loop 2
Input 3 = loop 3
Input 4 = loop 4
Input 2 and 3 = loop 5
Input 2 and 4 = loop 6
No input = do nothing

So I need to constantly read the 4 inputs and change output loops accordingly.  I have basic Arduino knowledge but am stumped.
This is what I've tried - I have 4 input pins and 8 output pins
int outPin = 0;
int inPin = 0;
int CAR = 0;
int pinRead = 0;


void setup()
{
 for (int outPin = 3; outPin < 11; outPin++);  {  //set pins 3 thru 10 as OUTPUT
   pinMode(outPin, OUTPUT);
  }

 for (int inPin = 9; inPin < 13; inPin++);  {    //set pins 9 thru 12 as INPUT
   pinMode(inPin, INPUT);
  }

}

void loop()
{
  for (int CAR = 3; CAR <11; CAR++); {  //set all pins to LOW
   digitalWrite(CAR, LOW);
  }

  // BRAKE

    {while (digitalRead(9 == HIGH) && digitalRead(10 == LOW) && digitalRead (11 == LOW) && digitalRead(12 == LOW)) {
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH); 

  }} 

  // LEFT

    {while (digitalRead(9 == LOW) && digitalRead(10 == HIGH) && digitalRead(11 == LOW) && digitalRead(12 == LOW)) {
    digitalWrite(0, HIGH);
    delay (200);
    digitalWrite(1, HIGH);
    digitalWrite(3, HIGH);
    delay (200);
    digitalWrite(2, HIGH);
    delay (200);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    delay (200);

  // RIGHT

  }}

    {while (digitalRead(9 == LOW) && digitalRead(10 == LOW) && digitalRead(11 == HIGH) && digitalRead(12 == LOW)) {
    digitalWrite(5, HIGH);
    delay (200);
    digitalWrite(4, HIGH);
    digitalWrite(6, HIGH);
    delay (200);
    digitalWrite(7, HIGH);
    delay (200);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay (200);

  // HAZARD

  }}

    {while (digitalRead(9 == LOW) && digitalRead(10 == LOW) && digitalRead(11 == LOW) && digitalRead(12 == HIGH)) {
    digitalWrite(0, HIGH);
    digitalWrite(5, HIGH);
    delay (200);
    digitalWrite(1, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    delay (200);
    digitalWrite(2, HIGH);
    digitalWrite(7, HIGH);
    delay (200);

      for (int CAR = 0; CAR < 8; CAR++); {  //set all pins to LOW
       digitalWrite(CAR, LOW);
    delay (200);}}

  // BRAKELEFT

  }

    {while ( digitalRead(9 == HIGH) && digitalRead(10 == HIGH) && digitalRead(11 == LOW) && digitalRead(12 == LOW)) {
    digitalWrite(0, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH); 
    delay (200);
    digitalWrite(1, HIGH);
    digitalWrite(3, HIGH);
    delay (200);
    digitalWrite(2, HIGH);
    delay (200);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    delay (200);
    }
  }

  // BRAKERIGHT

    {while (digitalRead(9 == HIGH) && digitalRead(10 == LOW) && digitalRead(11 == HIGH) && digitalRead(12 == LOW)) {
    digitalWrite(0, HIGH);
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
    digitalWrite(5, HIGH); 
    delay (200);
    digitalWrite(4, HIGH);
    digitalWrite(6, HIGH);
    delay (200);
    digitalWrite(7, HIGH);
    delay (200);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay (200);
    }
  }

  // NOTHING

    {while (digitalRead(9 == LOW) && digitalRead(10 == LOW) && digitalRead(11 == LOW) && digitalRead(12 == LOW)) {

       for (int CAR = 0; CAR < 8; CAR++); {  //set all pins to LOW
         digitalWrite(CAR, LOW); }}
    }
}


user42: When you need repeating behaviour in an Arduino program, putting loops within the loop() function often isn't the best approach. It leads to a lot of confusing logic which is quite hard to get right, and becomes quite unwieldy to maintain.
A more effective approach would be something like a simple state machine. At the start of loop() you would check the input values and figure out which behaviour (or state) you want to execute: BRAKE, LEFT, RIGHT etc.. You might want to store this information in a variable somewhere, e.g. state 0 could be BRAKE, state 1 could be LEFT, and so on.
Next, you simply check what the state value is, and execute the appropriate instructions once. For example:
switch (state)
{
    case 0:
        // BRAKE instructions here...
        break;

    case 1:
        // LEFT instructions here...
        break;

    // etc...
}

There's no need for any while loops because loop() is going to get called all the time anyway. There are two big advantages of this approach. First, you only need all the input conditions in one place, which makes the code easier to manage. And second, it means you can easily have your program doing other things later in loop() because it's no longer stuck in nested loops all the time.

As a side note, it's very important to mention that lines like this are incorrect:
digitalRead(9 == HIGH)

What you're doing here is comparing the number 9 to HIGH, and then passing the result (which is 0) to the digitalRead() function. That means you're effectively only ever checking pin 0. What you actually want to do is pass 9 to the function, and then compare the overall result to HIGH, like this:
digitalRead(9) == HIGH


7079: <p>I'm working on a project with 4 inputs and 7 possible outputs on an Arduino Uno.  Based on any combination of those inputs, I need to run a specific "loop" that will repeat until there is a change in ANY of the 4 inputs.  Specifically:</p>

<ul>
<li>Input 1 = loop 1</li>
<li>Input 2 = loop 2</li>
<li>Input 3 = loop 3</li>
<li>Input 4 = loop 4</li>
<li>Input 2 and 3 = loop 5</li>
<li>Input 2 and 4 = loop 6</li>
<li>No input = do nothing</li>
</ul>

<p>So I need to constantly read the 4 inputs and change output loops accordingly.  I have basic Arduino knowledge but am stumped.</p>

<p>This is what I've tried - I have 4 input pins and 8 output pins</p>

<pre><code>int outPin = 0;
int inPin = 0;
int CAR = 0;
int pinRead = 0;


void setup()
{
 for (int outPin = 3; outPin &lt; 11; outPin++);  {  //set pins 3 thru 10 as OUTPUT
   pinMode(outPin, OUTPUT);
  }

 for (int inPin = 9; inPin &lt; 13; inPin++);  {    //set pins 9 thru 12 as INPUT
   pinMode(inPin, INPUT);
  }

}

void loop()
{
  for (int CAR = 3; CAR &lt;11; CAR++); {  //set all pins to LOW
   digitalWrite(CAR, LOW);
  }

  // BRAKE

    {while (digitalRead(9 == HIGH) &amp;&amp; digitalRead(10 == LOW) &amp;&amp; digitalRead (11 == LOW) &amp;&amp; digitalRead(12 == LOW)) {
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH); 

  }} 

  // LEFT

    {while (digitalRead(9 == LOW) &amp;&amp; digitalRead(10 == HIGH) &amp;&amp; digitalRead(11 == LOW) &amp;&amp; digitalRead(12 == LOW)) {
    digitalWrite(0, HIGH);
    delay (200);
    digitalWrite(1, HIGH);
    digitalWrite(3, HIGH);
    delay (200);
    digitalWrite(2, HIGH);
    delay (200);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    delay (200);

  // RIGHT

  }}

    {while (digitalRead(9 == LOW) &amp;&amp; digitalRead(10 == LOW) &amp;&amp; digitalRead(11 == HIGH) &amp;&amp; digitalRead(12 == LOW)) {
    digitalWrite(5, HIGH);
    delay (200);
    digitalWrite(4, HIGH);
    digitalWrite(6, HIGH);
    delay (200);
    digitalWrite(7, HIGH);
    delay (200);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay (200);

  // HAZARD

  }}

    {while (digitalRead(9 == LOW) &amp;&amp; digitalRead(10 == LOW) &amp;&amp; digitalRead(11 == LOW) &amp;&amp; digitalRead(12 == HIGH)) {
    digitalWrite(0, HIGH);
    digitalWrite(5, HIGH);
    delay (200);
    digitalWrite(1, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    delay (200);
    digitalWrite(2, HIGH);
    digitalWrite(7, HIGH);
    delay (200);

      for (int CAR = 0; CAR &lt; 8; CAR++); {  //set all pins to LOW
       digitalWrite(CAR, LOW);
    delay (200);}}

  // BRAKELEFT

  }

    {while ( digitalRead(9 == HIGH) &amp;&amp; digitalRead(10 == HIGH) &amp;&amp; digitalRead(11 == LOW) &amp;&amp; digitalRead(12 == LOW)) {
    digitalWrite(0, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH); 
    delay (200);
    digitalWrite(1, HIGH);
    digitalWrite(3, HIGH);
    delay (200);
    digitalWrite(2, HIGH);
    delay (200);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    delay (200);
    }
  }

  // BRAKERIGHT

    {while (digitalRead(9 == HIGH) &amp;&amp; digitalRead(10 == LOW) &amp;&amp; digitalRead(11 == HIGH) &amp;&amp; digitalRead(12 == LOW)) {
    digitalWrite(0, HIGH);
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
    digitalWrite(5, HIGH); 
    delay (200);
    digitalWrite(4, HIGH);
    digitalWrite(6, HIGH);
    delay (200);
    digitalWrite(7, HIGH);
    delay (200);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    delay (200);
    }
  }

  // NOTHING

    {while (digitalRead(9 == LOW) &amp;&amp; digitalRead(10 == LOW) &amp;&amp; digitalRead(11 == LOW) &amp;&amp; digitalRead(12 == LOW)) {

       for (int CAR = 0; CAR &lt; 8; CAR++); {  //set all pins to LOW
         digitalWrite(CAR, LOW); }}
    }
}
</code></pre>
 42: <p>When you need repeating behaviour in an Arduino program, putting loops within the <code>loop()</code> function often isn't the best approach. It leads to a lot of confusing logic which is quite hard to get right, and becomes quite unwieldy to maintain.</p>

<p>A more effective approach would be something like a simple state machine. At the start of <code>loop()</code> you would check the input values and figure out which behaviour (or state) you want to execute: BRAKE, LEFT, RIGHT etc.. You might want to store this information in a variable somewhere, e.g. state <code>0</code> could be BRAKE, state <code>1</code> could be LEFT, and so on.</p>

<p>Next, you simply check what the state value is, and execute the appropriate instructions <strong>once</strong>. For example:</p>

<pre><code>switch (state)
{
    case 0:
        // BRAKE instructions here...
        break;

    case 1:
        // LEFT instructions here...
        break;

    // etc...
}
</code></pre>

<p>There's no need for any <code>while</code> loops because <code>loop()</code> is going to get called all the time anyway. There are two big advantages of this approach. First, you only need all the input conditions in one place, which makes the code easier to manage. And second, it means you can easily have your program doing other things later in <code>loop()</code> because it's no longer stuck in nested loops all the time.</p>

<hr>

<p>As a side note, it's very important to mention that lines like this are incorrect:</p>

<pre><code>digitalRead(9 == HIGH)
</code></pre>

<p>What you're doing here is comparing the number <code>9</code> to <code>HIGH</code>, and then passing the result (which is 0) to the <code>digitalRead()</code> function. That means you're effectively only ever checking pin 0. What you actually want to do is pass <code>9</code> to the function, and then compare the overall result to <code>HIGH</code>, like this:</p>

<pre><code>digitalRead(9) == HIGH
</code></pre>
