Program cycles through if statements in order

Im very new to programming, I have tried in the past but I usually break down and pay someone on fiverr. This helps me learn as well, as I can see what someone did to achieve my desired outcome. Onto the trouble of the day.

I wrote this program, the idea is to wait for a "win" condition which is a relay sending ground back to the pin that has been pulled up. I know there are far more elegant ways to program the desired result but since there are just 4 conditions I figured I would simply write it all out longhand (since I know that the elegant solutions exist... however I have NO IDEA what they are LOL). I'm sure that this is a very basic mistake on my part causing the program to simply run through the if conditions in order rather than waiting for input from the four input "buttons" but I cant find anything on this type of problem (probably because I'm the only idiot making this mistake).

Thanks for looking, and extra thanks for helping! You're welcome for the laugh at my expense (I hope as this would mean it was truly just a stupid omission or error).

  
  pinMode(2,OUTPUT);              //set pin 2 as output PLAYER 1 LOSE VIBRATION
  pinMode(3,OUTPUT);              //set pin 3 as output PLAYER 2 LOSE VIBRATION
  pinMode(4,OUTPUT);              //set pin 4 as output PLAYER 3 LOSE VIBRATION
  pinMode(5,OUTPUT);              //set pin 5 as output PLAYER 4 LOSE VIBRATION
  pinMode(6,OUTPUT);              //set pin 6 as output PLAYER 1 12V SUPPLY
  pinMode(7,OUTPUT);              //set pin 7 as output PLAYER 2 12V SUPPLY
  pinMode(8,OUTPUT);              //set pin 8 as output PLAYER 3 12V SUPPLY
  pinMode(9,OUTPUT);              //set pin 9 as output PLAYER 4 12V SUPPLY
  pinMode(10,INPUT_PULLUP);              //set pin 10 as input PLAYER 1 WIN
  pinMode(11,INPUT_PULLUP);              //set pin 11 as input PLAYER 2 WIN
  pinMode(12,INPUT_PULLUP);              //set pin 12 as input PLAYER 3 WIN
  pinMode(13,INPUT_PULLUP);              //set pin 13 as input PLAYER 4 WIN
  pinMode(A0,INPUT_PULLUP);              //set A0 to input from panic button
  pinMode(A1,OUTPUT);                    //set pin A1 as output PLAYER 1 LOSE SMOKE
  pinMode(A2,OUTPUT);                    //set pin A2 as output PLAYER 2 LOSE SMOKE
  pinMode(A3,OUTPUT);                    //set pin A3 as output PLAYER 3 LOSE SMOKE
  pinMode(A4,OUTPUT);                    //set pin A4 as output PLAYER 4 LOSE SMOKE
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
digitalWrite(A3, HIGH);
digitalWrite(A4, HIGH);



}

