PLEASE HELP ME!

Hello,
I am Chetan new to Arduino studying in Gr=8th so I am working on a project in which I am making a code for a train project I tried mannier times making the code.
the code is like when a train arrives at ir sensor1 the servo barrier(single barrier) closes and when it reaches to ir sensor 2 it opens and when it reaches ir sensor 3 barrier closes another one at different position and opens after 15 seconds

Thanks!
Sorry for bad Grammer.
I hope I will be assisted by someone.

I tried mannier times making the code.

Please post your best effort. Does it compile and if so what does it do ?

Details of your sensors and how they are wired will also be needed.

Note that you will need to power the servos from an external source not directly from the Arduino as they take more current than the Arduino can safely provide.

You decide the pin and its not compiling. and there are 4 IR and 2 servos and don't worry for external power supply.

its not compiling

So post the code and all the error messages

THREE or FOUR sensors?
If this a school project, you might ask your teacher...?
Otherwise - as above.

My teacher doesn't even know the microcontroller and 3 sensors

What happens if the train gets to sensor2 but the back of the train's still in the crossing?

I'd rethink that... (and yeah I assume it's a model, since 8th graders don't usually design real train control systems :wink: and you own the train so know how long it is) but it's an inelegant way of doing it.

I would put sensor2 into a sort of "armed" state when sensor1 is broken, and only raise the barrier when sensor2 goes through a break-followed-by-a-clear cycle, ie the back of the train passing sensor2 is the important thing, not the front.

But that can be phase II...

Meantime as suggested, post the code that won't complie and let's help you get that sorted.

I will kept the distance already for it and the code is
its not compiling and I am also not good in it.

#include <Servo.h>
const int servo_pin = 2;
const int servo2_pin = 3;
const int ir1 = 4;
const int ir2 = 5;
const int ir3 = 6;

void setup()
{
Serial.begin(9600);
myservo.attach(servo_pin, 500, 2400);
myservo1.attach(servo2_pin, 500 ,2400);
myservo.write(0);
myservo1.write(0);
pinMode (ir1,INPUT);
pinMode (ir2,INPUT);
pinMode (ir3,INPUT);
delay(3000);
}

void loop()
{
if(ir1==HIGH);
myservo.write(90);
delay(100);

}
if (ir2==HIGH);
myservo.write(0);
delay(100);
{
if (ir3==HIGH)
myservo1.write(90);
delay(15000);
myservo1.write (0);
}

void loop() 
{
 if(ir1==HIGH);
 myservo.write(90);
 delay(100);

} //<<<<<<<
if (ir2==HIGH); //XXXXX
myservo.write(0);

The } marked with the <<<< closes loop() so all the lines from XXXX on are invalid

Apart from that, you don’t want these ;'s on the “if” lines since they end the "if"s…

if(ir1==HIGH);

So you need to put the stuff that belongs to the if’s, in {…} of their own like this, then only end loop() right at the end:

void loop() 
{
 if(ir1==HIGH) //lose the ;
{ // this one is new
 myservo.write(90);
 delay(100);

}// this one ends the if

...
... all the other stuff

} //last } will end loop()

edit…

Use code tags: [code] sketch goes here [/code]

Hit control T in the IDE to format the code’s indents neatly

And you need these above setup():

Servo myservo;
Servo myservo1;

Which btw you would have seen from the error message:

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\xxxx\Documents\Arduino\train_barrier_600580\train_barrier_600580.ino: In function 'void setup()':

train_barrier_600580:13: error: 'myservo' was not declared in this scope

   myservo.attach(servo_pin, 500, 2400);

   ^

Referring to this code in Reply #7 ...

void loop() 
{
   if(ir1==HIGH) {
       myservo.write(90);
       delay(100);

First of all, NOTE that there should be a { after an IF and not a ; And there needs to be a matching } after all the parts included in the IF clause.

Rather than move the servo directly in response to the sensor you should save the value of the sensor to a variable and make the servo operate based on the value of the variable. That way you can have one function to read the sensor and another to operate the servo. This may seem like a lot of extra work now, but if you intend to extend your system the use of functions will make things very much easier.

Have a look at Planning and Implementing a Program

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

  if(ir1==HIGH)

ir1 has the value 4.

Are you missing a digitalRead?

#include <Servo.h>
const int servo_pin = 2;
const int servo2_pin = 3;
const int ir1 = 4;
const int ir2 = 5;
const int ir3 = 6;

void setup() 
{
  Serial.begin(9600);
  Servo myservo;
  Servo myservo1;
  myservo.attach(servo_pin, 500, 2400);
  myservo1.attach(servo2_pin, 500 ,2400);
  myservo.write(0);
  myservo1.write(0);
  pinMode (ir1,INPUT);
  pinMode (ir2,INPUT);
  pinMode (ir3,INPUT);
  delay(3000);
}

void loop() 
{
 if(ir1==HIGH){
 myservo.write(90);
 delay(100);
}
if (ir2==HIGH){
myservo.write(0);
delay(100);
}

if (ir3==HIGH){
myservo1.write(90);
delay(15000);
myservo1.write (0);
}
 }

now?

The lines I said in reply #9 need to be above setup(), not in setup().

And these lines:

if (ir2==HIGH){

... need to be digitalRead()'s as pointed out in # 11.

now?

No

You have

const int ir1 = 4;

and

if (ir1 == HIGH)

but the value of ir1 will never change

You need to read the state of the pin

if (digitalRead(ir1) == HIGH)

I can foresee other problems but start with that

Thanks to everyone it compiling now.
and this is the code what else is need please tell me.
#include <Servo.h>
const int servo_pin = 2;
const int servo2_pin = 3;
Servo myservo;
Servo myservo1;
const int ir1 = 4;
const int ir2 = 5;
const int ir3 = 6;

void setup()
{
Serial.begin(9600);
myservo.attach(servo_pin, 500, 2400);
myservo1.attach(servo2_pin, 500 ,2400);
myservo.write(0);
myservo1.write(0);
pinMode (ir1,INPUT);
pinMode (ir2,INPUT);
pinMode (ir3,INPUT);
delay(3000);
}

void loop()
{
if (digitalRead(ir1)==HIGH){
myservo.write(90);
delay(100);
}
if (digitalRead(ir2)==HIGH){
myservo.write(0);
delay(100);
}

if (digitalRead(ir3)==HIGH){
myservo1.write(90);
delay(15000);
myservo1.write (0);
}
}

what else is need

Testing.

And code tags.

#include <Servo.h>
const int servo_pin = 2;
const int servo2_pin = 3;
Servo myservo;
Servo myservo1;
const int ir1 = 4;
const int ir2 = 5;
const int ir3 = 6;

void setup() 
{
  Serial.begin(9600);
  myservo.attach(servo_pin, 500, 2400);
  myservo1.attach(servo2_pin, 500 ,2400);
  myservo.write(0);
  myservo1.write(0);
  pinMode (ir1,INPUT);
  pinMode (ir2,INPUT);
  pinMode (ir3,INPUT);
  delay(3000);
}

void loop() 
{
 if (digitalRead(ir1)==HIGH){
 myservo.write(90);
 delay(100);
}
if (digitalRead(ir2)==HIGH){
myservo.write(0);
delay(100);
}

if (digitalRead(ir3)==HIGH){
myservo1.write(90);
delay(15000);
myservo1.write (0);
}
 }

any change in delays?
and sorry I forgot to put the tag