Can anyone see any errors in my code that would affect the opening command? Upon first power up the open command (remoteA) works fine.
After that the opening command does what its supposed to except it only momentarily fires "digitalWrite (power, LOW)" command. It should be set LOW for the same amount of time as "digitalWrite (openGate, LOW)", but it doesnt.
Strange thing is, the close command code is almost identical and it always works as it should, no problems.
Any suggestions? (hopefully the code pastes in here ok, its acting weird)
/*
* Swing Gate Opener with 2Ch remote, gate lock, motor, buzzer, amp monitor
and two independent limit switches
*
* By Darren Clarke
*
*/
int current = A0; // monitor motor current
int buzzer = 3; // operation buzzer
int power = 5; // 24v power on/off
int remoteOpen = 6; // remote button A to digital pin 6
int remoteClose = 7; // remote button B to digital pin 7
int openGate = 8; // open gate to motor on digital pin 8
int closeGate = 9; // close gate to motor on digital pin 9
int gateLock = 10; // gate lock connected to digital pin 10
// variables
int openRelay = 0; // variable to store the motor direction
int closeRelay = 0; // variable for previous motor direction
int remoteA = 0; // variable for remote A condition
int remoteB = 0; // variable for remote B condition
int lock = 0; // varialbe for gate lock
int mA = 0; // variable for motor current
unsigned long previousMillisA = 0;
unsigned long previousMillisB = 0;
unsigned long currentMillisA = 0;
unsigned long currentMillisB = 0;
void setup() {
// put your setup code here, to run once:
pinMode(buzzer, OUTPUT); // sets the digital pin 4 as output
pinMode(power, OUTPUT); // sets the digital pin 5 as output
pinMode(remoteOpen, INPUT); // sets the digital pin 6 as input
pinMode(remoteClose, INPUT); // sets the digital pin 7 as input
pinMode(openGate, OUTPUT); // sets the digital pin 8 as output
pinMode(closeGate, OUTPUT); // sets the digital pin 9 as output
pinMode(gateLock, OUTPUT); // sets the digital pin 10 as output
digitalWrite(closeGate, HIGH); // sets relay to open
digitalWrite(openGate, HIGH); // sets relay to open
digitalWrite(power, HIGH); // sets relay to open
digitalWrite(gateLock, HIGH); // sets relay to open
analogWrite(buzzer, 0); // sets buzzer off
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
remoteA = digitalRead(remoteOpen); // read remote A input
remoteB = digitalRead(remoteClose); // read remote B input
openRelay = digitalRead(openGate); // read state of openGate
closeRelay = digitalRead(closeGate); // read state of closeGate
lock = digitalRead(gateLock); // read state of gate lock
mA = analogRead(current); // read value of motor current
currentMillisA = millis();
currentMillisB = millis();
Serial.print("aIn ");
Serial.print(remoteA);
Serial.print(", bIn ");
Serial.print(remoteB);
Serial.print(", Open ");
Serial.print(openRelay);
Serial.print(", Close ");
Serial.print(closeRelay);
Serial.print(", Lock ");
Serial.print(lock);
Serial.print(", mA ");
Serial.print(mA);
Serial.print(", cur ");
Serial.print(currentMillisA);
Serial.print(", prev ");
Serial.print(previousMillisA);
Serial.println(" ");
// to open the gate
if ((remoteA == HIGH) && (remoteB == LOW)) {
delay(500); // time for gate direction reversal
digitalWrite(power, HIGH); // stops previous movement
digitalWrite(closeGate, HIGH); // Stop gate open command
analogWrite(buzzer, 100); // starts buzzer
digitalWrite(gateLock, LOW); // energize lock open (unlock state)
delay(500); // time for lock to open
digitalWrite(openGate, LOW); // actuate gate open
digitalWrite(power, LOW); // supplies 24v power
previousMillisA = millis(); // set time references
currentMillisA = millis(); // set time references
}
// to stop open command
if ((closeRelay == HIGH) && (currentMillisA - previousMillisA) >= 6000) {
digitalWrite(gateLock, HIGH); // denergize lock (locked state)
digitalWrite(openGate, HIGH); // stop open relay to motor after open
digitalWrite(power, HIGH); // remove 24v power
analogWrite(buzzer, 0);
}
// to close the gate
if ((remoteB == HIGH) && (remoteA == LOW)) {
delay(500); // time for gate direction reversal
digitalWrite(power, HIGH); // stops previous movement
digitalWrite(gateLock, HIGH); // de-energize lock on quick reverse
digitalWrite(openGate, HIGH); // Stop gate close command
analogWrite(buzzer, 100); // starts buzzer
digitalWrite(closeGate, LOW); // actuate gate close
digitalWrite(power, LOW); // supplies 24v power
previousMillisB = millis(); // set time references
currentMillisB = millis(); // set time references
}
// to stop close command
if ((openRelay == HIGH) && (currentMillisB - previousMillisB) >= 6000) {
digitalWrite(closeGate, HIGH); // stop close relay to motor
digitalWrite(power, HIGH); // remove 24v power
analogWrite(buzzer, 0);
}
delay(100);
}