Hi, I have been trying to programme some code for this scenario but for some reason or another it still is not working, can anyone spot what is wrong with this code?
Thanks in advance.
Scenario: A control system which detects train coming, close a barrier (emulated by a motor) to stop vehicles from crossing and change the warning light of the two-way perpendicular traffic. When the train passes the gate would open and the lights would change back to indicate safe state meaning that the vehicles can pass.
Code:
// this constant won't change:
const int S1 = 1; // the pin that the switch indicating the train coming is attached to
const int S2 = 2; // the pin that the 2nd switch indicating that the train passed is attached to
const int L1 = 3; // the pin that the driver indication bulb is attached to
const int L2 = 4; // the pin that the driver 2nd indication bulb is attached to
const int M1_OPENING = 5; // the pin that the motor 1 is attached to
const int M1_CLOSING = 6; // the pin that the motor 1 is attached to
const int M2_OPENING = 7; // the pin that the motor 1 is attached to
const int M2_CLOSING = 8; // the pin that the motor 1 is attached to
const int EnA = 9;
const int EnB = 10;
// Variables will change:
int SW_TRN_COMING = 0; // current state of the button train coming
int SW_TRN_PASS = 0; // current state of the button train pass
void setup() {
// initialize the button pins as a input:
pinMode(S1, INPUT);
pinMode(S2, INPUT);
// initialize the LED & Motor pins as an output:
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(M1_OPENING, OUTPUT);
pinMode(M1_CLOSING, OUTPUT);
pinMode(M2_OPENING, OUTPUT);
pinMode(M2_CLOSING, OUTPUT);
pinMode(EnA, OUTPUT);
pinMode(EnB, OUTPUT);
analogWrite(EnA, 255); //variable to control motor speed on h bridge L239D
analogWrite(EnB, 255); //variable to control motor speed on h bridge L239D
}
void loop() {
// read the pushbutton input pin:
SW_TRN_COMING = digitalRead(S1);
SW_TRN_PASS = digitalRead(S2);
//Decistion making
//SW state
// S1 = LOW & S2 = LOW => open gate
// S1 = HIGH & S2 = LOW => close gate
// S1 = LOW & S2 = HIGH => open gate
// S1 = HIGH & S2 = HIGH => close gate
if((SW_TRN_COMING == LOW && SW_TRN_PASS == LOW) || (SW_TRN_COMING == LOW && SW_TRN_PASS == LOW))
{
op();
}
else
{
cl();
}
}
// the part of the programme that turns on the warning lights and closes the gate after 7 seconds
void cl()
{
digitalWrite(L1, HIGH);
digitalWrite(L2, HIGH);
delay(7000); // L1 and L2 turn on 7 seconds before the gate closes to notify drivers
digitalWrite(M1_CLOSING, HIGH);
digitalWrite(M2_CLOSING, HIGH);
delay(500); //till the gates are closing
digitalWrite(M1_CLOSING, LOW);
digitalWrite(M2_CLOSING, LOW);
}
// the part of the programme that turns off the warning lights and opens the gate after 1 second
void op()
{
digitalWrite(M1_OPENING, HIGH);
digitalWrite(M2_OPENING, HIGH);
delay(500); //till the gates are opening
digitalWrite(M2_OPENING, LOW);
digitalWrite(M2_OPENING, LOW);
delay(1000);// L1 and L2 turn off 1 second after the gate opens to notify the drivers that the can drive
digitalWrite(L1, LOW);
digitalWrite(L2, LOW);
}