remote gate control help

Hi all, frustrated beginner here. I'm trying to write a program to drive a remote controlled gate. it will have three functions. 1/ Open then close after 30 seconds. 2/ Open and stay open. 3/ close from stay open. I have figured out how to make it open then close automatically and i can make it stay open also the close function works but functions 1 and 2 wont work together. When i press remote control button B(open and stay open) the gate opens then hits limit switch it does the delay then close function. please help me save what little hair i have left. Any help will be appreciated.

/*
 * Program to control an automatic gate
 * Three functions
 * (A) Open gate, stop on openlimit switch wait 30 seconds, close gate stop on closelimit switch.
 * (B) Open gate stop on openlimit switch.
 * (C) Close gate  from open state stop on closelimit switch.
 */

#define motorOpen  11       //
#define motorClose  12      //
#define remote_A  3          //
#define remote_B  4          //  Set pins
#define remote_C  7          //
#define openlimit  8         //
#define closelimit  9        //
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;

void remote_A_function() {
    if (val1 == HIGH){                       //If remote A button pushed
    digitalWrite(motorOpen, HIGH);     // Turn on motor and drive gate open
  }
  if (val4 == LOW) {                       // When open limit switch hit 
    digitalWrite(motorOpen, LOW);      // turn off motor to stop gate
    delay(3000);                              // wait ? time
    digitalWrite(motorClose, HIGH);    // Turn on motor nad drive gate closed
  }
  if (val5 == LOW){                          // When close limit switch hit 
    digitalWrite(motorClose, LOW);     // Turn off motor
  }
} 
void remote_B_function() {
  if (val2 == HIGH) {                          //If remote button B pushed.
    digitalWrite(motorOpen, HIGH);     //Turn on motor and drive gate open.
  }                                          
  if (val4 == LOW){                           //When openlimit switch hit
    digitalWrite(motorOpen, LOW);      //Turn off motor.
  }
}
void remote_C_function() { 
   if (val3 == HIGH && val5 ==HIGH){     //If remote button C pushed and gate is open.
    digitalWrite(motorClose,HIGH);       //Turn on motor and drive gate closed.
  }                                      
  if (val5 == LOW) {                           //When closelimit switch is hit.
    digitalWrite(motorClose,LOW);         //Turn motor off
  } 
}
void setup() {
  pinMode(motorOpen, OUTPUT);           //
  pinMode(motorClose,OUTPUT);           //
  pinMode(remote_A, INPUT);              //
  pinMode(remote_B, INPUT);              // Set inputs & outputs
  pinMode(remote_C, INPUT);              //
  pinMode(openlimit, INPUT);             //
  pinMode(closelimit, INPUT);            //
}


void loop() {
  val1 = digitalRead(remote_A);
  val2 = digitalRead(remote_B);
  val3 = digitalRead(remote_C);
  val4 = digitalRead(openlimit);
  val5 = digitalRead(closelimit);
  
  remote_A_function();                 //

  remote_B_function();                 // Call functions
  
  remote_C_function();                 //

}

Functions won't work together because you have calls to "delay". Get rid of them.

The code box is supposed to go around the code, not after it.

Thanks for the reply. I'm not sure how to do remote A function without the delay I need the gate to open then wait a certain period then close could you please point me in the right direction.

Store the time that you open the gate. Check the time elapsed each time through loop(), then when enough time has elapsed, close the gate again.

I have changed the delay function to a millis function found elsewhere on forum but it is still doing the same thing.
how do i separate the val4 (open limit switch) read from the other functions depending on which remote button was pressed beforehand.

When i press remote A the gate opens then stops on limit switch(val4) wait some time then closes.
When i press remote B i want the gate to open and stop on limit switch(val4) and just stay open until told to close(remote C).
But it just opens stops on the limit switch(val4) then waits then closes just like the remote A function

``

/*

  • Program to control an automatic gate
  • Three functions
  • (A) Open gate, stop on openlimit switch wait 30 seconds, close gate stop on closelimit switch.
  • (B) Open gate stop on openlimit switch.
  • (C) Close gate from open state stop on closelimit switch.
    */

