Hello everyone!I have a hardware for a bcd to 7 segment.I am trying to make the common cathode count from 0 to 9 and the common anode from 9 to 0 using two switches for each of them.They dont count properly and display random numbers when it counts.
#define buzzer 5
#define LED1 6
#define LED2 7
#define SW1 8
#define SW2 9
#define Freq 2
const int E3= 3 ;
const int AA0= 4;
const int D0= 10;
const int D1 =11 ;
const int D2= 12;
const int D3= 13 ;
int seg1=9;
int seg2=0;
void setup(){
pinMode(buzzer,OUTPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(SW1,INPUT);
pinMode(SW2,INPUT);
pinMode(AA0, OUTPUT);
pinMode(E3, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D0, OUTPUT);
digitalWrite(LED1,LOW);//active low because it turns it on
delay(250);
digitalWrite(LED1,HIGH);
tone(buzzer,440,250);//RED
delay(250);
digitalWrite(LED2,LOW);//active low because it turns it on
delay(250);
digitalWrite(LED2,HIGH);
tone(buzzer,294,250);//RED
delay(250);
}
void loop()
{
if(digitalRead(SW1)==HIGH){
while(digitalRead(SW1)==HIGH){}//while the button is pressed
for(int i=0;i<=seg1;i++){
disp1(i);
delay(1000);
}
//seg1++;
}
if(digitalRead(SW2)==HIGH){//Switch is active LOW
while(digitalRead(SW2)==HIGH){}//while the button is pressed
//seg2--;
for(int a=9;a>=seg2;a--){
disp2(a);
delay(1000);
}
}
}
/*void disp(byte digit, byte num)
{
digitalWrite(D0, num & 1);
digitalWrite(D1, num & 2);
digitalWrite(D2, num & 4);
digitalWrite(D3, num & 8);
// Select which BCD to 7SEG chip to load
digitalWrite(AA0, digit & 1);
digitalWrite(E3, HIGH);
digitalWrite(E3, LOW);
}*/
void disp1(int num){ //this method entails making 0-9 combinations using the BCD pins by sending different outputs to turn each segment on or off.
digitalWrite(AA0,LOW);
digitalWrite(E3, HIGH);
if(num == 0)//0000
{
digitalWrite(D0, LOW);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
}
if(num == 1)//0001
{
digitalWrite(D0, HIGH);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
}
if(num == 2)//0010
{
digitalWrite(D0, LOW);//0
digitalWrite(D1, HIGH);//1
digitalWrite(D2, LOW);//0
digitalWrite(D3, LOW);//0
}
if(num == 3)//0011
{
digitalWrite(D0, HIGH);//1
digitalWrite(D1, HIGH);//1
digitalWrite(D2, LOW);//0
digitalWrite(D3, LOW);//0
}
if(num == 4)//0100
{
digitalWrite(D0, LOW);//0
digitalWrite(D1, LOW);//0
digitalWrite(D2, HIGH);//1
digitalWrite(D3, LOW);//0
}
if(num == 5)//0101
{
digitalWrite(D0, HIGH);//1
digitalWrite(D1, LOW);//0
digitalWrite(D2, HIGH);//1
digitalWrite(D3, LOW);//0
}
if(num == 6)//0110
{
digitalWrite(D0, LOW);//0
digitalWrite(D1, HIGH);//1
digitalWrite(D2, HIGH);//1
digitalWrite(D3, LOW);//0
}
if(num == 7) //0111
{
digitalWrite(D0, HIGH);//1
digitalWrite(D1, HIGH);//1
digitalWrite(D2, HIGH);//1
digitalWrite(D3, LOW);//0
}
if(num == 8) //1000
{
digitalWrite(D0, LOW);//0
digitalWrite(D1, LOW);//0
digitalWrite(D2, LOW);//0
digitalWrite(D3, HIGH);//1
}
if(num == 9)//1001
{
digitalWrite(D0, HIGH);//1
digitalWrite(D1, LOW);//0
digitalWrite(D2, LOW);//0
digitalWrite(D3, HIGH);//1
}
}
void disp2(int num){ //this method entails making 0-9 combinations using the BCD pins by sending different outputs to turn each segment on or off.
digitalWrite(AA0,HIGH);
digitalWrite(E3, HIGH);
if(num == 0)//0000
{
digitalWrite(D0, LOW);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
}
if(num == 1)//0001
{
digitalWrite(D0, HIGH);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
}
if(num == 2)//0010
{
digitalWrite(D0, LOW);//0
digitalWrite(D1, HIGH);//1
digitalWrite(D2, LOW);//0
digitalWrite(D3, LOW);//0
}
if(num == 3)//0011
{
digitalWrite(D0, HIGH);//1
digitalWrite(D1, HIGH);//1
digitalWrite(D2, LOW);//0
digitalWrite(D3, LOW);//0
}
if(num == 4)//0100
{
digitalWrite(D0, LOW);//0
digitalWrite(D1, LOW);//0
digitalWrite(D2, HIGH);//1
digitalWrite(D3, LOW);//0
}
if(num == 5)//0101
{
digitalWrite(D0, HIGH);//1
digitalWrite(D1, LOW);//0
digitalWrite(D2, HIGH);//1
digitalWrite(D3, LOW);//0
}
if(num == 6)//0110
{
digitalWrite(D0, LOW);//0
digitalWrite(D1, HIGH);//1
digitalWrite(D2, HIGH);//1
digitalWrite(D3, LOW);//0
}
if(num == 7) //0111
{
digitalWrite(D0, HIGH);//1
digitalWrite(D1, HIGH);//1
digitalWrite(D2, HIGH);//1
digitalWrite(D3, LOW);//0
}
if(num == 8) //1000
{
digitalWrite(D0, LOW);//0
digitalWrite(D1, LOW);//0
digitalWrite(D2, LOW);//0
digitalWrite(D3, HIGH);//1
}
if(num == 9)//1001
{
digitalWrite(D0, HIGH);//1
digitalWrite(D1, LOW);//0
digitalWrite(D2, LOW);//0
digitalWrite(D3, HIGH);//1
}
}