Blinking LED railroadcrossing

Good afternoon evereyone. My name is Ron and I am working on a lego train project.
What I am doing now is working on a Railroad crossing.
when the barrier goes down , i want a led flashing.
i would apreciate some help

// Servo aansturing tbv slagboom

#include <Servo.h>

Servo myservo;                              // create servo object to control a servo
const int ledPin =  12;                    // the number of the LED pin
const int buttonPin1 = 2;                    // the number of the pushbutton pin
const int buttonPin2 = 3;                    // the number of the pushbutton pin
//const int reedPin1 = 3;                    // the number of the pushbutton pin
//const int reedPin2 = 4;                    // the number of the pushbutton pin
int pos = 0;                                // variable to store the servo position
int buttonState1 = 0;                        // variable for reading the pushbutton status
int buttonState2 = 0;                        // variable for reading the pushbutton status
int reedState1 = 0;                        // variable for reading the pushbutton status
int reedState2 = 0;                        // variable for reading the pushbutton status


void setup() {
  myservo.attach(9);                         // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin1, INPUT);                // initialize the pushbutton pin as an input
  pinMode(buttonPin2, INPUT);                // initialize the pushbutton pin as an input
//  pinMode(reedPin1, INPUT);                 // initialize the pushbutton pin as an input
//  pinMode(reedPin2, INPUT);                 // initialize the pushbutton pin as an input
  pinMode(ledPin, OUTPUT);                  // initialize the LED pin as an output:
}

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



  if (buttonState1 == HIGH ) {                  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
     for (pos = 0; pos <= 85; pos += 1) {    //start positie slagboom hoog.goes from 0 degrees to 75 degrees in steps of 1 degree
digitalWrite(ledPin, HIGH);                  //led aan
            
        myservo.write(pos);                  //tell servo to go to position in variable 'pos'
        delay(75);                           //waits 15ms for the servo to reach the position
digitalWrite(ledPin, LOW);        
   
    
        
      }
  }                       
  buttonState2 = digitalRead(buttonPin2);       // read the state of the pushbutton value:
//  reedState2 = digitalRead(reedPin2);          // read the state of the pushbutton value:
  
  if (buttonState2 == HIGH ) {                 // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

      for (pos = 85; pos >= 0; pos -= 1) {     //goes from 90 degrees to 0 degrees
digitalWrite(ledPin, LOW);                    //led gaat uit        

          myservo.write(pos);                //tell servo to go to position in variable 'pos'
          delay(75);                         //waits 15ms for the servo to reach the position
        }
  }
}

when the barrier goes down , i want a led flashing.

Do you mean that the LED should flash whilst the barrier is moving down or when the barrier is down ?

If you want to do multiple things, don't wast 99% of it in delay()'s. See indeed blink without delay.

Also a tip, press Ctrl+T in the IDE and see how that looks :slight_smile:

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique. It has 3 blinking LEDs.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

First thing, do you have pulldown resistors on the pushbutton and switch input pins so they are not floating when the buttons / switches are open?