I have a sliding gate that i have built (no pre existing electrical or mechanical hardware) for my drive way that consists of a gate, on rollers, in a track connected at either end to a winch with a nylon rope.
The winch rotates clockwise, pulling the gate open, or ccw pulling the gate closed,
which was carried out manually by push (and hold) direction buttons,
on my quest to automate and reinvent the wheel,
I have added prox switches to either end of the gates travel,
I would like to have a single push button open the gate, then have a single push close the gate
(come to think of it, a single push to open then a timed period before auto closing would be better)
So far I have 'Come up' with this (I use the term loosely, I am trying to 'learn' all i can about coding but the temptation to pinch a slice of code here and there when Im just trying to gt something to work in my minut amount of spare time is too much)
const int buttonPin = 2;
const int openstopPin = 4;
const int closestopPin = 5;
const int openrelayPin = 6;
const int closerelayPin = 7;
boolean gateState = false; //false = closed true = open
int buttonState = 0;
volatile byte openrelayState = LOW;
volatile byte closerelayState = LOW;
void setup() {
Serial.begin(9600);
pinMode (buttonPin, INPUT);
pinMode (closestopPin, INPUT);
pinMode (openstopPin, INPUT);
pinMode (openrelayPin, OUTPUT);
pinMode (closerelayPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == HIGH && gateState == false) { //check to see if the button is pressed and the gate is closed
digitalWrite(openrelayPin, HIGH);
while(1){ //run motor until switch is tripped
if (digitalRead(openstopPin) == LOW) { //check switch state
gateState = true;
digitalWrite(openrelayPin, LOW);
break;
}
else digitalWrite(openrelayPin, HIGH);
}
}
if (digitalRead(buttonPin) == HIGH && gateState == true) { //check to see if the button is pressed and the gate is open
digitalWrite(closerelayPin, HIGH);
while(1){
if (digitalRead(closestopPin) == LOW) {
gateState = false;
digitalWrite(closerelayPin, LOW);
break;
}
else digitalWrite(closerelayPin, HIGH);
}
}
}
Now this code 'works' as in, i have it mocked in the bench and when the button is pushed, with one 'stop' input ground, it will switch the relays until the ground is removed and applied to the other 'stop' input and vill operate in the opposite fashion with the button pressed and the stops swapped,
however both relays are energised all the time with the arduino turning them off to cause the polarity/direction change.
as soon as i power up the arduino, the relays are energised... even tho the D/OUTPUT is LOW..... i test this with an LED circuit tester.....
before i rant anyfurther could someone take a look and advise me if im missing something very simple...
thanks for your time