Hi Ya folks as a 56yo with little to no programming knowledge apart from dos batch files.
I have been trying to get my head around this arduino thing ;
i think a finite state machine would be a better way but i cant seem to grasp the way to initilize it.
current issue is close gate function keeps on activating over and over in a loop. if i use the open gate button it works for one cycle then returns to a very high flash rate to the point the led just glows.
when it should light once then shut down.
if any one would like to point out where i am going wrong it would be great.
and in a very simple manner. once i grasp it its fine but reading and doing for me is not so easy.
each led represents one gate. red is open green is close,
thanking you all in anticipation.
shaun
/*
Made to run on Arduino uno clone
this code is to run linear actuators with out position sensors
but have inbuild stop switches in actuators
to be used as auto gate openers
useing 2 H bridge units to drive
delay time is to allow sufficent run time to open or close gates
overrun is not a concern as actuators stop at limits
*/
unsigned long Traffic=0; // traffic timer
unsigned long CloseGate=0; //Start time of timer
unsigned long GateRun=0; //Start time of timer
unsigned long Current_Time; // as states
const unsigned long GateRunTime = (30000); //reStart gates
const unsigned long GateHoldTime = (50000); //Pause timer for traffic
const unsigned long RunTime = (20000); //run timer for gate motion
// int pedestrian = 3; //open small gate only
int OpenGates = 4; // open both gates
int stopfeedA = 5; // sensor eye A to digital pin 5
int stopfeedB = 6; // sensor eye B to digital pin 6
int OpenGate2 = 7; // open gate to motor2 digital pin 7
int OpenGate1 = 8; // open gate to motor1 on digital pin 8
int CloseGate1 = 9; // close gate to motor1 on digital pin 9
int CloseGate2 = 10; // close gate to motor2 on digital pin 10
bool CloseOn=false; //check state gates closed
bool OpenOn=false; //check state gates open
bool HoldON=false; //check state gates hold
void setup()
{
Serial.begin(9600);
pinMode(OpenGate2, OUTPUT); // open gate 2
pinMode(OpenGate1, OUTPUT); // open gate 1
pinMode(CloseGate1, OUTPUT); // Close Gate 1
pinMode(CloseGate2, OUTPUT); // Close Gate 2
pinMode(stopfeedB, INPUT_PULLUP); // EMERGENCY STOP GATES
pinMode(stopfeedA, INPUT_PULLUP); // EMERGENCY STOP GATES
pinMode(OpenGates, INPUT_PULLUP); // OPEN GATES
}
void loop()
{//main loop
Current_Time = millis(); //get the current "time"
if (digitalRead(OpenGates) == LOW) //Button pressed
{
digitalWrite(OpenGate1, HIGH);
digitalWrite(OpenGate2, HIGH);
Serial.println("GateRun started and gates opening");
CloseOn = true;
OpenOn = true;
HoldON = true;
(GateRun = millis()); //start gate run timer
delay (5000); // testing only
}
if (OpenOn == true && Current_Time - GateRun >= GateRunTime);
{Serial.println("open gate going low");
digitalWrite(OpenGate1, LOW); // shutdown open gate
digitalWrite(OpenGate2, LOW); // shutdown open gate
Traffic = millis();
delay (5000); // testing only
}
if (HoldON == true && Current_Time - Traffic >= GateHoldTime);
{ GateRun = millis();
}
if (CloseOn == true && Current_Time - GateRun >= GateRunTime);
{
Serial.println("Traffic Delay already finished.Gates Closing now");
digitalWrite(CloseGate1, HIGH);
digitalWrite(CloseGate2, HIGH);
CloseOn = false;
HoldON = false;
delay (5000); // testing only
}
if (CloseOn == true && Current_Time - GateRun >= GateRunTime);
{
digitalWrite(CloseGate1, LOW);
digitalWrite(CloseGate2, LOW);
HoldON = false;
OpenOn = false;
CloseOn = false;
}
// THIS SECTION IS EMERGENCYSTOP
if (digitalRead(stopfeedB) == LOW)
{
Serial.println("Emergency stop B pressed stops gates");
digitalWrite(OpenGate1, LOW);
digitalWrite(OpenGate2, LOW);
digitalWrite(CloseGate1, LOW);
digitalWrite(CloseGate2, LOW);
}
if (digitalRead(stopfeedA) == LOW)
{
Serial.println("Emergency stop A pressed stops gates");
digitalWrite(CloseGate1, LOW);
digitalWrite(CloseGate2, LOW);
digitalWrite(OpenGate1, LOW);
digitalWrite(OpenGate2, LOW);
}
Serial.println("end script and rerun");
}