I am trying the following code to read from the ArduinoPreformatted text digital pin 2 for my frequency counter, but it doesn't display the data properly on my 4 digit 7 segments. These are connected in multiplexing.
Only 1 digit is being displayed and the rest keep on flickering.
// Declare the pins for the segments:
int a = 3;
int b = 4;
int c = 5;
int d = 6;
int e = 7;
int f = 8;
int g = 9;
int dp = 10;
// Declare the pins for the digits:
int d1 = A3;
int d2 = A2;
int d3 = A1;
int d4 = A0;
// Control variables:
int refRate = 10; //Controls the refresh rate of the display
int rCount;
int freqCounter = 0;
long preMillis = 0;
void isr() //interrupt service routine
{
freqCounter++;
}
void setup()
{
// Set all the pins of the LEDs to output:
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(dp, OUTPUT);
pinMode(d1, OUTPUT);
pinMode(d2, OUTPUT);
pinMode(d3, OUTPUT);
pinMode(d4, OUTPUT);
displayNumber();
attachInterrupt(0, isr, RISING);
}
void loop()
{
while((millis()-preMillis)<1000);
detachInterrupt(0); //detaches the interrupt
displayNumber();
freqCounter = 0;
preMillis = millis();
attachInterrupt(0,isr,RISING); //attaching the interrupt again
}
void displayNumber()
{
pickDigit(3);
pickNumber(freqCounter / 1000);
delay(5);
pickDigit(2);
pickNumber((freqCounter % 1000)/100);
delay(5);
pickDigit(1);
pickNumber((freqCounter % 100)/10);
delay(5);
pickDigit(0);
pickNumber(freqCounter % 10);
delay(5);
}
// Function to select the LED
void pickDigit(int x)
{
// Initially turn all four LEDs off:
digitalWrite(d4, LOW);
digitalWrite(d3, LOW);
digitalWrite(d2, LOW);
digitalWrite(d1, LOW);
switch(x)
{
case 0:
digitalWrite(d4, HIGH);
digitalWrite(d3,LOW);
digitalWrite(d2,LOW);
digitalWrite(d1,LOW);
break;
case 1:
digitalWrite(d3, HIGH);
digitalWrite(d2,LOW);
digitalWrite(d1,LOW);
digitalWrite(d4,LOW);
break;
case 2:
digitalWrite(d2, HIGH);
digitalWrite(d3,LOW);
digitalWrite(d4,LOW);
digitalWrite(d1,LOW);
break;
default:
digitalWrite(d1, HIGH);
digitalWrite(d3,LOW);
digitalWrite(d2,LOW);
digitalWrite(d4,LOW);
break;
}
}
// Function to control number displayed on each LED. Here x is the number to be displayed:
void pickNumber(int x)
{
switch(x)
{
default:
zero();
break;
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
case 7:
seven();
break;
case 8:
eight();
break;
case 9:
nine();
break;
}
}
// Function to clear the LEDs:
void clearLEDs()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 0:
void zero()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,!HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 1:
void one()
{
digitalWrite(a,HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 2:
void two()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,!HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 3:
void three()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 4:
void four()
{
digitalWrite(a,HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 5:
void five()
{
digitalWrite(a,!HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 6:
void six()
{
digitalWrite(a,!HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,!HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 7:
void seven()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
// Function to display 8:
void eight()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,!HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}
// Function to display 9:
void nine()
{
digitalWrite(a,!HIGH);
digitalWrite(b,!HIGH);
digitalWrite(c,!HIGH);
digitalWrite(d,!HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,!HIGH);
digitalWrite(g,!HIGH);
digitalWrite(dp,HIGH);
}