void loop() {
 if (digitalRead(10) == HIGH); {  //player 1 wins
    digitalWrite(6, LOW);        //free winner
    digitalWrite(3, LOW);        //player 2 vibrate
    digitalWrite(4, LOW);        //player 3 vibrate
    digitalWrite(5, LOW);        //player 4 vibrate
    delay (3000);                 //delay to lessen smoke
    digitalWrite(A2, LOW);        //player 2 smoke
    digitalWrite(A3, LOW);        //player 3 smoke
    digitalWrite(A4, LOW);        //player 4 smoke
    delay (3000);                 //continue to smoke and vibrate
    digitalWrite(3, HIGH);         //end vibrate
    digitalWrite(4, HIGH);         //end vibrate        
    digitalWrite(5, HIGH);         //end vibrate       
    digitalWrite(A2, HIGH);         //end smoke
    digitalWrite(A3, HIGH);         //end smoke
    digitalWrite(A4, HIGH);         //end smoke
    digitalWrite(7, LOW);        //free player 2
    digitalWrite(8, LOW);        //free player 3
    digitalWrite(9, LOW);        //free player 4
     delay(20000);                 //allow players to stand up
    digitalWrite(6, HIGH);        //restore 12v for next game
    digitalWrite(7, HIGH);        //restore 12v for next game
    digitalWrite(8, HIGH);        //restore 12v for next game
    digitalWrite(9, HIGH);        //restore 12v for next game
    
  }
  if (digitalRead(11) == HIGH); {  //player 2 wins
    digitalWrite(7, LOW);        //free winner
    digitalWrite(2, LOW);        //player 1 vibrate
    digitalWrite(4, LOW);        //player 3 vibrate
    digitalWrite(5, LOW);        //player 4 vibrate
    delay (3000);                 //delay to lessen smoke
    digitalWrite(A1, LOW);        //player 1 smoke
    digitalWrite(A3, LOW);        //player 3 smoke
    digitalWrite(A4, LOW);        //player 4 smoke
    delay (3000);                 //continue to smoke and vibrate
    digitalWrite(2, HIGH);         //end vibrate
    digitalWrite(4, HIGH);         //end vibrate        
    digitalWrite(5, HIGH);         //end vibrate       
    digitalWrite(A1, HIGH);         //end smoke
    digitalWrite(A3, HIGH);         //end smoke
    digitalWrite(A4, HIGH);         //end smoke
    digitalWrite(6, LOW);        //free player 1
    digitalWrite(8, LOW);        //free player 3
    digitalWrite(9, LOW);        //free player 4
     delay(20000);                 //allow players to stand up
    digitalWrite(6, HIGH);        //restore 12v for next game
    digitalWrite(7, HIGH);        //restore 12v for next game
    digitalWrite(8, HIGH);        //restore 12v for next game
    digitalWrite(9, HIGH);        //restore 12v for next game
    
  }
  if (digitalRead(12) == HIGH); {  //player 3 wins
    digitalWrite(8, LOW);        //free winner
    digitalWrite(2, LOW);        //player 1 vibrate
    digitalWrite(3, LOW);        //player 2 vibrate
    digitalWrite(5, LOW);        //player 4 vibrate
    delay (3000);                 //delay to lessen smoke
    digitalWrite(A1, LOW);        //player 1 smoke
    digitalWrite(A2, LOW);        //player 2 smoke
    digitalWrite(A4, LOW);        //player 4 smoke
    delay (3000);                 //continue to smoke and vibrate
    digitalWrite(2, HIGH);         //end vibrate
    digitalWrite(3, HIGH);         //end vibrate        
    digitalWrite(5, HIGH);         //end vibrate       
    digitalWrite(A1, HIGH);         //end smoke
    digitalWrite(A2, HIGH);         //end smoke
    digitalWrite(A4, HIGH);         //end smoke
    digitalWrite(6, LOW);        //free player 1
    digitalWrite(7, LOW);        //free player 2
    digitalWrite(9, LOW);        //free player 4
     delay(20000);                 //allow players to stand up
    digitalWrite(6, HIGH);        //restore 12v for next game
    digitalWrite(7, HIGH);        //restore 12v for next game
    digitalWrite(8, HIGH);        //restore 12v for next game
    digitalWrite(9, HIGH);        //restore 12v for next game
    
  }
 if (digitalRead(13) == HIGH); {  //player 4 wins
    digitalWrite(9, LOW);        //free winner
    digitalWrite(2, LOW);        //player 1 vibrate
    digitalWrite(3, LOW);        //player 2 vibrate
    digitalWrite(4, LOW);        //player 3 vibrate
    delay (3000);                 //delay to lessen smoke
    digitalWrite(A2, LOW);        //player 2 smoke
    digitalWrite(A3, LOW);        //player 3 smoke
    digitalWrite(A4, LOW);        //player 4 smoke
    delay (3000);                 //continue to smoke and vibrate
    digitalWrite(2, HIGH);         //end vibrate
    digitalWrite(3, HIGH);         //end vibrate        
    digitalWrite(4, HIGH);         //end vibrate       
    digitalWrite(A1, HIGH);         //end smoke
    digitalWrite(A2, HIGH);         //end smoke
    digitalWrite(A3, HIGH);         //end smoke
    digitalWrite(6, LOW);        //free player 1
    digitalWrite(7, LOW);        //free player 2
    digitalWrite(8, LOW);        //free player 3
     delay(20000);                 //allow players to stand up
    digitalWrite(6, HIGH);        //restore 12v for next game
    digitalWrite(7, HIGH);        //restore 12v for next game
    digitalWrite(8, HIGH);        //restore 12v for next game
    digitalWrite(9, HIGH);        //restore 12v for next game
    
  }
 
  if (digitalRead(A0) == HIGH); {  //panic button pressed
    digitalWrite(6, LOW);        //free player 1
    digitalWrite(7, LOW);        //free player 2
    digitalWrite(8, LOW);        //free player 3
    digitalWrite(9, LOW);        //free player 4
     delay (20000);                 //allow players to stand up
    digitalWrite(6, HIGH);        //restore 12v for next game
    digitalWrite(7, HIGH);        //restore 12v for next game
    digitalWrite(8, HIGH);        //restore 12v for next game
    digitalWrite(9, HIGH);        //restore 12v for next game
  }
}`Preformatted text`
type or paste code here

When you define a button as INPUT_PULLUP, its default value is HIGH.

So you need to look for LOW to indicate a button press.

 if (digitalRead(10) == HIGH);

The semicolon on the end of this line is the only code whose execution is controlled by the result of the if statement. Subsequent code is executed unconditionally

Delete the semicolon and any others in similar positions

1 Like

DUH!!! THIS is why you dont write code at 4 am... Thanks. I knew it was something simple but man... I feel REALLY dumb right now.

...and also see UKHeliBob's post. You need to get rid of those semi colons in your if statements.

I did that as well, thanks. HOWEVER... now for some reason 6,7,8,9 are triggering the relays all the time and grounding 10,11,12, or 13 does nothing.

Post your revised code

Nevermind, I found it.

I neglected to fix A0 input (left it HIGH) so it was in panic mode continuously

Thanks Bob and Red for the really fast bailout on my incredibly dumb mistakes! I was one logic reversal from being really pleased with myself... LOL, Im glad it was that simple though. Means I have made progress.

Any thoughts on a more elegant way to code something of this nature?

Post your revised code as a starting point

You have a lot of repeated, or very similar code for each player. I would probably use arrays and a struct to represent the players.

Something like...

const uint8_t MAX = 4;

struct player
{
  uint8_t buttonPin;
  uint8_t vibratePin;
  uint8_t powerPin;
  uint8_t smokePin;
};

player players[MAX]  = { 10, 2, 6, A1, 
                         11, 3, 7, A2, 
                         12, 4, 8, A3, 
                         13, 5, 9, A4 }; 



void setup()
{
  for (uint8_t x = 0; x < MAX; x++)
  {
    pinMode(players[x].buttonPin,  INPUT_PULLUP);
    pinMode(players[x].vibratePin, OUTPUT);
    pinMode(players[x].powerPin,   OUTPUT);
    pinMode(players[x].smokePin,   OUTPUT);

    // Set initial values here...
  }
}

void loop()
{
  for (uint8_t x = 0; x < MAX; x++)
  {
    if (digitalRead(players[x].buttonPin) == LOW)
    {
      // Do stuff...
    }
  }
}

Thanks, I will have to try to figure out more about arrays. I assume that "uint8_t" is just a random choice and not something specific with a meaning... just something you are giving meaning to?

Just means "u"nsigned "i"nteger 8 bits "t"ype... (0-255) ... it's a standard C type. I like using them because it explicitly tells you how big the variable is... and it doesn't depend on the platform you are running on.

You could just use "int"... if you wanted to.

next question.... the triggering of relays seems to cause a brown out situation... I am using a barrel Jack 12v 5 amp power supply so I know it isnt starving externally. It must be an internal issue on the board itself. I am using Keystudio arduino and I find them to be second only to ruggeduino and have never experienced such a thing before. I solved the problem by inserting a short delay before the "release the players" triggering. I know that it browns out because (in spite of the 20 second delay) I can immediately trigger another win condition from any player. So the arduino is clearly losing power to the point that it dumps the program and resets itself. My question is, arent these 8 relay modules designed to run off the 5v pin? Or arent these 5v pins designed to run these modules? (either way)

It's not a good idea to power things directly from the Arduino... can you show how you have everything connected.

WOW... That's something I look forward to using. I have used int before but I like knowing there is more depth in another command.

here is the beast

I could add a buck converter but I am running out of room here. LOL the lower level is all 110v.

The 12v power supply is external and plugs in via barrel jack (not pictured)

If you're powering the relays via the Arduino 5v pin then all the current required is being pulled through the Arduino voltage regulator... and you are potentially overloading it.

If just a few of the relays are switched do you get the same power problems?