exclusive AND, OR gates

Hello,

I've searched around for exclusive logic gates and they don't seem to exist in Arduino language, but I'm sure there's a way around this. It's Arduino!

I have 2 pushbuttons (pins 5 and 8) and 2 LEDs (pins 12 and 13)

I want

button 5 AND 8 to blink both LEDs
button 5 to turn on LED 12
button 8 to turn on LED 13

I wonder if it would be possible to have some sort of exlusion, like XAND that would say :

turn on LED 12 and only LED 12 if button 5 and only button 5 is HIGH
turn on LED 13 and only LED 13 if button 8 and only button 8 is HIGH
blink both LEDs if buttons 5 AND 8 and only if buttons 5 AND 8 are HIGH

here's the code I made up,

button 5 turns on LED 12
button 8 turns on LED 13
both buttons blink LED 13 and turn on LED 12 with no blinking

const int buttonPin2 = 2;   
const int buttonPin5 = 5;    
const int buttonPin8 = 8;     
const int ledPin12 =  12;     
const int ledPin13 =  13;    

int buttonState2 = 0;         
int buttonState5 = 0;
int buttonState8 = 0; 

void setup() 
{
    
  pinMode(ledPin12, OUTPUT); 
  pinMode(ledPin13, OUTPUT);   

  pinMode(buttonPin2, INPUT); 
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin8, INPUT);
  
}

void loop()
{
  buttonState2 = digitalRead(buttonPin2);   
  buttonState5 = digitalRead(buttonPin5);
  buttonState8 = digitalRead(buttonPin8);

     if (buttonState5 == HIGH && buttonState8 == HIGH) 
    {     
      digitalWrite(ledPin12, HIGH);
      digitalWrite(ledPin12, HIGH);    
      delay(100);                     
      digitalWrite(ledPin13, LOW);
      digitalWrite(ledPin13, LOW);    
      delay(100); 
    } 
     if (buttonState5  == HIGH)
   {     
     digitalWrite(ledPin12, HIGH); 
   } 
     if (buttonState8  == HIGH )
   {     
     digitalWrite(ledPin13, HIGH); 
   } 
    else
   {
    digitalWrite(ledPin12, LOW);
    digitalWrite(ledPin13, LOW);  
   }
  
}

I also tried it with by implementing a third button for blinking

3 pushbuttons (pins 2,5 and 7) and 2 LEDs (pins 12 and 13)

I want

button 2 to blink both LEDs
button 5 to turn on LED 12
button 8 to turn on LED 13

here's the code I made up,

button 5 turns on LED 12
button 8 turns on LED 13
button 2 turns on LED 12 no blinking

const int buttonPin2 = 2;    
const int buttonPin5 = 5;    
const int buttonPin8 = 8;     
const int ledPin12 =  12;     
const int ledPin13 =  13;    

int buttonState2 = 0;         
int buttonState5 = 0;
int buttonState8 = 0; 

void setup() 
{
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin13, OUTPUT); 
  pinMode(buttonPin2, INPUT); 
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin8, INPUT);  
}

void loop()
{ 
  buttonState2 = digitalRead(buttonPin2);
  buttonState5 = digitalRead(buttonPin5);
  buttonState8 = digitalRead(buttonPin8);

     if (buttonState2 == HIGH) 
    {     
      digitalWrite(ledPin12, HIGH);
      digitalWrite(ledPin12, HIGH);    
      delay(100);                     
      digitalWrite(ledPin13, LOW);
      digitalWrite(ledPin13, LOW);    
      delay(100); 
    } 
      if (buttonState5  == HIGH)
    {     
      digitalWrite(ledPin12, HIGH); 
    } 
      if (buttonState8  == HIGH )
    {     
      digitalWrite(ledPin13, HIGH); 
    } 
       else
    {
      digitalWrite(ledPin12, LOW);
      digitalWrite(ledPin13, LOW);  
    }
  
}

Thanks!

Bitwise XOR is the circumflex "^"

I've seen this Bitwise in other posts but I wouldn't know how to use it in my example.

Could you please point me in a direction,

thanks

Bitwise is where you apply the logical operation to a bit pattern.

So, 0100 BITAND 1111 = 0100
0100 BITOR 1111 = 1111
0100 XOR 1111 = 1011

I think you mostly have it.

There are two kinds of 'exclusive'.
There is an operation on data, which is the XOR operator, '^' which changes the binary bits in a variable.

There is program control exclusive which is
if () {...} else if {...} else {}

Something like this for your first example:

