2 digit 7 segment display with buttons

Hey guys,

I'm having a problem with my code...

I want the display to go up when a button is pressed or go down when the other button is pressed and stay that way. Unfortunately for some reason when the let any of the buttons go, it goes back to it's original value. Any ideas? Also, any ideas of how to optimize this code?

#define A A0
#define B A1
#define C A2
#define D A3
#define E A4
#define pinF A5
#define G 3

#define BTN1 10
#define BTN2 9

#define CA1 12
#define CA2 11

const int segs[7] = {A, B, C, D, E, pinF, G};
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };

int digit=0;
int digit1=0;
int digit2=0;

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BTN1, INPUT);
  pinMode(BTN2, INPUT);
  pinMode(CA1, OUTPUT);
  pinMode(CA2, OUTPUT);
}

void loop() {

  digit=15;
  
  if (digitalRead(BTN1)==HIGH){
    digit++;
  }
  if (digitalRead(BTN2)==HIGH){
    digit--;
  }
  
  digit1=(digit/10)%10;
  digit2=digit%10;
  
  // display number
  unsigned long startTime = millis();
  for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() - startTime) {
    lightDigit1(numbers[digit1]);
    delay(5);
    lightDigit2(numbers[digit2]);
    delay(5);
  }
}

void lightDigit1(byte number) {
  digitalWrite(CA1, LOW);
  digitalWrite(CA2, HIGH);
  lightSegments(number);
}

void lightDigit2(byte number) {
  digitalWrite(CA1, HIGH);
  digitalWrite(CA2, LOW);
  lightSegments(number);
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
  }
}

johnnyshepard:
for some reason when the let any of the buttons go, it goes back to it's original value. Any ideas?

By "original value", do you mean 15?

johnnyshepard:

void loop() 

{
  digit=15;  //  <<< here's the "some reason"
            // it always will come back here, won't it?
 
  if (digitalRead(BTN1)==HIGH){
    digit++;
  }
  if (digitalRead(BTN2)==HIGH){
    digit--;
  }

// ...

Wow, that's blatantly obvious, I can't believe I missed that. Thanks a bunch lol.

Now for the optimization part. Anything I can do differently that would be easier, faster, use less power, etc. etc.?

I would guess you now have 2 or 3 little "bugs" to iron out:

  1. The displays are a bit dim

  2. You may see some segments lit very dimly that are not supposed to be on at all

  3. Its not easy to set a particular number using the buttons because they change so fast

Did I guess right?

  1. The displays seem fine?

  2. I do kind of see segments that are dimly lit but I don't know how I would change that.

  3. The numbers don't change that fast at all, the buttons I have are a little annoying to use though.

Hi,

  1. Sorry, I read your code wrong first time. I can see now it will be ok brightness-wise.

  2. You're getting "bleed" from one digit to the other because of the sequence you're changing the anodes & cathodes. Turn both cathodes off before setting the segments up, then turn the appropriate cathode on.

  3. Another problem imaged by me due to misinterpreting your code. However, the buttons are only getting checked every 600ms I think. If you changed the code to improve that, you might need to (A) check for "leading edges" of button presses and (B) take care to avoid "button bounce".

Sorry again for imagining some problems that weren't acutually there!

Paul