Gate opener code NOOB needs help

I have written my code to use digital pins as inputs and outputs to read 3 open and closed switches and activate 2 relays. It does not activate the relays. I would like to add serial monitor to see what is going on also. Any help would be great.
Here is my code:

//https://forum.arduino.cc/t/2-inputs-required-for-1-output-and-gate/327548/15
//"O/S/C"=BUTTON TO OPEN
//PHO=SAFETY LOOP NEEDS TO BE ACIVATED WITH BUTTON TO ENSURE GATE DOES NOT CLOSE BECAUSE OV THE BUTTON
//UP&DOWN= OPEN AND CLOSED LIMIT SWITCHES
//IF BUTTON INPUT AND !CLOSED AND OPEN LIMIT = GATE IS OPEN, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
//IF BUTTON INPUT AND !CLOSED AND !OPEN LIMIT = GATE IS MOVING, WAIT 25000 OR UNTIL CLOSED LIMIT, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
//IF BUTTON INPUT AND CLOSED AND !OPEN LIMIT = GATE IS CLOSED, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
//IF ACCESS INPUT AND !CLOSED AND OPEN LIMIT = GATE IS OPEN, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY.
//IF ACCESS INPUT AND !CLOSED AND !OPEN LIMIT = GATE IS MOVING, WAIT 25000 OR UNTIL CLOSED LIMIT, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY.
//IF ACCESS INPUT AND CLOSED AND !OPEN LIMIT = GATE IS CLOSED, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY.
//TEST IF GATE OPENS WHILE CLOSING BY ACTIVATING SAFEY!!!!!!!!!!!!!!!


int buttoninPin = 4;
int openPin = 3;
int closedPin = 2;
//int accessPin = v1;
int buttonoutPin = 12;
int safetyPin = 11;
//int relay1Pin = 8;
//int hl1State = 0; 
//int hl2State = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(buttoninPin, INPUT);
  pinMode(closedPin, INPUT);
  pinMode(openPin, INPUT);
 
 // pinMode(accesPin, OUTPUT);
  pinMode(buttonoutPin, OUTPUT);
  pinMode(safetyPin, OUTPUT);

   digitalWrite (buttonoutPin, LOW);
   digitalWrite (safetyPin, LOW);
  
}

void loop()
{
  digitalRead (buttoninPin);
  digitalRead (openPin);
  digitalRead (closedPin);
  
//IF BUTTON INPUT AND !CLOSED AND OPEN LIMIT = GATE IS OPEN, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
  
  if  (buttoninPin == LOW && closedPin != LOW && openPin == LOW){
  
    digitalWrite (buttonoutPin, HIGH);
    delay (750);    
    digitalWrite (safetyPin, HIGH);
    delay (750);
    digitalWrite (buttonoutPin, LOW);
    delay (750);  
    digitalWrite (safetyPin, LOW);
 
  }
  
//IF BUTTON INPUT AND !CLOSED AND !OPEN LIMIT = GATE IS MOVING, PHO SAFETY 750.
//or WAIT 22000 OR UNTIL CLOSED LIMIT, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.

  
  /*if  (buttoninPin == LOW && closedPin == HIGH && openPin == HIGH){
   
    delay (22000);    
    digitalWrite (buttonoutPin, HIGH);
    delay (1000);    
    digitalWrite (safetyPin, HIGH);
    digitalWrite (buttonoutPin, LOW);
    delay (1000);  
    digitalWrite (safetyPin, LOW);
  }
*/
    if  (buttoninPin == LOW && closedPin != LOW && openPin != LOW){

      if  (closedPin == LOW && openPin != LOW){
    digitalWrite (safetyPin, HIGH);
    delay (750);
    digitalWrite (safetyPin, LOW);
    /*
    digitalWrite (buttonoutPin, HIGH);
    delay (750);   
    digitalWrite (safetyPin, HIGH);
    delay (750);
    digitalWrite (buttonoutPin, LOW);
    delay (750);
    digitalWrite (safetyPin, LOW);
    */
      }
     
  }
  
 //IF BUTTON INPUT AND CLOSED AND !OPEN LIMIT = GATE IS CLOSED, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
 

  if  (buttoninPin == LOW && closedPin == LOW && openPin != LOW){
   
    digitalWrite (buttonoutPin, HIGH);
    delay (750);  
    digitalWrite (safetyPin, HIGH);
    delay (750);  
    digitalWrite (buttonoutPin, LOW);
    delay (750);    
    digitalWrite (safetyPin, LOW);
 
  }
  
  else {
    digitalWrite (buttonoutPin, LOW);
    digitalWrite (safetyPin, LOW);
  }
}

SO MUCH depends on how you have everything wired and powered. A real schematic should be given so someone can actually tell if you have programmed correctly.
Paul

Please edit your post and put your code in code tags, use the </> icon in the posting menu.

