how to delay the next servo command

hello i combined 2 codes that i found on the forum

it works great on the simulator but not prefect jet

its for a railroad crossing so when the train touch the button the code activates

the redlights will go flashing and the servo turns the barrier down from 90 to 180 degrees

works great

the next thing is the servo will reverse directly from 180 to 90 without any delay

how can i add a delay that the servo will pauze for 15sec and the lights will still flashes
and after 15sec the servo turns back from 180 to 90 and if done the lights go to "switch off state "

i hope you understand what i mean and that someone can help me

/*
// constants won't change. Used here to set a pin number :
const int rood1 =  12;     // the number of the LED pin
const int rood2 =  10;     // the number of the LED pin
const int wit1 =  11;     // the number of the LED pin
const int switchPin = 1;    // the number of the switch pin
int switchState = 0;       // variable for reading the switch status 

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 90;    // variable to store the servo position

void setup() {
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
 pinMode(rood1, OUTPUT);  // initialize the LED pin as an output:
 pinMode(rood2, OUTPUT);
 pinMode(wit1, OUTPUT);
 pinMode(switchPin, INPUT);  // initialize the pushbutton pin as an input:
}

void loop() 
{
 switchState = digitalRead(switchPin);   // read the state of the //pushbuttonvalue:
 if (switchState == HIGH)   //if the switch on:
 {
 digitalWrite(wit1, LOW);     //white LED off
 digitalWrite(rood1, HIGH);   //red1 LED on
 digitalWrite(rood2, LOW);    //red2 LED off
 delay(200);                     //wait 400ms
 digitalWrite(rood1, LOW);    //red1 LED off
 digitalWrite(rood2, HIGH);   //red2 LED on
 delay(200);
 for(pos = 90; pos < 180; pos += 10)  // goes from 90 degrees to 180 degrees
 { // in steps of 10 degree
 myservo.write(pos);              // tell servo to go to position in variable 'pos'
 digitalWrite(wit1, LOW);     //white LED off
 digitalWrite(rood1, HIGH);   //red1 LED on
 digitalWrite(rood2, LOW);    //red2 LED off
 delay(200);                     //wait 200ms
 digitalWrite(rood1, LOW);    //red1 LED off
 digitalWrite(rood2, HIGH);   //red2 LED on
 delay(200);                       // waits 200ms for the servo to reach the position
 }
 { delay(200);                       // waits 200ms for the servo to reach the position
 } 
 { digitalWrite(wit1, LOW);     //white LED off
 digitalWrite(rood1, HIGH);   //red1 LED on
 digitalWrite(rood2, LOW);    //red2 LED off
 delay(200);                     //wait 200ms
 digitalWrite(rood1, LOW);    //red1 LED off
 digitalWrite(rood2, HIGH);   //red2 LED on
 delay(200);
 }
 
 for (pos = 180; pos > 90; pos -= 10)  // goes from 180 degrees to 90 degrees
 { // in steps of 10 degree
 myservo.write(pos);              // tell servo to go to position in variable 'pos'
 digitalWrite(wit1, LOW);     //white LED off
 digitalWrite(rood1, HIGH);   //red1 LED on
 digitalWrite(rood2, LOW);    //red2 LED off
 delay(200);                     //wait 200ms
 digitalWrite(rood1, LOW);    //red1 LED off
 digitalWrite(rood2, HIGH);   //red2 LED on
 delay(200);                       // waits 200ms for the servo to reach the position
 }
 { delay(200);                       // waits 200ms for the servo to reach the position
 } 
 
 }
 if (switchState == LOW)              //if the switch off
 {
 digitalWrite(rood1, LOW);    //red1 LED off
 digitalWrite(rood2, LOW);    //red2 LED off
 digitalWrite(wit1, HIGH);    //white LED on
 delay(600);                  //wait 600ms
 digitalWrite(wit1, LOW);     //white LED off
 delay(600); //wait 600ms
 }
 { 
 }}

youtube video of the simulation

evinc: it works great on the simulator but not prefect jet

here where i live, as a train approaches a signal, this is what happens:

the crossing lights begin to flash the barrier is lowered (lights still flashing) the train clears the intersection . (lights still flashing) the barriers are raised . (lights still flashing) the lights stop their flashing

so, you want the lights to flash while the barrier is raised or lowered, yes?

you have to learn to program those 5 states without using delay...

in holland we have crossing where the white/yellow light stays flashing for attention/safety by night

so red flashing on barrier down

white/yellow flashing slower when barriers are up

evinc: in holland we have crossing where the white/yellow light stays flashing for attention/safety by night

so red flashing on barrier down

white/yellow flashing slower when barriers are up

so, still 5 states...

yeah 5 states

but im totally new with this so i did not learn anything buy some things today sensors wires resistors ect ect

(total noob) :)

just search things and combined them to get what i think i needed :)

without learning :P think thats my handicap :) just try and learn :P

evinc: yeah 5 states

but im totally new with this so i did not learn anything buy some things today sensors wires resistors ect ect

(total noob) :)

just search things and combined them to get what i think i needed :)

without learning :P think thats my handicap :) just try and learn :P

yes, try...

My suggestion is that you learn how to flash the LEDs without using delay.

Moving the servo is harder, but you can get help there.

Look at the "blink without delay" example in the IDE's File->Example->Digital section

It will help you understand how to use millis() function to make things happen.

BulldogLowell: yes, try...

My suggestion is that you learn how to flash the LEDs without using delay.

Moving the servo is harder, but you can get help there.

Look at the "blink without delay" example in the IDE's File->Example->Digital section

It will help you understand how to use millis() function to make things happen.

oke after some reading and trying i got this for the lights now

if the train hits the button it flashes and if it lost it the lights go's out

const int red1 = 13;
const int red2 = 12;
const int train = 7;
int red1State = LOW;
int red2State = LOW;
int trainState = LOW;              

unsigned long previousMillis = 0;
const long interval = 1000;

void setup() {
    pinMode(red1, OUTPUT);
    pinMode(red2, OUTPUT);
    pinMode(train, INPUT);
}

void loop() 
{   trainState = digitalRead(train);
    unsigned long currentMillis = millis();

    if (trainState == HIGH)
    {

        if (currentMillis - previousMillis >= interval) 
        {
            previousMillis = currentMillis;

            if (red1State == LOW) 
            {
                red1State = HIGH;
                red2State = LOW;
            } 
            else 
            {
                red1State = LOW;
                red2State = HIGH;}
        }
        digitalWrite(red1, red1State);
        digitalWrite(red2, red2State);
    }
    else {  red1State = LOW;
        red2State = LOW;
        digitalWrite(red1, red1State);
        digitalWrite(red2, red2State);
        digitalWrite(train, trainState);}
}