Lesson 12 – Logic Gates code
In this Lesson I will teach about LOGIC Gates, with a very FUN and Simple Program.
The Program will Light LEDS and Print On Screen According to LOGIC GATES: AND, OR, NOT, NAND (other will be covered in Next lessons)
// By Roee Bloch LOGIC GATE LEARNING //All right Reserved // Copyright (c) 2015 All Right Reserved, http://www.electronics-freak.com // // This source is subject to the Roee Bloch License. // Please see the License.txt file for more information. // All other rights reserved. // // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A // PARTICULAR PURPOSE. // // </copyright> // <author>Roee Bloch</author> // <email>roeebloch@walla.co.il</email> // <date>June 2015</date> #define AND1 2 #define AND2 3 #define ANDOUT 4 #define OR1 5 #define OR2 6 #define OROUT 7 #define MYNOT 8 #define NOTOUT 9 #define NAND1 10 #define NAND2 11 #define NANDOUT 12 boolean and1, and2, or1, or2, mynot, nand1, nand2; boolean andout, orout, notout, nandout; unsigned long int mytime = 0; void setup() { Serial.begin(57600); mymessage(); pinMode(AND1, INPUT); // set pin to input pinMode(AND2, INPUT); // set pin to input pinMode(ANDOUT, OUTPUT); // set pin to output pinMode(OR1, INPUT); // set pin to input pinMode(OR2, INPUT); // set pin to input pinMode(OROUT, OUTPUT); // set pin to output pinMode(MYNOT, INPUT); // set pin to input pinMode(NOTOUT, OUTPUT); // set pin to output pinMode(NAND1, INPUT); // set pin to input pinMode(NAND2, INPUT); // set pin to input pinMode(NANDOUT, OUTPUT); // set pin to output mytime = millis(); Serial.println("Starting in 10 Seconds from now.."); delay(10000); Serial.println("S T A R T E D .........................."); } void loop() { //make sure all INPUTs are pull down digitalWrite(AND1, LOW); // turn on pullup resistors and1 = digitalRead(AND1); digitalWrite(AND2, LOW); // turn on pullup resistors and2 = digitalRead(AND2); digitalWrite(OR1, LOW); // turn on pullup resistors or1 = digitalRead(OR1); digitalWrite(OR2, LOW); // turn on pullup resistors or2 = digitalRead(OR2); digitalWrite(MYNOT, LOW); // turn on pullup resistors mynot = digitalRead(MYNOT); digitalWrite(NAND1, LOW); // turn on pullup resistors nand1 = digitalRead(NAND1); digitalWrite(NAND2, LOW); // turn on pullup resistors nand2 = digitalRead(NAND2); andout = and1 && and2; orout = or1 || or2; notout = not(mynot); nandout = not(nand1 && nand2); if ((millis() - mytime) > 5000) { Serial.print("AND="); Serial.print(int(andout)); Serial.print(", OR="); Serial.print(int(orout)); Serial.print(", NOT="); Serial.print(int(notout)); Serial.print(", NAND="); Serial.println(int(nandout)); Serial.println("INPUTS PINS: AND->2,3 OR->5,6 NOT->8 NAND->10,11"); Serial.println("OUTPUTS PINS TO LEDS: AND->4 OR->7 NOT->9 NAND->12"); mytime = millis(); } digitalWrite(ANDOUT, andout); digitalWrite(OROUT, orout); digitalWrite(NOTOUT, notout); digitalWrite(NANDOUT, nandout); } void mymessage() { Serial.println("By: ROEE BLOCH --> www.electronics-freak.com LOGIC-GATE V1.0"); Serial.println("This SW will teach you how LOGIC GATE ARE WORKING"); Serial.println("The GATES Are: AND, OR, NOT, NAND"); Serial.println("Please connect INPUT PINS to wires, AND EACH of OUTPUTs to: "); Serial.println("Psitive LED, from Negative LED to 330 ohms resistor, and from Resistor to GND"); Serial.println("Like this: OUTPUT-----LED(+), LED(-)----330ohm(RESISTOR)-----GND"); Serial.println("When INPUT wire is NOT CONNECTED (in AIR) OR CONNECTED TO GND - IT WILL BE CONSIDERED AS '0'"); Serial.println("NOTE: WHEN IN AIR RESULT MIGHT BE WRONG!! NOT RECOMMENDED TO LEAVE INPUTS UNCONNECTED!"); Serial.println("When INPUT wire is CONNECTED TO 5V or 3.3V - IT WILL BE CONSIDERED AS '1' "); Serial.println("THE OUTPUT LED will show LOGIC RESULTS"); Serial.println("Results will be sampled and Showed on SCREEN each 5 SECONDS"); Serial.println("However on LEDS RSULTS will be changed IMMEDIATELY!!!!!"); Serial.println("goto Menu->TOOLs->Seiral Monitor"); Serial.println("On the LOWER RIGHT SIDE Choose 57600 baud"); }
/*
This Program is Educational to learn about Logic Gates: AND, OR, NOT and NAND (other will be covered later)
It works like this:
You connect wires to inputs of Arduino, and LEDS to OUTPUTS
For AND 2,3 are inputs 4 is output
For OR 5,6 are inputs 7 is output
For NOT 8 input and 9 is output
For NAND 10,11 inputs and 12 is output
To all outputs connect LEDS as described (HELP is on screen when running) – RUN
Menu->TOOLs->Seiral Monitor , choose on bottom right Baud 57,600
By YOUR inputs the LED’s Will Be changed according to LOGIC FUNCTION of gate
Also on screen you will see results (ZERO or ONE LOGIC)
A VIDEO will be sent for This one Later
*/
[adsenseyu6]
|