2 inputs required for 1 output (AND GATE)

Hi!

I am absolutly no genious when it comes co coding

what I want to do is have a output go high or low when 2 inputs are both high or low att the same time

in my head the logic thing would be some sort of a AND command but I cant find anything on the net mainly because I dont know what to look for !

I am using 2 hall effect sensors
one connected to pin 2 and one to pin 4
I have a led on 13 and 12 that lights up when the input for that output is activated
the thing is that I want port 8 that is my relay to go high when both 2 and 4 are activated just like a AND gate

I could use a 74hct08 and make a circuit but i want to code it and use the uno

I know the code is garbage but if i could please get a hint to correct it i vould be soo happy

here is my code

int hl1Pin = 4;
int hl2Pin = 2;
int led1Pin = 13;
int led2Pin = 12;
int relay1Pin = 8;
int hl1State = 0; 
int hl2State = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(hl1Pin, INPUT);
  pinMode(hl2Pin, INPUT);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(relay1Pin, OUTPUT);
  
}

void loop()
{
  hl1State = digitalRead (hl1Pin);

  if  (hl1State == LOW){
  
  digitalWrite (led1Pin, HIGH);
  }
  
  else {
    digitalWrite (led1Pin, LOW);
  }
  hl2State = digitalRead (hl2Pin);

  if  (hl2State == LOW){
  
  digitalWrite (led2Pin, HIGH);
  }
  
  else {
    digitalWrite (led2Pin, LOW);
  }
  hl1State = digitalRead (hl1Pin);
  hl2State = digitalRead (hl2Pin);
  
  if (hl1State == LOW)AND(hl2State == LOW){
    
    digitalWrite (relay1Pin, LOW);
  }

  else {
    digitalWrite (relay1Pin, HIGH);
  }
}

when 2 inputs are both high or low

which means that the inputs are equal? If so, change this:

if (hl1State == LOW)AND(hl2State == LOW){

to this:

if (hl1State == hl2State){

Pete

"in my head the logic thing would be some sort of a AND command but I cant find anything on the net mainly because I dont know what to look for !"

try the tab at the top of the forum

learning-reference

that will show you most of the basic commands

&& = and

p.s when posting please use ctrl+t then copy and paste between code tags its makes it easy for other people to copy the code for assistance and it makes reading it easier

Sounds like you want a logic XOR gate simulation. If both inputs are the same, the output is low.
Google XOR, see if that is your goal.

Maybe: if dio2 = dio4, output a low, else output a high.

maybe this way

if (hl1State == hl2State) {//are both the same..
  //they are both the same
  if (hl1State == LOW) {
    //is one low..to get to this point they are both the same
    //so test either one
    digitalWrite (relay1Pin, LOW);
  }
  else {//not low so must both be high
    digitalWrite (relay1Pin, HIGH);
  }
}//they are not the same... exit if

//notice how the code is stagered

It seems apparent that you want some logic function simulated. But it is not clear which. Google logic functions, and, or, xor, and let us know which one will work for you.

Looks to me that el_Supremo nailed it already.

jack from what im reading he wants to know when both are high do one thing or when both are low do something different
maybe im reading the original post wrong.

OP will have to explain the problem better.

"from what im reading he wants to know when both are high do one thing or when both are low do something different"
Well, that would still leave a third option. What if one is high, and the other is low ? What to do then?

Digital logic circuits (and, or, xor, inverter), generally only have a choice of two outputs, either high, or low, (no third option).

BlueScreenTT:
what I want to do is have a output go high or low when 2 inputs are both high or low att the same time

in my head the logic thing would be some sort of a AND command ...

You need to clarify your requirements. An AND command would be when both inputs are high, but not when both are low.

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" icon above the posting area. It is the first icon, with the symbol: </>

BlueScreenTT:
what I want to do is have a output go high or low when 2 inputs are both high or low att the same time

Huh?

Oh - I think he means that if the inputs are not the same, then the output should be left as it is.

Another way to do this kind of thing is by bit-fiddling and using a switch statement. Not really worth it when you have just the two booleans, but for more complicated cases, it can be. The advantage of a switch() statement is that it usually compiled down to a computed goto in machine code, and so can be faster and more compact than a series of nested ifs.

switch( input1 | (input2<<1)) {
case 0: // both inputs low
  break;
case 3: // bot inputs high
  break;
}

Now, let's say you only wanted to do whatever it is if the output doesn't match the inputs. Easy:

switch( input1 | (input2<<1) | (currentOutput<<2)) {
case 4: // both inputs low, output high
  break;
case 3: // bot inputs high, output low
  break;
}

You can make the truth table as complex as you like, at the price of possibly obscuring the logic.

Wau
what a response :slight_smile:
Thank you all soo much

what i wanted was to know how to do a "AND" code i couldent find it any where as i wrote i didnt know how to formulate my search!

yes i wanted to do a logic function it dont have to be low it could also be high that all depends how i want to use it !
i could use a NC relay together with the and function to get the door hold magnet to release when i want to enter

but i got a lot of ideas and code exampels on how to get on with my project now
Thank you so much for your help

lets try again i'm just babbling in the previous post

i have 2 hall effect sensors and one relay
the sensors go low then activated
they connect to 2 and 4
i have a NC relay connected on 8

when i activate sensor 1 or 2 nothing happens i only want output 8 to go high when both input 2 and 4 are activated (go LOW)

so the logic would be an AND gate with a NOT gate on the inputs

if (input2 == LOW && input4 == LOW) {...}

Got it ! :slight_smile:

Thank you all

int hl1Pin = 4;
int hl2Pin = 2;
int led1Pin = 13;
int led2Pin = 12;
int relay1Pin = 8;
int hl1State = 0; 
int hl2State = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(hl1Pin, INPUT);
  pinMode(hl2Pin, INPUT);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(relay1Pin, OUTPUT);
  
}

void loop()
{
  hl1State = digitalRead (hl1Pin);
  hl2State = digitalRead (hl2Pin);
  
  if  (hl1State == LOW){
  
    digitalWrite (led1Pin, HIGH);
  }
  
  else {
    digitalWrite (led1Pin, LOW);
  }
  
  if  (hl2State == LOW){
  
    digitalWrite (led2Pin, HIGH);
  }
  
  else {
    digitalWrite (led2Pin, LOW);
  }

  if  (hl1State == LOW && hl2State == LOW){
    digitalWrite (relay1Pin, HIGH);
  }
  
  else {
    digitalWrite (relay1Pin, LOW);
  }
}