Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- //Constants
- const int Sens1 = 2; //Arrival detecting IR sensor on pin 2
- const int Sens2 = 4; // Gate exit IR sensor on pin 4
- const int OBsensor = 7; // Obstruction sensor on pin 7
- const int TrainLEDG = 12; // Traffic and train directing LEDS
- const int TrainLEDR = 13;
- const int TrafficLEDG = 5;
- const int TrafficLEDR = 6;
- Servo gateXing; // Declare servo as gate mechanism
- // Variables
- int OBsensorState = 0;
- int Sens1State = 0; // current state of the IR sensor
- int lastSens1State = 0; // previous state of the IR sensor
- // NOTE: These variables are necessary, in addition to the constant IR sensor declaration.
- int Sens2State = 0;
- int lastSens2State = 0;
- void setup() {
- pinMode(Sens1, INPUT); // IR sensors used as input
- pinMode(Sens2, INPUT);
- pinMode (OBsensor, INPUT); // Obstruction Sensor
- pinMode(9, OUTPUT); // Buzzer
- pinMode(TrafficLEDG, OUTPUT);
- pinMode(TrafficLEDR, OUTPUT);
- pinMode(TrainLEDG, OUTPUT);
- pinMode(TrainLEDR, OUTPUT); //LEDs for traffic control
- gateXing.attach(10); // servo on digital pin 10
- }
- void loop() {
- // Obstruction sensor code (first because highest priority)
- OBsensorState = digitalRead (OBsensor);
- if (OBsensorState == HIGH){
- digitalWrite(TrainLEDR, HIGH);
- digitalWrite(TrainLEDG, LOW);
- digitalWrite (TrafficLEDR, HIGH);
- digitalWrite (TrafficLEDG, LOW);
- gateXing.write(0);
- }
- else{
- digitalWrite (TrainLEDR, LOW);
- // read the IR input pin:
- Sens1State = digitalRead(Sens1);
- Sens2State = digitalRead (Sens2);
- //Arrival Proximity Sensor code
- digitalWrite(TrainLEDG, HIGH);
- if (Sens1State != lastSens1State) { // compare the sensor state to its previous state, used for edge detection
- if (Sens1State == HIGH) {
- gateXing.write(0); // gate closes (0 degree angle- 3 o'clock position
- digitalWrite(9, HIGH);
- digitalWrite(TrafficLEDR, HIGH);
- digitalWrite(TrafficLEDG, LOW);
- }
- }
- lastSens1State = Sens1State; // current state (ON or OFF) is assigned as the sensor's comparator state for next loop cycle.
- //Exit Crossing Sensor code
- if (Sens2State != lastSens2State) {
- if (Sens2State % 1 == 0)
- {
- gateXing.write(90); // gate is up (90 degree angle)
- digitalWrite (9, LOW);
- digitalWrite(TrafficLEDR, LOW);
- digitalWrite(TrafficLEDG, HIGH);
- delay(700);
- }
- }
- lastSens2State = Sens2State;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement