I’m very new at this coding thing, I understand the logic but have a lot of learning ahead of me. I am designing a gate opener.
There are (4) inputs;
One NO push button, (2) NO limit switches, electric eye that basically operates as a NO switch and becomes NC if something in in the gate’s path.
The push button starts the gate in motion with a press, a second press if the gate is in motion will stop the gate, a third press will start the gate in motion (opposite direction), etc.
Once the gate is in motion there will be a 15 second (safety) timer that will stop the motor if a limit switch isn’t triggered first. If the gate is stopped mid-way the arduino should be able to calculate the time and use that value instead of 15 seconds, (this may be too hard to code).
When the gate is in motion and the push button is pressed the gate will stop, the next button press will cause the gate to be the opposite direction.
If a limit switch is triggered the next motion will be the opposite direction.
If the 15 second time limit is reached the gate will stop, the next motion will be the opposite direction.
In short, every time the motor / gate stops its next move will be the opposite direction.
I also want a ramp for the actuator motor on start and maybe stop, not sure on stop but it would be nice to play with.
The board I am using is an UNO and a Cytron shield MC-10
I am very new at programming, I have enlisted the help of a friend who did programming many years ago but is new at this Arduino thing but types fast and is real good at explaining what he is doing. We have looked at a lot of other code, copied some and played with it. I have the boards prototyped on the table, it works in some areas but integrating ALL the requirements is making for some long days.
Below is the code we have so far, ezbutton library has been added but it isn't used yet.
Here is what we have so far, we are both open for comments and help.
#include <ezButton.h>
#include "CytronMotorDriver.h"
CytronMD motor(PWM_DIR, 3, 4); // PWM = Pin 3, DIR = Pin 4.
//#define MotorDirection 3 //Pin to use for DIR control
//#define MotorSpeed 2 //Pin to use for PWM control
int buttonPin = A1;
int counter = 0;
int buttonState = 0;
int lastButtonState = 0;
int limitOpen = A2;
int limitClose = A3;
int lastState = 0;
int count = 0;
int currentButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 100; //Set button press time needed to start
void setup() {
pinMode(buttonPin,INPUT); //Set input pin as momentary switch that can be monitored
digitalWrite(buttonPin, INPUT_PULLUP);
pinMode (limitOpen, INPUT);
digitalWrite(limitOpen, INPUT_PULLUP);
pinMode (limitClose, INPUT);
digitalWrite(limitClose, INPUT_PULLUP);
Serial.begin(9600);
}
void loop()
{
motor.setSpeed(0);
currentButtonState = digitalRead(buttonPin);
if (currentButtonState != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (currentButtonState != buttonState) {
buttonState = currentButtonState;
}
if (buttonState == 0 && lastState == 0) {
openGate();
return;
}
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (currentButtonState != buttonState) {
buttonState = currentButtonState;
}
if (buttonState == 0 && lastState == 1) {
closeGate();
}
}
if (lastState == 0)
{
lastState = 1;
}
else{
lastState = 0;
}
lastButtonState = currentButtonState;
}
void openGate()
{
int i = 10;
Serial.println("Opening");
while (i > 0)
{
i--;
Serial.println(i);
motor.setSpeed(128);
delay(300);
currentButtonState = digitalRead(buttonPin);
Serial.println(currentButtonState);
if (currentButtonState == 0)
{
motor.setSpeed(0);
delay(1000);
return;
}
}
}
void closeGate()
{
int j = 10;
Serial.println("Closing");
while (j > 0)
{
j--;
Serial.println(j);
motor.setSpeed(-128);
delay(300);
currentButtonState = digitalRead(buttonPin);
Serial.println(currentButtonState);
if (currentButtonState == 0)
{
motor.setSpeed(0);
delay(1000);
return;
}
}
}