Logic Gate Simulator Arduino and LCD

Post here to discuss all things Arduino!

Moderators: Chuckt, Garth, bitfogav

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Logic Gate Simulator Arduino and LCD

Post by Saimaster13 » Thu Sep 20, 2012 12:22 pm

I made a code for a logic gate simulator.
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);
  }
}
    
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Logic Gate Simulator Arduino and LCD

Post by brad » Thu Sep 20, 2012 8:13 pm

I
think
that
the
different
colors
are
really
cool
and
everyone
on
this
forum
should
do
the
same!

As for your code, it looks pretty good to me. That is basically how I would select from different options (by assigning them a number and cycling through the number) I think it would be a great little device to make up, house in a little box as a little training tool for new comers to digital electronics!

Do you have a few photo's of the project?

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Logic Gate Simulator Arduino and LCD

Post by Saimaster13 » Thu Sep 20, 2012 11:36 pm

I could get some pictures, or maybe even a video. What would be better than this to teach newcommers is to have a program that does both this AND can simulate combined logic gates. Currently working on it now.
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Logic Gate Simulator Arduino and LCD

Post by brad » Fri Sep 21, 2012 7:31 am

Saimaster13 wrote:I could get some pictures, or maybe even a video. What would be better than this to teach newcommers is to have a program that does both this AND can simulate combined logic gates. Currently working on it now.
Now that is a fantastic idea!

Although how would you implement it? a graphical interface or some sort of text based interface?

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Logic Gate Simulator Arduino and LCD

Post by Saimaster13 » Fri Sep 21, 2012 8:00 am

Just got finished with the mode select prototype. It displays on the LCD and goes something like this:

The screen being displayed is defined by a variable, lets call it "screen"

if screen=0 then the startup screen code runs. The startup code has in it code for a potentiometer, and with that potentiometer you can choose between the two modes, basic or complex. When a select button is pressed, the Arduino reads whether basic or complex was selected, and then sets screen to 1 if its on basic or 2 if its on complex.

The screen then clears its display (since if something is not overwritten, it stays and needs to be overwritten) then jumps to either the basic display and code or the complex display and code.

Pretty simple when you create all the code in different void functions. Probably takes up more bytes, but when you get to implementing the main code, it is easy to put whole functions in the loop where you want them to run, like this:

Code: Select all

int toggle=1; //allows toggle for clearing the display (see void ClearScreenOnce)
int screen=0; //displays startup screen at beginning


void setup(){}

void loop(){
  if(screen=0){         //displays startup screen at beginning
    startUpScreen();  //startUpScreen code, defined by void startUpScreen() function (I did not define it in this example)
  }
  if(screen=1){
    screenClearOnce();  //if basic is selected on startup screen, screen is set to 1 in the startup screen code and
    basicScreen();        //BasicScreen code starts after screen is cleared
  }
  if(screen=2){
    screenClearOnce();  //same thing for complex thing if it is selected
    ComplexScreen():
  }
}



void screenClearOnce(){ //what can be done to clean the screen
  if(toggle==1)              //toggle is used to start it
    lcd.setCursor(0,0);        //floods display with all spaces, effectivelly clearing it
    lcd.print("                 ");
    lcd.setCursor(0,1);
    lcd.print("                 ");
    toggle=0;                      //sets toggle to 0 making so it does not reactivate and only runs once
  }
}
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Logic Gate Simulator Arduino and LCD

Post by brad » Fri Sep 21, 2012 9:18 pm

Great minds think alike, this is basically how I do all my different menu systems!

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Logic Gate Simulator Arduino and LCD

Post by Saimaster13 » Sat Sep 22, 2012 2:21 am

To select which screen I want, I use one button. Problem is, when you press that button, the Arduino tries to load the next screen (it takes about 1.5 seconds to do so) and when it does, it thinks that the select button is still HIGH and skips to the third screen. I have implemented a 7 second delay in between when you can press the button, but it seems unstable. Also, any lower than 7 seconds and it may or may not keep the screen from switching. is there a better way to do this?

I have also tried a small delay from the first screen to screen 2, but it did not help


Transition from 2 to 3:

Code: Select all


void loop(){
  if(screen==2){ //screen 2
    timeElapsed = millis(); //prevents the select button from being read as HIGH when it is not supposed to
    gateNumScreen(); //this screen is being run

}

void gateNumScreen(){
  if(selectButton==1 && timeElapsed > 7000){  //wait 7 seconds until screen can be switched
      screen = 3;            //go to screen 3
 }
}
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Logic Gate Simulator Arduino and LCD

Post by brad » Sat Sep 22, 2012 8:57 am

I use a piece of code that will only allow you to check if the button has been pressed, it if is not currently pressed (if that makes sense.) So you setup a flag that is set, when the button is pressed and cleared when it is not pressed.

Here's some code (written in basic but it should be super simple to port to arduino):

Code: Select all

Sub CheckButton()
    If ButtonHasBeenPressed = false Then   ' only allow something to happen if the button has NOT been pressed yet.
        If Button = 1 Then  ' this means the button is pressed 
            ButtonHasBeenPressed = true ' now that has been pressed, we won't be able to run this piece of code until the button is released, and then pressed again.
            ' PUT YOUR CODE HERE FOR WHEN THE BUTTON GETS PRESSED
        EndIf 
    else '  if the button HAS been pressed then check to see if it has been released.
        If Button = 0 Then  ' check if the button has now been released
            ButtonHasBeenPressed = false    ' now that the button has been released, clear the flag which will allow us to do something when it actually is pressed again
        endif
    EndIf
End Sub

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Logic Gate Simulator Arduino and LCD

Post by Saimaster13 » Sat Sep 22, 2012 9:33 am

That's an awesome idea! Its so simple and will help in.... half the codes I plan to write. Thanks Brad!
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Logic Gate Simulator Arduino and LCD

Post by brad » Sat Sep 22, 2012 9:03 pm

Saimaster13 wrote:That's an awesome idea! Its so simple and will help in.... half the codes I plan to write. Thanks Brad!
No worries, it's a great little piece of code when you don't need a repeat rate function for your button (kinda like your computer keyboard has when you press and hold a key and it keeps writing that letter over and over again...)

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Logic Gate Simulator Arduino and LCD

Post by Saimaster13 » Sun Sep 23, 2012 3:35 pm

The code I've been making is too messy, too many variables and functions making it really hard to keep track of. It was pretty simple when I had one menu, but now that I want around six its just too complicated. Clean up time it is...
Joshua

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Logic Gate Simulator Arduino and LCD

Post by Saimaster13 » Sun Sep 23, 2012 3:37 pm

Current amount of redundant variables found: 2
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Logic Gate Simulator Arduino and LCD

Post by brad » Mon Sep 24, 2012 7:44 am

How's the cleanup coming along?

I have also made some messy code when making a menu system. You have main menu's, which go into sub menu's which can go into sub - sub menu's!

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Logic Gate Simulator Arduino and LCD

Post by Saimaster13 » Tue Sep 25, 2012 9:05 am

The cleanup was going well, but now I need to make some hardware (analog input buttons) to test it while I work on it. The cleanup was good, I shortened the code, and learned what did and did not work. apparently when you set a variable to work as a function's variable and then set the function variable to a value, the initial variable does not change, which is disappointing. I am pretty sure I can get past that though.
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Logic Gate Simulator Arduino and LCD

Post by brad » Tue Sep 25, 2012 8:58 pm

So you can't modify global variables in functions?

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests