title: How to constantly listen for button input in Arduino?
body: <p>I am trying to create a working physical simulation of a traffic light intersection. I want to listen for button readings (pedestrian buttons to turn pedestrian lights green) continuously throughout the program and want to run a different function that will handle pedestrian lights if the buttons are ever pressed during the program.
  I have tried it with while loops and if conditions but it just won't work because then it would read the button input at a specific point in time when I want it to read the readings constantly throughout the program and break the loop if the condition is ever untrue (while and do-while loops only check the condition at the end of the loop when I want it to check the condition throughout the loop). I also need to get which button was pressed if that possible. Then depending on which button was pressed, I want to run a function called pedes() or pedes2().
  Feel free to ask if you need any clarifications.</p>
  
  <p>I have posted my original code below that I want to run constantly until a button will be pressed.
  Thanks!</p>
  
  <pre><code>// If at any point in time during this code, a button is pushed, I want to run a function called
      pedes() or pedes2() depending on which button is pressed.
  
  // First set of Trafiic Lights
  int redT = 13 ;
  int yellowT = 12;
  int greenT = 11;
  
  // First set of Pedestrian Lights
  int redP = 10;
  int greenP = 9;
  
  // Second set of Traffic Lights
  int redT2 = 8;
  int yellowT2 = 7;
  int greenT2 = 6;
  
  // Second set of Pedestrian Lights
  int redP2 = 5;
  int greenP2 = 4;
  
  // Pedestrian Buttons
  int buttonT = 3;
  int buttonT2 = 2;
  int buttonT3 = 1;
  int buttonT4 = 0;
  
  int buttonStateT = 0;
  int buttonStateT2 = 0;
  int buttonStateT3 = 0;
  int buttonStateT4 = 0;
  
  // Booleans which will handle which button was pressed
  
  void setup() {
    // First set of Trafiic Lights
    pinMode(redT, OUTPUT);
    pinMode(yellowT, OUTPUT);
    pinMode(greenT, OUTPUT);
  
    // First set of Pedestrian Lights
    pinMode(redP, OUTPUT);
    pinMode(greenP, OUTPUT);
  
    // Second set of Traffic Lights
    pinMode(redT2, OUTPUT);
    pinMode(yellowT2, OUTPUT);
    pinMode(greenT2, OUTPUT);
  
    // Second set of Pedestrian Lights
    pinMode(redP2, OUTPUT);
    pinMode(greenP2, OUTPUT);
  
    // Pedestrian Buttons
    pinMode(buttonT, INPUT);
    pinMode(buttonT2, INPUT);
    pinMode(buttonT3, INPUT);
    pinMode(buttonP4, INPUT);
  }
  
  void loop() {
      // Resetting all the traffic lights
      digitalWrite(redP, HIGH); // Turns on red pedestrian LED from 1st bunch
      digitalWrite(redP2, HIGH); // Turns on red pedestrian LED from 2nd bunch
  
      digitalWrite(yellowT, LOW); // Turns off yellow traffic LED from 1st bunch
      digitalWrite(yellowT2, LOW); // Turns off yellow traffic LED from 1st bunch
  
      digitalWrite(redT, HIGH); // Turns on red traffic LED from 1st bunch
      digitalWrite(redT2, HIGH); // Turns on red traffic LED from 2nd bunch
  
      delay(2000);
  
      digitalWrite(redT, LOW); // Turns off red traffic LED from 1st bunch
      digitalWrite(redT2, HIGH); // Turns on red traffic LED from 2nd bunch
  
      digitalWrite(greenT, HIGH); // Turns on green traffic LED from 1st bunch
      digitalWrite(greenT2, LOW); // Turns off green traffic LED from 2nd bunch
  
      delay(10000); // Pauses program for 8 seconds
  
      digitalWrite(greenT, LOW); // Turns off green traffic LED from 1st bunch
      digitalWrite(yellowT, HIGH); // Turns on yellow traffic LED from 1st bunch
  
      delay(3000); // Pauses program for 3 seconds
  
      digitalWrite(yellowT, LOW); // Turns off yellow traffic LED from 1st bunch
      digitalWrite(redT, HIGH); // Turns on red traffic LED from 1st bunch
  
      delay(2000); // Pauses program for 3 seconds
  
      digitalWrite(redT2, LOW); // Turns off red traffic LED from 2nd bunch
      digitalWrite(greenT2, HIGH); // Turns on green traffic LED from 2nd bunch
  
      delay(6000); // Pauses program for 8 seconds
  
      digitalWrite(greenT2, LOW); // Turns off green traffic LED from 2nd bunch
      digitalWrite(yellowT2, HIGH); // Turns on yellow traffic LED from 2nd bunch
  
      delay(3000); // Pauses program for 3 seconds
  
      digitalWrite(yellowT2, LOW); // Turns off yellow traffic LED from 2nd bunch
      digitalWrite(redT2, HIGH); // Turns on red traffic LED from 2nd bunch
  
      delay(2000); // Pauses program for 3 seconds
    }
  </code></pre>