Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. //Constants
  4. const int Sens1 = 2; //Arrival detecting IR sensor on pin 2
  5. const int Sens2 = 4; // Gate exit IR sensor on pin 4
  6. const int OBsensor = 7; // Obstruction sensor on pin 7
  7.  
  8. const int TrainLEDG = 12; // Traffic and train directing LEDS
  9. const int TrainLEDR = 13;
  10. const int TrafficLEDG = 5;
  11. const int TrafficLEDR = 6;
  12.  
  13. Servo gateXing; // Declare servo as gate mechanism
  14.  
  15. // Variables
  16. int OBsensorState = 0;
  17.  
  18. int Sens1State = 0; // current state of the IR sensor
  19. int lastSens1State = 0; // previous state of the IR sensor
  20. // NOTE: These variables are necessary, in addition to the constant IR sensor declaration.
  21. int Sens2State = 0;
  22. int lastSens2State = 0;
  23.  
  24. void setup() {
  25.  
  26. pinMode(Sens1, INPUT); // IR sensors used as input
  27. pinMode(Sens2, INPUT);
  28. pinMode (OBsensor, INPUT); // Obstruction Sensor
  29. pinMode(9, OUTPUT); // Buzzer
  30.  
  31. pinMode(TrafficLEDG, OUTPUT);
  32. pinMode(TrafficLEDR, OUTPUT);
  33. pinMode(TrainLEDG, OUTPUT);
  34. pinMode(TrainLEDR, OUTPUT); //LEDs for traffic control
  35.  
  36. gateXing.attach(10); // servo on digital pin 10
  37. }
  38.  
  39.  
  40. void loop() {
  41.  
  42. // Obstruction sensor code (first because highest priority)
  43. OBsensorState = digitalRead (OBsensor);
  44.  
  45. if (OBsensorState == HIGH){
  46. digitalWrite(TrainLEDR, HIGH);
  47. digitalWrite(TrainLEDG, LOW);
  48. digitalWrite (TrafficLEDR, HIGH);
  49. digitalWrite (TrafficLEDG, LOW);
  50. gateXing.write(0);
  51. }
  52. else{
  53.  
  54. digitalWrite (TrainLEDR, LOW);
  55.  
  56. // read the IR input pin:
  57. Sens1State = digitalRead(Sens1);
  58.  
  59. Sens2State = digitalRead (Sens2);
  60.  
  61. //Arrival Proximity Sensor code
  62.  
  63. digitalWrite(TrainLEDG, HIGH);
  64.  
  65. if (Sens1State != lastSens1State) { // compare the sensor state to its previous state, used for edge detection
  66.  
  67.  
  68. if (Sens1State == HIGH) {
  69. gateXing.write(0); // gate closes (0 degree angle- 3 o'clock position
  70. digitalWrite(9, HIGH);
  71. digitalWrite(TrafficLEDR, HIGH);
  72. digitalWrite(TrafficLEDG, LOW);
  73. }
  74.  
  75. }
  76.  
  77. lastSens1State = Sens1State; // current state (ON or OFF) is assigned as the sensor's comparator state for next loop cycle.
  78.  
  79. //Exit Crossing Sensor code
  80.  
  81. if (Sens2State != lastSens2State) {
  82.  
  83. if (Sens2State % 1 == 0)
  84. {
  85. gateXing.write(90); // gate is up (90 degree angle)
  86. digitalWrite (9, LOW);
  87. digitalWrite(TrafficLEDR, LOW);
  88. digitalWrite(TrafficLEDG, HIGH);
  89. delay(700);
  90.  
  91. }
  92. }
  93. lastSens2State = Sens2State;
  94.  
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement