7-segment display has a stroke when it hits a number higher than 9

December 10, 2022, 13:44

illegitimate_egg

Here's the code that the arduino is running, I wrote it myself:
arduino
#define latchPin 5
#define clockPin 6
#define dataPin 4
#define outputEnable 3
#define disp1 7
#define disp2 8
#define disp3 9
#define disp4 10

char *seg = (char []) {B11111100, B01100000, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110};

byte leds = 0;
int num = 0;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode(outputEnable, OUTPUT);
  pinMode(disp1, OUTPUT);
  digitalWrite(disp1, HIGH);
  pinMode(disp2, OUTPUT);
  digitalWrite(disp2, HIGH);
  pinMode(disp3, OUTPUT);
  digitalWrite(disp3, HIGH);
  pinMode(disp4, OUTPUT);
  digitalWrite(disp4, HIGH);

  cli();
  
  TCCR1A = 0;
  TCCR1B = 0;
  
  TCCR1B |= B00000100;
  TIMSK1 |= B00000010;

  OCR1A = 31250;
  sei();
}

ISR(TIMER1_COMPA_vect) {
  TCNT1 = 0;
  num++;
}

void loop() {
    writeDisplay(num);
}

void writeDisplay(int x) {
  for (int i = 0; i < 4; i++) {
    switch (i) {
      case 0:
        leds = seg[x & 0xFF];
        updateShiftRegister();
        digitalWrite(disp1, LOW);
        delay(1);
        digitalWrite(disp1, HIGH);
        break;
      case 1:
        leds = seg[(x >> 8) & 0xFF];
        updateShiftRegister();
        digitalWrite(disp2, LOW);
        delay(1);
        digitalWrite(disp2, HIGH);
        break;
      case 2:
        leds = seg[(x >> 16) & 0xFF];
        updateShiftRegister();
        digitalWrite(disp3, LOW);
        delay(1);
        digitalWrite(disp3, HIGH);
        break;
      case 3:
        leds = seg[(x >> 24) & 0xFF];
        updateShiftRegister();
        digitalWrite(disp4, LOW);
        delay(1);
        digitalWrite(disp4, HIGH);
        break;  
    }
  }
}

void updateShiftRegister() {
  digitalWrite(outputEnable, HIGH);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
  digitalWrite(outputEnable, LOW);
}

illegitimate_egg

Any ideas?

illegitimate_egg

I think the problem is to do with the way that I'm breaking down the numbers

illegitimate_egg

it 100% is

illegitimate_egg

I'm treating it like a string

illegitimate_egg

when it's not

illegitimate_egg

So I'm shifting off random bits

illegitimate_egg

it all makes sense now

illegitimate_egg

And the reason for the sporadic numbers is because the array of numbers isn't memory safe

illegitimate_egg

So the real question is now how do I convert from base 2 to base 10

illegitimate_egg

or, how do I split base 2 into base 10 chunks

illegitimate_egg

I have a solution

illegitimate_egg

I can convert it to a string

illegitimate_egg

lop off the unnecessary bits

illegitimate_egg

then convert it back to an int

illegitimate_egg

it didn't work

illegitimate_egg

idk what to do now

illegitimate_egg

I'll have to write a custom implementation of base 10