Pins D2, D3 & D4 will ground when the switches are closed/activated. Pins D11 & D12 will go from 0v to 5v when activated. Do I have the pins labeled correctly in my code for the digital pins?

Thank you, that helped me make it show up better.

Then use INPUT_PULLUP in pinMode().

See S3 in the image below:

Thank you. Changed the code. It still does not change the state of the output pins. I may have made it overly complicated. I need to control 2 output based on 3 inputs.

//https://forum.arduino.cc/t/2-inputs-required-for-1-output-and-gate/327548/15
//"O/S/C"=BUTTON TO OPEN
//PHO=SAFETY LOOP NEEDS TO BE ACIVATED WITH BUTTON TO ENSURE GATE DOES NOT CLOSE BECAUSE OV THE BUTTON
//UP&DOWN= OPEN AND CLOSED LIMIT SWITCHES
//IF BUTTON INPUT AND !CLOSED AND OPEN LIMIT = GATE IS OPEN, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
//IF BUTTON INPUT AND !CLOSED AND !OPEN LIMIT = GATE IS MOVING, WAIT 25000 OR UNTIL CLOSED LIMIT, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
//IF BUTTON INPUT AND CLOSED AND !OPEN LIMIT = GATE IS CLOSED, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
//IF ACCESS INPUT AND !CLOSED AND OPEN LIMIT = GATE IS OPEN, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY.
//IF ACCESS INPUT AND !CLOSED AND !OPEN LIMIT = GATE IS MOVING, WAIT 25000 OR UNTIL CLOSED LIMIT, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY.
//IF ACCESS INPUT AND CLOSED AND !OPEN LIMIT = GATE IS CLOSED, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY.
//TEST IF GATE OPENS WHILE CLOSING BY ACTIVATING SAFEY!!!!!!!!!!!!!!!


int buttoninPin = 4;
int openPin = 3;
int closedPin = 2;
//int accessPin = v1;
int buttonoutPin = 12;
int safetyPin = 11;
//int relay1Pin = 8;
//int hl1State = 0; 
//int hl2State = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(buttoninPin, INPUT_PULLUP);
  pinMode(closedPin, INPUT_PULLUP);
  pinMode(openPin, INPUT_PULLUP);
 
 // pinMode(accesPin, OUTPUT);
  pinMode(buttonoutPin, OUTPUT);
  pinMode(safetyPin, OUTPUT);

   digitalWrite (buttonoutPin, LOW);
   digitalWrite (safetyPin, LOW);
  
}

void loop()
{
  digitalRead (buttoninPin);
  digitalRead (openPin);
  digitalRead (closedPin);
  
//IF BUTTON INPUT AND !CLOSED AND OPEN LIMIT = GATE IS OPEN, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
  
  if  (buttoninPin == LOW && closedPin != LOW && openPin == LOW){
  
    digitalWrite (buttonoutPin, HIGH);
    delay (750);    
    digitalWrite (safetyPin, HIGH);
    delay (750);
    digitalWrite (buttonoutPin, LOW);
    delay (750);  
    digitalWrite (safetyPin, LOW);
 
  }
  
//IF BUTTON INPUT AND !CLOSED AND !OPEN LIMIT = GATE IS MOVING, PHO SAFETY 750.
//or WAIT 22000 OR UNTIL CLOSED LIMIT, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.

  
  /*if  (buttoninPin == LOW && closedPin == HIGH && openPin == HIGH){
   
    delay (22000);    
    digitalWrite (buttonoutPin, HIGH);
    delay (1000);    
    digitalWrite (safetyPin, HIGH);
    digitalWrite (buttonoutPin, LOW);
    delay (1000);  
    digitalWrite (safetyPin, LOW);
  }
*/
    if  (buttoninPin == LOW && closedPin != LOW && openPin != LOW){

      if  (closedPin == LOW && openPin != LOW){
    digitalWrite (safetyPin, HIGH);
    delay (750);
    digitalWrite (safetyPin, LOW);
    /*
    digitalWrite (buttonoutPin, HIGH);
    delay (750);   
    digitalWrite (safetyPin, HIGH);
    delay (750);
    digitalWrite (buttonoutPin, LOW);
    delay (750);
    digitalWrite (safetyPin, LOW);
    */
      }
     
  }
  
 //IF BUTTON INPUT AND CLOSED AND !OPEN LIMIT = GATE IS CLOSED, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
 

  if  (buttoninPin == LOW && closedPin == LOW && openPin != LOW){
   
    digitalWrite (buttonoutPin, HIGH);
    delay (750);  
    digitalWrite (safetyPin, HIGH);
    delay (750);  
    digitalWrite (buttonoutPin, LOW);
    delay (750);    
    digitalWrite (safetyPin, LOW);
 
  }
  
  else {
    digitalWrite (buttonoutPin, LOW);
    digitalWrite (safetyPin, LOW);
  }
}

