Help with coding rail crossing

I am working on building a wooden train rail in my son's room. I want it to have a railroad crossing at on point where it with have the crossing bars blinking lights and the ringing bells. I have four led's in my breadboard and I can get them to blink on button press but I need then to continue to blink through until the second button is pressed. So far they blink as long as I hold down the first button and then two remain on until second press. I am very new to electronics and coding so I know I'm just missing the bit of code or I have it all coded wrong to begin with. Keep in mind I would like this code to have the motor controls and to play the bell sounds in the future.

/*
 Turns on and off a Rail Crossing
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin0 = 2;     // the number of the pushbutton pin
const int buttonPin1 = 3;     
const int ledPin0 =  13;      // the number of the LED pin
const int ledPin1 =  12;      
const int ledPin2 =  11;      
const int ledPin3 =  10;      


// variables will change:
int buttonState0 = 0;         // variable for reading the pushbutton status
int buttonState1 = 0;        

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin0, OUTPUT); 
  pinMode(ledPin1, OUTPUT);  
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin0, INPUT);    
  pinMode(buttonPin1, INPUT);    
}

void loop(){
  // read the state of the pushbutton value:
  buttonState0 = digitalRead(buttonPin0);
  buttonState1 = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState0 == HIGH) {  
    // Blink Led's   
    digitalWrite(ledPin0, HIGH); 
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin1,LOW); 
    digitalWrite(ledPin3,LOW);   
    delay(300);                  //Wait 300 milliseconds
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin0,LOW); 
    digitalWrite(ledPin2,LOW); 
    delay(300);
  } 
 if (buttonState1 == HIGH) {  
    // Turn off Led's   
    digitalWrite(ledPin0, LOW); 
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW); 
  } 
}

Hi. This happens because you ask that action is taken only when your button is pushed. You need to ask that action is taken when at some point, the button has been pressed, unregarding its current state. Here's a piece of code you could use :

int buttonState = digitalRead(buttonPin);
int buttonPressed = false; //Define a boolean.
if (buttonState == HIGH)   buttonPressed  = true;  //This boolean becomes true when the button is pressed.

  if( buttonPressed == true && buttonState == LOW ) //Action is taken just after the button is released.
  {
    // do something like switching between blinking modes that you'll define later.
    buttonPressed = false;  //Reinitialise the boolean.
  }

Hope this helps.

I’m not sure where to insert those lines in the code. The int buttonstate and int buttonpressed go before void setup right? I am confused. I am just starting this and I am not sure I am mentally capable of understanding this stuff.

  if (buttonState0 == HIGH) {

Read my post about the switches. You need to detect a transition, not just a HIGH state. The way you have it is like having a light switch where you have to keep your finger on the button for the light to stay on.

I tried to read your pages it was to far over my head. I'm on medications that limit my mental abilities. I am new to Arduino and coding and little of it makes sense. I understand the if else statements and I am starting to understand the way to declare pins as either inputs or outputs. but that's the limit so far. I will learn but it will take longer than most.

This is what I mean:

/*
 Turns on and off a Rail Crossing
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin0 = 2;     // turn on crossing lights
const int buttonPin1 = 3;     // turn off crossing lights

const int ledPin0 =  13;      // the number of the LED pin
const int ledPin1 =  12;      
const int ledPin2 =  11;      
const int ledPin3 =  10;      


// variables will change:
int buttonState0 = 0;         // variable for reading the pushbutton status
int buttonState1 = 0;        

void setup() 
  {
  // initialize the LED pin as an output:
  pinMode(ledPin0, OUTPUT); 
  pinMode(ledPin1, OUTPUT);  
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin0, INPUT_PULLUP);    
  pinMode(buttonPin1, INPUT_PULLUP);    
  }

boolean isFlashing;
unsigned long lastFlash;

void loop(){
  // read the state of the pushbutton value:
  buttonState0 = digitalRead(buttonPin0);
  buttonState1 = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  if (buttonState0 == LOW)
    isFlashing = true;
    
  if (buttonState1 == LOW)
     isFlashing = false;

  if (isFlashing)
    {  
    if (millis () - lastFlash >= 300)
      {
      if (digitalRead (ledPin0) == LOW)
        {
        digitalWrite(ledPin0, HIGH); 
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin1,LOW); 
        digitalWrite(ledPin3,LOW);   
        }
      else
        {
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin3, HIGH);
        digitalWrite(ledPin0,LOW); 
        digitalWrite(ledPin2,LOW); 
        }
      lastFlash = millis ();  // remember when we flashed them
      }  // end of if time to flash
    }   // end of flashing wanted
  else
    {  
    // Turn off Led's   
    digitalWrite(ledPin0, LOW); 
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW); 
    }   // end of flashing not wanted

}  // end of loop

I tested for LOW rather than HIGH for a button press because I used the internal pull-ups to detect when the button was pressed (see: Gammon Forum : Electronics : Microprocessors : Switches tutorial for an explanation). So you wire the switch between the pin and ground, and let the internal pull-up give you a defined HIGH if the switch is not pressed.

In the code above the switches merely set isFlashing appropriately, and then every time through the main loop we flash the lights on and off if 300 mS has elapsed.

Ok got it working. Had to modify my circuit a little but its working great. Will need to study it a bit more to understand what the code is doing exactly. Thanks alot. Now to work on the bell ringing sounds. Is it best to work on separate codes for each part then combine them later once its all done?

I tend to test in small pieces, so if you aren't sure, probably. However combining sketches can be challenging because you need to merge two loops into one, and so on.