my truth table for this circuit:
ABX|AB
000 00
001 01
010 01
011 10
100 10
101 11
110 11
111 00
my code:
//x=pin 8 and clk=pin 9, A=pin 6 and B=pin 7 clk=pin 2
int a,b,c,d,e,f,g,h,i,j,k,x;
int A,B;
int clk;
void setup(){
A=0;
B=0;
pinMode(6,OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
//delay(3000);
// attachInterrupt(0,clock,RISING);
}
void loop(){
x=digitalRead(8);
// clk=digitalRead(9);
a=NAND(b,c);
b=NOR(d,d);
c=NAND(x,g);
d=NAND(e,f);
e=NAND(A,h);
f=NAND(A,!B);
g=NAND(A,!B);
h=NOR(x,x);
i=NAND(h,B);
j=NAND(x,!B);
k=NAND(i,j);
int lasttemp =0;
int temp = digitalRead(9); // gets the clock pulse
delay(50);
if ( (temp != lasttemp) && (temp == 1) && clk!=1) {
A=dff(a,clk,A);
B=dff(k,clk,B);
clk =1;
}
else if (digitalRead(9)==0)clk = 0;
lasttemp=temp;
if(clk){
}
digitalWrite(6,A);
digitalWrite(7,B);
}
int NAND(int x, int y){
if(x && y)
return(0);
else return(1);
}
int NOR(int x, int y){
if(!x && !y)
return(1);
else return(0);
}
int dff(int d, int clk, int memory){
if (clk==1)
return(d);
else return(memory);
}
void clock(){
//int a,b,c,d,e,f,g,h,i,j,k,x;
//clk=digitalRead(9);
// x=digitalRead(8);
/*
int temp = digitalRead(9); // gets the clock pulse
delay(50);
if ( (temp != lasttemp) && (temp == 1) ) clk = 1;
else clk = 0;
lasttemp=temp;
*/
}
you can probably see that I am attempting to fix the button bounce. I always have problems with my code in the A output especially the first 3 rows of the truth table. My question is can you fix my code to do what I want it to. I have played with the idea of button debounce and attachInterupt as well. I am at a loss, also I have been thinking that there is a problem with the rising edge of my switches, i don't understand how to fix this