void loop()
{
buttonState2 = digitalRead(buttonPin2);
buttonState5 = digitalRead(buttonPin5);
buttonState8 = digitalRead(buttonPin8);

if (buttonState5 == HIGH && buttonState8 == HIGH)
{
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin12, HIGH);
delay(100);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin13, LOW);
delay(90);
}
else if (buttonState5 == HIGH)
{
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin13, LOW);
}
else if (buttonState8 == HIGH )
{
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, HIGH);
}
else
{
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, LOW);
}

delay(10); // delay to let you see something

}

The logic of your second example is such that both leds are always turned off when buttonState8 == LOW.
You probably want to use "if () {...} else if () {} else if () {...} ...".

HTH
GB

Probably worth adding, that the first example might be a bit clearer if the "if" conditions were written out fully.

I took the liberty of reformatting to make it a bit more compact

void loop()
{
buttonState2 = digitalRead(buttonPin2);
buttonState5 = digitalRead(buttonPin5);
buttonState8 = digitalRead(buttonPin8);

if (buttonState5 == HIGH && buttonState8 == HIGH) {
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin12, HIGH);
delay(100);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin13, LOW);
delay(90);
} else if (buttonState5 == HIGH && buttonState8 == LOW) {
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin13, LOW);
} else if (buttonState5 == LOW && buttonState8 == HIGH ) {
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, HIGH);
} else if (buttonState5 == LOW && buttonState8 == LOW ) {
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, LOW);
}

delay(10); // delay to let you see something
}

I added a small delay at the end so that it avoids the switch bounce, and you get to see the LEDs.

HTH
GB

AWOL, I peeked on some info about Bitwise stuff, interesting but too advanced for me right now... It will probably become necessary at some point I guess, see you then :wink:

gbulmer Between 2 posts I had started rewriting the code and I was going in the same direction as you have. But the else if part was missing from my vocabulary!

But still, I uploaded the new code and I can't get the LEDs to blink!

When I push the 2 buttons, I get LED12 on, that's it. And it does the same for both versions of the code, same same!?

I'm wondering if playing around with code like the 2 examples bellow could produce different result for esoteric reasons?

else if (buttonState5  == HIGH && buttonState8 == LOW)     
  }

compared to

   else if (buttonState5  != LOW && buttonState8  != HIGH) {  
 
  }

I also wonder why you changed the second delay from 100 to 90 ms in the blinking sequence, is it to catch up with the 10 ms of the switch bounce delay?

I will continue playing around anyway,

thanks for the help, really appreciate it!

I'm wondering if playing around with code like the 2 examples bellow could produce different result for esoteric reasons?

Google terms "de Morgan's laws"

My mystake,

SORRY all is working good!

I copied the mistake in the second code...

12 HIGH
12 HIGH

13LOW
13LOW

Thanks

digitalWrite(ledPin12, HIGH);
      digitalWrite(ledPin12, HIGH);    
      delay(100);                    
      digitalWrite(ledPin13, LOW);
      digitalWrite(ledPin13, LOW);    
      delay(100);

Hummm ... hummm...
I kinda guess you mean:

digitalWrite(ledPin12, HIGH);
      digitalWrite(ledPin[glow]13[/glow], HIGH);    
      delay(100);                    
      digitalWrite(ledPin[glow]12[/glow], LOW);
      digitalWrite(ledPin13, LOW);    
      delay(100);

-Fletcher

Exactly!

I copied the mistake in the second code...

Doh! me too.

I also wonder why you changed the second delay from 100 to 90 ms in the blinking sequence, is it to catch up with the 10 ms of the switch bounce delay?

Well spotted. Yes, I wasn't sure how critical the flash time was. Must have been a bit puzzling. Sorry I didn't comment on that.

GB

It will pay you, in the long run, to get into the habit of using subroutines.

In the example above, a fine candidate is all of the code for blinking both LEDs.

If you add the following to the program....

void WinkBoth()
    {    
      digitalWrite(ledPin12, HIGH);
      digitalWrite(ledPin12, HIGH);    
      delay(100);                    
      digitalWrite(ledPin13, LOW);
      digitalWrite(ledPin13, LOW);    
      delay(90);
    }

Once that's in place, then in main part of your code you only need....

  if (buttonState5 == HIGH && buttonState8 == HIGH)_
{WinkBoth}

(There are other opportunities for code clarification, using the same tricks, elsewhere in the program under discussion.)

  if (buttonState5 == HIGH && buttonState8 == HIGH)_
{WinkBoth}

I think that there is an extraneous _ in there, and a missing pair of parentheses and a semicolon. Not to mention white space.

  if (buttonState5 == HIGH && buttonState8 == HIGH)
{
   WinkBoth[glow]();[/glow]
}