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 pressesint 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.

askedOct 7, 2018 at 15:44
user50125's user avatar

1 Answer1

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 themall off)
  • Repeatedly displayboth digitsall 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 theloop() handles that for you).

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

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

answeredOct 7, 2018 at 16:19
Majenko's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.