hello guys is there someone who can help me with my first project. its basically about logic circuits.
by using tact switch and pull-up resistor to set the values to logical 1 or 0. the output can be verified by using a LED as an indicator. so the code goes like this:
//AND GATE
int pin2=1; //set pin2=1
int pin3=1; //set pin3=1
void setup()
{
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop()
{
pin2=digitalRead(2);
pin3=digitalRead(3);
if(pin2==1) //turn led on pin11 when pin2 =1
{
digitalWrite(11,HIGH);
}
else
{
digitalWrite(11,LOW);
}
if(pin3==1) ////turn led on pin12 when pin3 =1
{
digitalWrite(12,HIGH);
}
else
{
digitalWrite(12,LOW);
}
//TRUTH TABLE OF AND
if(pin2 && pin3)
{
digitalWrite(4,HIGH);
}
else
{
digitalWrite(4,LOW);
}
}
//OR GATE
int pin2=1;
int pin3=1;
void setup()
{
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop()
{
pin2=digitalRead(2);
pin3=digitalRead(3);
if(pin2==1)
{
digitalWrite(11,HIGH);
}
else
{
digitalWrite(11,LOW);
}
if(pin3==1)
{
digitalWrite(12,HIGH);
}
else
{
digitalWrite(12,LOW);
}
//TRUTH TABLE OF OR
if(pin2 || pin3)
{
digitalWrite(4,HIGH);
}
else
{
digitalWrite(4,LOW);
}
}
//NAND GATE
int pin2=1;
int pin3=1;
void setup()
{
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop()
{
pin2=digitalRead(2);
pin3=digitalRead(3);
if(pin2==1)
{
digitalWrite(11,HIGH);
}
else
{
digitalWrite(11,LOW);
}
if(pin3==1)
{
digitalWrite(12,HIGH);
}
else
{
digitalWrite(12,LOW);
}
//TRUTH TABLE OF NAND
if(!(pin2 && pin3))
{
digitalWrite(4,HIGH);
}
else
{
digitalWrite(4,LOW);
}
}
can you guys help me to run this in one sketch by using tact switch to select the logic gate to be tested or run? thank you in advance
pin2
,pin3
,setup()
andloop()
are all defined three times. You cannot paste three programs one after the other and expect the whole to be a valid program. Just run one program at a time. If you have no push button, use a wire that you manually connect and disconnect to/from GND (if using a pullup resistor) or 5V (if using a pulldown).