Can someone please help me. I want to add sensor2 to the code so that the grade crossing works from either sensor. I am using a UNO
#include <Servo.h>
Servo gate1servo; // create servo object to control crossing gate 1
//ASSUMPTION Gate is down at position 18 and up at position 60
Servo gate2servo; // create servo object to control crossing gate 2
//ASSUMPTION Gate is down at position 18 and up at position 60
int sensor1 = 2; //block detectors
int sensor2 = 3; //LDR detectors
int bell = 4; //MP3 player
int flasher = 5; //Flasher circuit
int gateposition = 60; // variable to store the servo gateposition
int gates_started = 0; // this says if the crossing is active
void setup()
{
gate1servo.attach(9); // attaches the servo on pin 9 to the servo object
gate2servo.attach(11); // attaches the servo on pin 11 to the servo object
gate1servo.write(gateposition); //start assuming no train
gate2servo.write(gateposition); //start assuming no train
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
pinMode(bell, OUTPUT);
pinMode(flasher, OUTPUT);
}
void loop()
{
if ((digitalRead (sensor1) == LOW) && (gates_started == 0))
{
gates_started = 1;
starting_sequence();
}
if ((digitalRead(sensor1) == HIGH) && (gates_started == 1))
{
ending_sequence();
}
}
void starting_sequence() {
for (gateposition = 18; gateposition < 60; gateposition++) // goes from 18 degrees to 50 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'
delay(50); // waits 40ms to slow servo
digitalWrite(bell, LOW);
digitalWrite(flasher, LOW);
}
}
void ending_sequence() {
for (gateposition = 60; gateposition > 18; gateposition -= 1) // goes from 50 degrees to 18 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'
delay(50); // waits 40ms to slow servo
digitalWrite(bell, HIGH);
digitalWrite(flasher, HIGH);
gates_started = 0; // gates are open
}
}