I have an arduino project for RR crossing gates and lights. reed switch turns it on reed switch 2 turns it off The problem is The bell rings and the lights flash but for some reason I cannot get the gates to raise and lower. Could someone please help me with this?
#include <Servo.h>
// control_pin1 signal: ___┌───┐_____________________________________┌───┐___________
// control_pin2 signal: ______________________________┌───┐____________________┌───┐_
// ^START ^STOP ^START ^STOP
#define delta 540 // 0.540 seconds on and off
#define bstrobe 60
#define led_pin1 11 // the Crossing LED is attached to pin 11
#define led_pin2 12 // the Crossing LED is attached to pin 12
int control_pin1 = 14; // Control Pin for first reed sensor is Pin 14 (A0) Low/Ground is OFF
int control_pin2 = 17; // control pin for second reed sensor
// OPEN (Unattachd) Pin is ON
#define bell_pin 15 // D15 == A1 This is the signal for the single Bell
Servo servo1; // servo object
int servo1_pin = 10; // servo pin
Servo servo2; // servo object
int servo2_pin = 9; // servo pin
int servo_delay = 40; // this number decides how fast the servos will move. higher number = slower servos
int position_on = 5; // position of the servo motor when it's running
int position_off = 45; // position of the servo motor when it's off
bool gates_down = false;
bool should_run = false; // this will be a true or false value indicating whether the process should be running or not
int control1 = LOW; // current value of the first reed switch
int prev_control1 = LOW; // this will keep track of the previous value of the control pin (neccessary for finding the edge condition)
int control2 = LOW; // current value of the second reed switch
int prev_control2 = LOW; // previous value of the second reed switch
void setup() {
// initialize led_pin as digital output pin
pinMode(led_pin1, OUTPUT);
pinMode(led_pin2, OUTPUT);
pinMode(bell_pin, OUTPUT);
digitalWrite(bell_pin, HIGH);
pinMode(control_pin1,INPUT_PULLUP);
pinMode(control_pin2,INPUT_PULLUP);
servo1.attach(servo1_pin);
servo2.attach(servo2_pin);
}
// the loop function runs over and over again forever
void loop() {
prev_control1 = control1;
control1 = digitalRead(control_pin1);
prev_control2 = control2;
control2 = digitalRead(control_pin2);
if (control1 == HIGH && prev_control1 == LOW)
{
// the loop should only run in the case where there was a change and it was a rising edge (see diagram at top)
should_run = true;
}
if (control2 == HIGH && prev_control2 == LOW)
{
// the loop should stop when there was a rising edge on the second sensor
should_run = false;
}
if (should_run == true) {
lowerGates(); // calls the function to turn the servos to the "on" position
digitalWrite(led_pin1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led_pin2, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(bell_pin, LOW);
delay(bstrobe);
digitalWrite(bell_pin, HIGH);
delay(delta); // wait for a second
digitalWrite(led_pin1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led_pin2, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(bell_pin, LOW);
delay(bstrobe);
digitalWrite(bell_pin, HIGH);
delay(delta); // wait for a second
} else {
raiseGates(); // calls function to turn the servos to the "off" position
digitalWrite(led_pin1, LOW); // turn the LED off
digitalWrite(led_pin2, LOW); // turn the LED off
}
}
void lowerGates()
{
// moves the servos from position_off to position_on
if (gates_down == false)
{
for (int i=position_off; i <= position_on; i++)
{
servo1.write(i);
servo2.write(i);
delay(servo_delay); // This should slow down the servos
}
gates_down = true;
}
}
void raiseGates()
{
// moves the servos from position_on to position_off
if (gates_down == true)
{
for (int i=position_on; i > position_off; i--)
{
servo1.write(i);
servo2.write(i);
delay(servo_delay); // This should slow down the servos
}
gates_down = false;
}
}