Sie sind auf Seite 1von 5

Arduino Logic: NOT

The purpose of a NOT gate in digital logic is to invert the input. If the input is HIGH,
the output is LOW and vice versa. It's a very simple matter to replicate this on the
Arduino. We need one pushbutton and one LED as a visual indicator of the logic
status. So, let's set up a pushbutton to bring a pin reliably low. Here's how:

I use the larger two-terminal buttons for preference because they aren't as fiddly to
wire and if I'm doing it on a breadboard they're more suited. Whichever you use,
here's how to wire:

Here's some code to slap your Arduino with:


// NOT Simulator
void setup(){
pinMode(2,OUTPUT);
}
void loop(){
if (digitalRead(3)==HIGH){digitalWrite(2,0);}
else{digitalWrite(2,1);}
}
So, assuming all has gone well, pressing the button switches the LED off and
releasing the button switches the LED back on again. This is because as pin 3 goes
high, pin 2 goes low and vice-versa courtesy of the if ....else statement in the loop.
You can use this to temporarily pause a task. If the sensor is a force one, it can stop
a servo turning, if a proximity it can stop something moving, if it's light it can be
used to switch off a lighting circuit.

Arduino Logic: AND


A discrete AND gate has two inputs and one output. If either input is low, or both
inputs are low, the output will be low too. Only when both inputs are high will the
output be high as well.
Here's the buttons again:

And here's how to wire:

The sketch is very short:


// AND Simulator
void setup(){
pinMode(2,OUTPUT);
}
void loop(){
if (digitalRead(3)==HIGH&&digitalRead(4)==HIGH){digitalWrite(2,1);}
else{digitalWrite(2,0);}
}
and assuming all has gone well what you'll find is that if press S1 nothing happens.
If you press S1 and S2 at the same time though, the LED comes on. This is due to
the if...else statement in the loop. Let's look at the argument in the if more closely.
if (digitalRead(3)==HIGH&&digitalRead(4)==HIGH)
There are two parts connected by && and that symbol of && means a boolean
AND. In plain English, both parts of the argument have got to be true before the
code under the conditional can be satisfied.
To be true, both pin 3 and 4 have got to be high. Because the switches have been
wired to keep these pins reliably low when not pressed, neither can be high until the
switch is pressed.
Using toggle switches you can use a system like this for remotely isolating
machinery that for any reason you don't want to be started by an operator on the

shop floor, or disabling alarms when not needed. It can also be used where two
consenting operators are needed, such as weapons launching.
Arduino Logic: OR
An OR gate will have a high output when either or both of its inputs are high, and a
low output when neither of its inputs are high. The wiring can be done in exactly the
same way as for the AND gate:

And here's how to wire your Arduino:

The code is not that much more complex either:


// OR Simulator
void setup(){
pinMode(2,OUTPUT);
}
void loop(){
if (digitalRead(3)==HIGH||digitalRead(4)==HIGH){digitalWrite(2,1);}
else{digitalWrite(2,0);}
}
What will happen here is that the LED will be off. Press either button and it will go
on. Release either button and the LED will go off and stay off.
The basis of this lies in the if....else statement. Let's look at the argument:
if (digitalRead(3)==HIGH||digitalRead(4)==HIGH){digitalWrite(2,1);}
The double pipe || is a boolean OR and means that if either condition is satisfied
that code under the conditional will be executed. The condition is that if pin 3 OR
pin 4 are high, and they can only be high when the buttons attached to them are
pressed.

An OR gate is used to measure events from disparate parts of systems when an


event is required common to those parts, or to stop an event happening when not
required.
Arduino Logic: XOR
An XOR gate's output will be low when neither input is high, and also when both
inputs are high. The output will be high when either input is high but not both.
Here's the wiring for this experiment:

As before we will use two buttons attached to A1 ans A2 passing a 5V supply to


simulate a digital signal.
The sketch this time is a little more complex, but not much so:
// XOR Simulator
void setup(){
pinMode(2,OUTPUT);
}
void loop(){
boolean LEDON=false;
if (digitalRead(3)==HIGH&&digitalRead(4)==LOW){LEDON=true;}
if (digitalRead(4)==HIGH&&digitalRead(3)==LOW){LEDON=true;}
if (LEDON==true){digitalWrite (2,1);}
else{digitalWrite (2,0);}
}
The loop continues to cycle indefinitely and so every time it begins a cycle the
boolean variable LEDON gets set to false and it stays that way unless one of the
following conditionals is satisfied:
if (digitalRead(3)==HIGH&&digitalRead(4)==LOW){LEDON=true;}
if (digitalRead(4)==HIGH&&digitalRead(3)==LOW){LEDON=true;}

Each conditional refers in turn to the input pins as being high or low. There are four
possible combinations:

Both are low

Both are high

3 is low and 4 is high

4 is high and 3 is low

The latter two is what these conditionals look for and if found the variable LEDON is
set to true. It can't go back to false until the loop starts again and so now we've got
a reference for an if...else statement:
if (LEDON==true){digitalWrite (2,1);}
else{digitalWrite (2,0);}
An XOR can be used as a pause/interrupt to stop a device doing whatever it does
temporarily until the second pulse is removed without resetting or switching off the
rest of the device.
http://ianlangelectronic.webeden.co.uk/#/arduino-logic/4571280726

Das könnte Ihnen auch gefallen