1

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); }}
    }
}
4
  • What have you done already? Can you post your current code?
    – jfpoilpret
    Feb 1, 2015 at 20:57
  • Yes. Added to post above
    – ASM
    Feb 1, 2015 at 21:45
  • From your code, there is a conflict for pins 9 and 10, it seems you want to define the as both inputs and outputs!
    – jfpoilpret
    Feb 2, 2015 at 5:11
  • Please note this gets way harder when you use delay(). Feb 3, 2015 at 0:31

1 Answer 1

2

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
4
  • I've looked at the switch (case) before, but didn't get it. Based on your reply, I've constructed this-
    – ASM
    Feb 2, 2015 at 1:50
  • void loop() { {if (digitalRead(9) == HIGH && digitalRead(10) == LOW && digitalRead(11) == LOW && digitalRead(12) == LOW) button = 0;} {if (digitalRead(9) == LOW && digitalRead(10) == HIGH && digitalRead(11) == LOW && digitalRead(12) == LOW) button = 1;} {if (digitalRead(9) == LOW && digitalRead(10) == LOW && digitalRead(11) == HIGH && digitalRead(12) == LOW) button = 2;} ETC then, switch (button) { case 0: // BRAKE digitalWrite(3, HIGH); digitalWrite(4, HIGH); digitalWrite(5, HIGH); digitalWrite(6, HIGH); digitalWrite(7, HIGH); ETC
    – ASM
    Feb 2, 2015 at 1:57
  • Not sure how to format code for comments. Sorry.
    – ASM
    Feb 2, 2015 at 2:07
  • @ASM Google "switch statement" and you'll get a quick description. Basically, it evaluates the value at the very beginning and if it's equal to the first value, then only that code is executed. If it's equal to the second value, only that code is executed (and so on). However, it seems like you need an if statement that's nested since, as far as I'm aware, you can't use the && operator except at the very beginning. Feb 3, 2015 at 0:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.