#define motorOpen 11 //
#define motorClose 12 //
#define remote_A 3 //
#define remote_B 4 // Set pins
#define remote_C 7 //
#define openlimit 8 //
#define closelimit 9 //
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;

void setup() {
pinMode(motorOpen, OUTPUT); //
pinMode(motorClose,OUTPUT); //
pinMode(remote_A, INPUT); //
pinMode(remote_B, INPUT); // Set inputs & outputs
pinMode(remote_C, INPUT); //
pinMode(openlimit, INPUT); //
pinMode(closelimit, INPUT); //
}

void loop() {
val1 = digitalRead(remote_A);
val2 = digitalRead(remote_B);
val3 = digitalRead(remote_C);
val4 = digitalRead(openlimit);
val5 = digitalRead(closelimit);

remote_A_function(); //
//
remote_B_function(); // Call functions
//
remote_C_function(); //

}

void remote_A_function() {

if (val1 == HIGH){ //If remote A button pushed
digitalWrite(motorOpen, HIGH); // Turn on motor and drive gate open
}
if (val4 == LOW) {
digitalWrite(motorOpen, LOW);
unsigned long start_time;
unsigned long current_time;
unsigned long elapsed_time;

start_time = millis();
do {
current_time = millis();
elapsed_time = current_time - start_time;
}
while(elapsed_time < 3000);
// When open limit switch hit
digitalWrite(motorClose, HIGH); // Turn on motor nad drive gate closed
}
if (val5 == LOW){ // When close limit switch hit
digitalWrite(motorClose, LOW); // Turn off motor
}
}
void remote_B_function() {
if (val2 == HIGH) { //If remote button B pushed.
digitalWrite(motorOpen, HIGH); //Turn on motor and drive gate open.
}
if (val4 == LOW){ //When openlimit switch hit
digitalWrite(motorOpen, LOW); //Turn off motor.
}
}
void remote_C_function() {

if ((val3 == HIGH) && (val4 ==LOW)){ //If remote button C pushed and gate is open.
digitalWrite(motorClose,HIGH); //Turn on motor and drive gate closed.
}
if (val5 == LOW) { //When closelimit switch is hit.
digitalWrite(motorClose,LOW); //Turn motor off
}
}

    start_time = millis();
do {
    current_time = millis();
    elapsed_time = current_time - start_time;
}
while(elapsed_time < 3000);

Congratulations on reinventing the delay function.

At no time was it suggested that this was the way to use millis.

I’d suggest that you figure out how YOU (not the Arduino) would perform the actions you want the Arduino to perform, if all you had as a watch and a pad of paper, and someone was using a cattle prod to make you do stuff.

A state machine is a way to do it - search the forums for examples. Also, I'm a little surprised that it hasn't been mentioned yet - it's pretty much a law of physics that in these circumstances, you will be advised to look at the blink without delay example to see how millis can be used.

PaulS Ok Guy pokes me with cattle prod and shouts remote A i look down at my paper it says open gate start timer wait however long close gate i do that. Guy pokes me with cattle prod and shouts remote B i look down at my paper it says open gate and leave it open i do that. Guy pokes me with cattle prod and shouts remote C i look down at my paper it says close gate i do that.

some constructive criticism would be nice :)

wildbill thank you i will look those up but i thought blink without delay was just doing sone thing repetative without using the delay function i will look at it again.

No, no "wait however long" Waiting is wasteful. Tell the gate to start closing any go off and do something else. What if the guy prods you whilst you're waiting - are you just going to ignore him?

Just start with the Remote a function.

Guy pokes you and says Open the gate, then looks at his watch and writes the time down. Guy looks at his watch again - have 3 seconds passed? No. Guy looks at his watch again - have 3 seconds passed? No. Guy looks at his watch again - have 3 seconds passed? Yes. Guy pokes you and says Close the gate.

dxwOOD So are you saying i need separate functions for open gate, waiting time and close gate?

You would have separate functions for open gate and close gate. The time checking happens inside loop().

You need a statement (or function) to open the gate (depending on what opening the gate actually involves) and a statement to define when that happened. You need a statement (or function) to close the gate (depending on what closing the gate actually involves).

You need to write some code to determine if it is now time to close the gate, if it is open.