1

I try to create a display so that when I press the button, the counter is increased by one. The problem is only one of them light up and it is not showing the correct output, it is showing the combination of both digit (e.g. 01 showing 0, 02 showing 8). Here is my full code.

const int a = 8;  //For displaying segment "a"
const int b = 9;  //For displaying segment "b"
const int c = 4;  //For displaying segment "c"
const int d = 5;  //For displaying segment "d"
const int e = 6;  //For displaying segment "e"
const int f = 2;  //For displaying segment "f"
const int g = 3;  //For displaying segment "g"
const int D1 = 7;
const int D2 = 13;

bool bPress = false;
const int IncbuttonPin = 10;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonPushCounterD1 = 0;
int buttonPushCounterD2 = 0;

int IncbuttonState = 0;         
int lastIncbuttonState = 0;     

void setup() {
  // put your setup code here, to run once:
  pinMode(a, OUTPUT);  //A
  pinMode(b, OUTPUT);  //B
  pinMode(c, OUTPUT);  //C
  pinMode(d, OUTPUT);  //D
  pinMode(e, OUTPUT);  //E
  pinMode(f, OUTPUT);  //F
  pinMode(g, OUTPUT);  //G
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);

  pinMode( IncbuttonPin , INPUT_PULLUP );
  Serial.begin(9600);

}

void loop() {

   IncbuttonState = digitalRead(IncbuttonPin);

   checkIncButtonPress();


  if( bPress ){
    bPress = false;
     turnOff();
     lightDigit1(buttonPushCounterD1);
     delay(5);
     lightDigit2(buttonPushCounterD2);
     delay(5);
  }
}

void checkIncButtonPress()
{
   // compare the IncbuttonState to its previous state
  if (IncbuttonState != lastIncbuttonState) {
    // if the state has changed, increment the counter
    if (IncbuttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      buttonPushCounter++;
      buttonPushCounterD1 = buttonPushCounter / 10;
      buttonPushCounterD2 = buttonPushCounter % 10; 
      Serial.println("on");
      Serial.println(buttonPushCounterD1);
      Serial.println(buttonPushCounterD2);

    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastIncbuttonState = IncbuttonState;

}

void lightDigit1(byte digit) {
  digitalWrite(D1, LOW);
  digitalWrite(D2, HIGH);
  displayDigit1(digit);
}

void lightDigit2(byte digit) {
  digitalWrite(D1, HIGH);
  digitalWrite(D2, LOW);
  displayDigit2(digit);
}

void displayDigit1(byte digit)
{
 //Conditions for displaying segment a
 if(digit!=1 && digit != 4)
 digitalWrite(a,HIGH);

 //Conditions for displaying segment b
 if(digit != 5 && digit != 6)
 digitalWrite(b,HIGH);

 //Conditions for displaying segment c
 if(digit !=2)
 digitalWrite(c,HIGH);

 //Conditions for displaying segment d
 if(digit != 1 && digit !=4 && digit !=7)
 digitalWrite(d,HIGH);

 //Conditions for displaying segment e 
 if(digit == 2 || digit ==6 || digit == 8 || digit==0)
 digitalWrite(e,HIGH);

 //Conditions for displaying segment f
 if(digit != 1 && digit !=2 && digit!=3 && digit !=7)
 digitalWrite(f,HIGH);
 if (digit!=0 && digit!=1 && digit !=7)
 digitalWrite(g,HIGH);
}


void displayDigit2(byte digit)
{
// digitalWrite(D1, HIGH);
// digitalWrite(D2, LOW);
 //Conditions for displaying segment a
 if(digit!=1 && digit != 4)
 digitalWrite(a,HIGH);

 //Conditions for displaying segment b
 if(digit != 5 && digit != 6)
 digitalWrite(b,HIGH);

 //Conditions for displaying segment c
 if(digit !=2)
 digitalWrite(c,HIGH);

 //Conditions for displaying segment d
 if(digit != 1 && digit !=4 && digit !=7)
 digitalWrite(d,HIGH);

 //Conditions for displaying segment e 
 if(digit == 2 || digit ==6 || digit == 8 || digit==0)
 digitalWrite(e,HIGH);

 //Conditions for displaying segment f
 if(digit != 1 && digit !=2 && digit!=3 && digit !=7)
 digitalWrite(f,HIGH);
 if (digit!=0 && digit!=1 && digit !=7)
 digitalWrite(g,HIGH);
}


void turnOff()
{
  digitalWrite(a,LOW);
  digitalWrite(b,LOW);
  digitalWrite(c,LOW);
  digitalWrite(d,LOW);
  digitalWrite(e,LOW);
  digitalWrite(f,LOW);
  digitalWrite(g,LOW);
}

when I remove the code

     lightDigit1(buttonPushCounterD1);
     delay(5);

It is showing the correct output, but only for 1 digit. Anyone can help me on this? Thanks.

1

You aren't multiplexing your displays properly.

At the moment you are doing:

  1. Do nothing until the button is pressed
  2. Switch on display 1
  3. Turn on the segments for digit 1
  4. Wait 5ms
  5. Switch off display 1
  6. Switch on display 2
  7. Turn on the segments for digit 2
  8. Wait 5ms
  9. Go to 1

For about 5ms you will get a brief flash on one digit, but since you don't turn off the segments you turned on for that digit they are just added to for the second digit - which you display for an extended period.

To multiplex properly you must:

  • Turn off segments that are no longer used for the current display (best to turn them all off)
  • Repeatedly display both digits all the time.

Ideally your method would be:

  1. Turn off all segments
  2. Turn on segments for digit 1
  3. Turn on digit 1
  4. Delay 5ms
  5. Turn off digit 1
  6. Turn off all segments
  7. Turn on segments for digit 2
  8. Turn on digit 2
  9. Delay 5ms
  10. Turn off digit 2
  11. Go to 1 (usually the loop() handles that for you).

And separate to that you handle your button press and changing the value to display.

Also note the turning off of a digit before arranging the segments for the next digit. This prevents ghosting where you get a faint impression of the previous digit overlaid on the current digit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.