There are two input buttons, one output LED, and a logic select button.
The LCD displays the two input states (as either 1s or 0s, 1 for on, 0 for off,) the current logic gate, and it also shows the output status.
So, when starting it up, the Gate starts as AND. Pressing both input buttons at the same time turns on the LED and the LCD shows when the inputs are on, when the LED is on, and what gate it is currently on.
Pressing the logic select button switches the gate to OR, so when one or both of the buttons are pressed the light turns on.
And you can keep cycling through AND, OR, NOT, YES, NAND, NOR, XOR, and XNOR.
Note: the NOT and YES gates only use one button, the first input button. The second input does nothing when these gates are selected.
To hook this up, connect Arduino pins:
2 to input button 1
3 to input button 2
4 to next button
5 to output LED
8 goes to LCD RS pin
9 to LCD enable
10 to D4
11 to D5
12 to D6
13 to DC
Remember to hook up LCD pins Vss, or Gnd, and RW to ground, vcc to +5V, and V0 to a variable potentiometer.
Do these colors help to separate the explanations, or are they just annoying? By the way, I would love all comments on the code if you have any.
Code: Select all
//logic gate program fOR 1 AND 2 input logics with LCD suppORt
#include <LiquidCrystal.h>
int select=0; //the select state
int selectAmount=8; //amount of select positions, fOR 1 position put 1, etc.
int output=0; //the output logic
int selectButton=4; //the select button
int input1=0; //first input state
int input2=0; //second input state
int outputLED=5; //the output logic pin
int input1Button=2; //first input button
int input2Button=3; //second input button
unsigned long pressTime=500; //time between selectButton presses in miliseconds
unsigned long pressedTime=501; //time since select was last pressed (501 is just start of sketch value)
const char LCDcharacters[][9] = {"AND ", "OR ", "NOT ", "YES ", "NAND", "NOR ", "XOR ", "XNOR"}; //logic gate LCD names
LiquidCrystal lcd(8, 9, 10, 11, 12, 13); //defines LCD pins (RS, Enable, D4, D5, D6, D7)
void setup(){
pinMode(input1, INPUT); //sets inputs as inputs
pinMode(input2, INPUT);
pinMode(selectButton, INPUT); //sets the select button as an input
pinMode(outputLED, OUTPUT); //sets output LED as output
digitalWrite(outputLED, LOW); //starts the LED off
lcd.begin(16, 2); //Define LCD rows AND Columns
}
void loop(){
constantUpdates(); //runs constant updates
AND(); //runs all logic gate functions
OR();
NOT();
YES();
NAND();
NOR();
XOR();
XNOR();
LCD(); //runs the LCD
}
void constantUpdates(){ //actions that need to happen continuously
writeLED(); //update the LED status
input1=digitalRead(input1Button); //inputs read the input buttons
input2=digitalRead(input2Button);
selectStatus(); //updates the select button position
}
void LCD(){
lcd.setCursor(0,0); //set where to display starting characters
lcd.print("IN:");
lcd.setCursor(4,0); //prints input1 status (0 or 1, on or off)
if(input1==1){
lcd.print("1, ");
}
else{
lcd.print("0,");
}
lcd.setCursor(6,0); //prints input2 status
if(input2==1){
lcd.print("1");
}
else{
lcd.print("0");
}
lcd.setCursor(0,1); //prints the output status, on or off
if(output==1){
lcd.print("OUT: On ");
}
else{
lcd.print("OUT: Off ");
}
lcd.setCursor(11,0); //prints the word gate
lcd.print("GATE:");
lcd.setCursor(12,1);
for(int y=0; y < selectAmount; y++){ //prints the logic gate name
if(select==y){
lcd.print(LCDcharacters[y]);}
}
}
void AND(){ //AND logic
if(select==0){
if(input1==1 && input2==1){
output=1;
}
else{
output=0;
}
}}
void OR(){ //OR logic
if(select==1){
if(input1==1 || input2==1){ //if input 1 OR 2 is on, output=1
output=1;
}
else{
output=0;
}
}}
void NOT(){ //NOT logic
if(select==2){
if(input1==1){ //if input1 is on, output is off
output=0;
}
else{
output=1;
}
}}
void YES(){ //"YES" logic
if(select==3){
if(input1==1){ //if input is 1, output is 1
output=1;
}
else{
output=0;
}
}}
void NAND(){ //NAND logic
if(select==4){
if(input1==1 && input2==1){ //if both input 1 AND 2 are on, output is off
output=0;
}
else{
output=1;
}
}}
void NOR(){ //NOR logic
if(select==5){
if(input1==0 && input2==0){ //if both inputs off, output on
output=1;
}
else{
output=0;
}
}}
void XOR(){ //exclusive OR logic
if(select==6){
if(input1==input2){ //if input 1 is NOT equal to input 2 then output on
output=0;
}
else{
output=1;
}
}}
void XNOR(){ //exclusive NOR logic
if(select==7){
if(input1==input2){ //if input 1 is equal to input 2 then output on
output=1;
}
else{
output=0;
}
}}
void writeLED(){ //write outputLED HIGH OR LOW
if (output==1){
digitalWrite(outputLED, HIGH);
}
if (output==0){
digitalWrite(outputLED, LOW);
}
}
void selectStatus(){ //the state of select button
if((digitalRead(selectButton))==HIGH){ //when selectButton is pressed, select increases if changeTime amount has gone past since last press
select++; // adds 1 to select
delay(300);
}
if(select >= selectAmount){ //select resets when it goes to the set amount
select=0;
}
if(pressedTime >= 30000){ //to avoid overflow, if pressedTime exceeds 32.768 seconds, it is reset AND held at pressTime+1
pressedTime=(1+pressTime);
}
}