You have:


if  (buttoninPin == LOW && closedPin != LOW && openPin != LOW){

      if  (closedPin == LOW && openPin != LOW){

How can:
closedPin != LOW and at the same time closedPin == LOW


BTW
closedPin != LOW
same as
closedPin == HIGH

Thank you I commented out that second line and changed to == HIGH. It is still not changing the output.

Do I need to change from

digitalRead (buttoninPin); 
digitalRead (openPin); 
digitalRead (closedPin);

To

buttoninPin = digitalRead (4); 
openPin = digitalRead (3); 
closedPin = digitalRead (2);

Does the above code section work ?

The only thing that works is the pins being pulled up or down correctly for when no buttons are pressed.

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.

These lines don’t do anything by themselves.


int buttoninPin = 4;
. . .
if (buttoninPin == LOW && closedPin != LOW && openPin == LOW){

Since buttoninPin is set to 4 it cannot ever be LOW :wink:



You need:

byte buttoninPinState = digitalRead (buttoninPin);
byte openPinState = digitalRead (openPin);
byte closedPinState = digitalRead (closedPin);
. . .

if (buttoninPinState == LOW && closedPinState != LOW && openPinState == LOW){

etc . . .

Thank you. I think you have pinpointed the problem I will try that.

I have it working. How do I add serial monitor?

//https://forum.arduino.cc/t/2-inputs-required-for-1-output-and-gate/327548/15
//"O/S/C"=BUTTON TO OPEN
//PHO=SAFETY LOOP NEEDS TO BE ACIVATED WITH BUTTON TO ENSURE GATE DOES NOT CLOSE BECAUSE OV THE BUTTON
//UP&DOWN= OPEN AND CLOSED LIMIT SWITCHES
//IF BUTTON INPUT AND !CLOSED AND OPEN LIMIT = GATE IS OPEN, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
//IF BUTTON INPUT AND !CLOSED AND !OPEN LIMIT = GATE IS MOVING, SWITCH ON PHO SAFETY 750.
//IF BUTTON INPUT AND CLOSED AND !OPEN LIMIT = GATE IS CLOSED, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.



int buttoninPin = 4;
int openPin = 3;
int closedPin = 2;
//int accessPin = v1;
int buttonoutPin = 12;
int safetyPin = 11;
//int relay1Pin = 8;
//int hl1State = 0; 
//int hl2State = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(buttoninPin, INPUT_PULLUP);
  pinMode(closedPin, INPUT_PULLUP);
  pinMode(openPin, INPUT_PULLUP);
 
 // pinMode(accesPin, OUTPUT);
  pinMode(buttonoutPin, OUTPUT);
  pinMode(safetyPin, OUTPUT);

   digitalWrite (buttonoutPin, LOW);
   digitalWrite (safetyPin, LOW);
  
}

void loop()
{
  buttoninPin = digitalRead (4);
  openPin = digitalRead (3);
  closedPin = digitalRead (2);
  
//IF BUTTON INPUT AND !CLOSED AND OPEN LIMIT = GATE IS OPEN, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
  
  if  (buttoninPin == LOW && closedPin != LOW && openPin == LOW){
  
    digitalWrite (buttonoutPin, HIGH);
    delay (750);    
    digitalWrite (safetyPin, HIGH);
    delay (750);
    digitalWrite (buttonoutPin, LOW);
    delay (750);  
    digitalWrite (safetyPin, LOW);
    delay (2000);
  }
  
//IF BUTTON INPUT AND !CLOSED AND !OPEN LIMIT = GATE IS MOVING, PHO SAFETY 750.

    if  (buttoninPin == LOW && closedPin != LOW && openPin != LOW){

     
    digitalWrite (safetyPin, HIGH);
    delay (750);
    digitalWrite (safetyPin, LOW);
    delay (2000);
    
      }
     
  
 //IF BUTTON INPUT AND CLOSED AND !OPEN LIMIT = GATE IS CLOSED, SWITCH ON BUTTON OUTPUT 1000 THEN PHO SAFETY 1000.
 

  if  (buttoninPin == LOW && closedPin == LOW && openPin != LOW){
   
    digitalWrite (buttonoutPin, HIGH);
    delay (750);  
    digitalWrite (safetyPin, HIGH);
    delay (750);  
    digitalWrite (buttonoutPin, LOW);
    delay (750);    
    digitalWrite (safetyPin, LOW);
    delay (2000);
  }
  
  else {
    digitalWrite (buttonoutPin, LOW);
    digitalWrite (safetyPin, LOW);
  }
}

In setup( ) you need to set the baud rate (data transmission speed) for the serial monitor.
When you open the serial monitor window you have to match its baud rate to that which you set in your sketch.

Watch this: