Uk level crossing for model railway help with script

hi i am trying to do a level crossing using 12 micro leds 2 servos 2 push button and dfplayer, when pressing button 1 dfplayer starts mp3 4 yellow come on and barriers go down and then 8 red lights alternate 4 on 4 off, when button 2 is pressed barriers go up lights and sound stops , many thanks heres what i have i am very new to this

#include <DFRobotDFPlayerMini.h>
#include "SoftwareSerial.h"
#include "Arduino.h"
#include <Servo.h>
Servo gate1servo; // create servo object to control crossing gate 1
//ASSUMPTION Gate is down at position 30 and up at position 120
Servo gate2servo; // create servo object to control crossing gate 2
//ASSUMPTION Gate is down at position 30 and up at position 120
SoftwareSerial mySoftwareSerial(2,3); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
int buttonPin1 = 9;
int buttonPin2 = 11;
int red1 = 4; // Led 1 pin first alternating flasher
int red1 = 8; // Led 2 pin first alternating flasher
int red1 = 12; // Led 3 pin first alternating flasher
int red1 = a4; // Led 4 pin first alternating flasher
int red2 = 7; // Led 5 pin second alternating flasher
int red2 = 10; // Led 6 pin second alternating flasher
int red2 = 13; // Led 7 pin second alternating flasher
int red2 = a5; // Led 8 pin second alternating flasher
int yellow = a0; // Led 9 pin yellow
int yellow = a1; // Led 10 pin yellow
int yellow = a2; // Led 11 pin yellow
int yellow = a3; // Led 12 pin yellow
int buttonState = 0;
int gatelow = 30; // variable to store the servo low gate stop position
int gatehigh = 120; // variable to store the servo high gate stop position
int gateposition = 120; // variable to store the servo gateposition
int gates_started = 0; // this says if the crossing is active
int flash_state = 0;
long flash_time = 0;
long flash_interval = 900; // time in milliseconds between alternating flashes

void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(115200);

Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));

myDFPlayer.volume(10); //Set volume value. From 0 to 30
myDFPlayer.play(1); //Play the first mp3
gate1servo.attach(5); // attaches the servo on pin 3 to the servo object
gate2servo.attach(6); // attaches the servo on pin 4 to the servo object
gate1servo.write(gateposition); //start assuming no train
gate2servo.write(gateposition); //start assuming no train
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(red1, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(ywllow, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(red1, LOW); // Start with all flashers off
digitalWrite(red2, LOW);
digitalWrite(yellow, LOW);
digitalWrite(gateflashers1, LOW);
digitalWrite(gateflashers2, LOW);

flash_time = millis();
}
void loop()
{ // read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);

if (buttonState == HIGH) (gates_started==0))
myDFPlayer.play(1); //Play the first mp3
digitalWrite(YELLOW, HIGH); //put amber warning LED on for 3 sec
delay (3000);
digitalWrite(YELLOW, LOW);
}
gates_started = 1;
starting_sequence();
if (gates_started) flash_leds(); //gates are down continue flashing

digitalWrite(RED1, HIGH);
delay(1000);
digitalWrite(RED1, LOW);
digitalWrite(RED2, HIGH);
delay(1000);
digitalWrite(RED2, LOW);

// we only get here if the train has really left the crossing
ending_sequence();
}

buttonState = digitalRead(buttonPin2);
(buttonState == HIGH)
void starting_sequence() {
long wait_time;
flash_time = millis();
wait_time = millis()+3000;
while (wait_time > millis()) flash_leds(); //flash before dropping gates
for(gateposition = gatehigh; gateposition>gatelow; gateposition-=1) // goes from gatehigh degrees to gatelow degrees
{
gate1servo.write(gateposition); // tell servo to go to gateposition in variable ‚Äėgateposition‚Äô
gate2servo.write(gateposition); // tell servo to go to gateposition in variable ‚Äėgateposition‚Äô
flash_leds(); // keep flashing leds
delay(40); // waits 40ms to slow servo
}
}
void ending_sequence() {

for(gateposition = gatelow; gateposition<gatehigh; gateposition++) // goes from gatelow degrees to gatehigh degrees
{
gate1servo.write(gateposition); // tell servo to go to gateposition in variable ‚Äėgateposition‚Äô
gate2servo.write(gateposition); // tell servo to go to gateposition in variable ‚Äėgateposition‚Äô
flash_leds(); // keep flashing leds
delay(40); // waits 40ms to slow servo
}
digitalWrite(led1, LOW); // flashers completely off
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
digitalWrite(led4, LOW);
digitalWrite(gateflashers1, LOW);
digitalWrite(gateflashers2, LOW);
digitalWrite(alarmbells, LOW);
delay(30000); // 30 second delay to account for the train passing the starting entry sensor
}
void flash_leds() {
if (flash_time > millis()) return;
flash_state = ~flash_state;
digitalWrite(red1, flash_state); // Alternate flashers
digitalWrite(red2, flash_state);
digitalWrite(yellow, ~flash_state);
flash_time = millis()+flash_interval;
}

Please read the sticky at the top of the forum "Read this before posting a programming question".

I am not sure what the question is.

Hello
Post a picture of crossing showing the arrangement of LEDs.

This must have been moved (sensibly) to "Project Guidance".

OP: Please read the instructions for posting at the top of each forum and edit your post to correctly display the code.


Well, that was ignored, so I'm out of this discussion! :roll_eyes:

Hi, @muskett1986
Welcome to the forum.

Does your code compile?

One of the problems you have is;

int red1 = 4; // Led 1 pin first alternating flasher
int red1 = 8; // Led 2 pin first alternating flasher
int red1 = 12; // Led 3 pin first alternating flasher
int red1 = a4; // Led 4 pin first alternating flasher

In this bit of code, red1 == a4, not 4 or 8 or 12.

int red1LED1Pin = 4; // Led 1 pin first alternating flasher
int red1LED2Pin = 8; // Led 2 pin first alternating flasher
int red1LED3Pin = 12; // Led 3 pin first alternating flasher
int red1LED4Pin = a4; // Led 4 pin first alternating flasher

Will assign the separate values you need for each pin.
You will have to do the same with variable red2 and yellow.

The rest of your code does not have the structure of Arduino C++

https://www.arduino.cc/reference/en/language/structure/control-structure/if/

Can you please post a circuit diagram.

Tom... :smiley: :+1: :coffee: :australia:

Did you mean "A4"?

Yes..

hi @TomGeorge i was wanting to use the analog pins as digital too for control of leds
on a uno iv laid it out as follows
rx pin 2
tx pin3
first red led pin 4
gate servo pin 5
gate servo pin 6
sec red led pin 7
first red led pin 8
push button pin 9
sec red led pin 10
push button pin 11
first red led pin 12
sec red led pin 13
yellow led pin a0
yellow led pin a1
yellow led pin a2
yellow led pin a3
first red led pin a4
sec red led pin a5
pressing 1 button starts dfplayer amber flashers and starts to lower gates, when gates lowered change to red wig wags led red 1 and red 2 until button 2 is pressed, when button 2 is pressed the dfplayer turns off gates go up and red wig wags turn off

@TomGeorge Whyteleafe South Level Crossing, Surrey - YouTube

You may find charlieplexing saves you a few pins for the LEDs.

You have made the classic mistake like a majority of beginners - you tried to write the entire solution in one go. What you have clearly won't compile.

I suggest that you save what you have and write a new program that does a tiny part of the solution. When it compiles and works, add a bit more and repeat until you have what you need.

Hi,
This;
yellow led pin a0
yellow led pin a1
yellow led pin a2
yellow led pin a3
Each pin has to have a unique name, you cannot name pin A0 and pin A1 with the same variable name, it will not work.

Each pin name has to be different

int yellowLED1 = A0;
int yellowLED2 = A1;
int yellowLED3 = A2;
int yellowLED4 = A3;

If ALL the RED LEDs flash together, then why not use ONE output pin and a MOSFET or BJT to switch ALL 4?
Same for the other LED groups.

Tom... :smiley: :+1: :coffee: :australia:

Some of them could I expect, but not all - the level crossings in the UK have two red lights on each side that alternatively flash.

@wildbill iv got the 3 scripts that would make it up however combining them is where I'm getting the problems ,

Hi,
Please post your scripts, in code tags as in the link posted on post#2.
Do your scripts compile?
Have you loaded then to a controller yet?
What model Arduino are you using?
Do you have a circuit diagram to post for us?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Same principle applies. If they work standalone, take the longest one and start adding bits piece by piece. Trying to get them all working at once is like, well, work :wink:

@wildbill the largest 1 to compile uses sensors and buzzer and no yellow. I have tried to swap sensors with pushbutton and add the yellows and dfplayer but I am very much new to all this and all this coding jargon and I'm finding it hard to learn my brain just won't register it :confused::confused::confused::confused:

Post what you have that compiles. Does it actually work too?

great idea, I like it.

You need current limiting resistors in series with the LEDs.

You need to work through some of the example sketches that ship with the IDE, to begin to understand what is going on.