displaying digits on a 4 x 7 segment LED display

I have the following sketch which if i change the buffer values form num 1, num2, num3, num4 to a number it displays them but what i want to do is to have a 4 digit number broken down and displayed in the correct segments.

#include <Wire.h>
 
void setup()
{
  Wire.begin();
}
 
void TwiLeDispIntAndPts(uint8_t address, uint8_t upLed, uint8_t loLed, uint8_t bright_rb, uint8_t bright_7s, int16_t number, uint8_t points)
{
  int num = 4321; // test number to show on the display
  int num1 = (num/1000); // pulls the 4 from the number
  int num2 = ((num - num1) / 100); // pulls the 3 from the number
  int num3 = ((num-num1 - num2) / 10); // pulls the 2 from the number
  int num4 = (num - num1 - num2 - num3); // pulls the 1 from the number
  uint8_t i;
  uint8_t Buffer[8];
  Buffer[0] = ((1 & upLed) << 7) | ((1 & loLed) << 6) | (1 << 5) | (bright_7s & 31); // no idea what this sentance does
  Buffer[1] = bright_rb & 127; // changes to 127 dont do anything
  //snprintf(&Buffer[2], 5, "%d", number); // if this line will not work, try to replace it with these:
  Buffer[2] = num1; // this number is displayed on the first section
  Buffer[3] = num2; // this number is displayed on the second section
  Buffer[4] = num3; // this number is displayed on the third section
  Buffer[5] = num4; // this number is displayed on the fourth section
  Buffer[6] = 0b1111 & points; // no idea what this sentance does
  Buffer[7] = address << 1; 
  for (i = 0; i <= 6; i++) Buffer[7] ^= Buffer[i];
  Wire.beginTransmission(address);
  // try this if above will not work: Wire.beginTransmission(address << 1);
  Wire.write(&(Buffer[0]), 8);
  Wire.endTransmission();
}
 
void loop()
{
 // int num = -987;
  TwiLeDispIntAndPts(3, 0, 0, 5, 20, 2, 0b00000000); // (address, top led on/off, bottom led on/off, brightness, ? , last 4 on/off decimal points)
  //num++;
  delay(1000);
}

it shows the following at the moment 4 blank blank 7

it shows the following at the moment 4 blank blank 7

Have you tried debugging this yourself? Where are the Serial.print() statements that show the values in num1, num2, num3, and num4?

  int num = 4321; // test number to show on the display
  int num1 = (num/1000); // pulls the 4 from the number

This looks right.

  int num2 = ((num - num1) / 100); // pulls the 3 from the number

This looks like rubbish. 4321 - 4 = 4317. 4317 / 100 = 43. 43 != 3.

num2 should be (num - (num1*1000)) / 100.

Thanks for the reply.

You are correct that my math was wrong - to long fiddeling with the code and missed the obvoius.

Another try tonight with the correct calculations and see where i end up.

Math error corrected and now the LED display counts up nicely.

#include <Wire.h>

int num=0000; 
void setup()
{
  Wire.begin();
}
 
void TwiLeDispIntAndPts(uint8_t address, uint8_t upLed, uint8_t loLed, uint8_t bright_rb, uint8_t bright_7s, int16_t number, uint8_t points)
{
//int num = 1234; // test number to show on the display
  int num1 = (num/1000); // pulls the 4 from the number
  int num2 = ((num - (num1*1000)) / 100); // pulls the 3 from the number
  int num3 = ((num-(num1*1000)-(num2*100))/10); // pulls the 2 from the number
  int num4 = ((num-(num1*1000)-(num2*100)-(num3*10))); // pulls the 1 from the number
  uint8_t i;
  uint8_t Buffer[8];
  Buffer[0] = ((1 & upLed) << 7) | ((1 & loLed) << 6) | (1 << 5) | (bright_7s & 31); // no idea what this sentance does
  Buffer[1] = bright_rb & 127; // changes to 127 dont do anything
  //snprintf(&Buffer[2], 5, "%d", number); // if this line will not work, try to replace it with these:
  Buffer[2] = num1; // this number is displayed on the first section
  Buffer[3] = num2; // this number is displayed on the second section
  Buffer[4] = num3; // this number is displayed on the third section
  Buffer[5] = num4; // this number is displayed on the fourth section
  Buffer[6] = 0b1111 & points; // no idea what this sentance does
  Buffer[7] = address << 1; 
  for (i = 0; i <= 6; i++) Buffer[7] ^= Buffer[i];
  Wire.beginTransmission(address);
  // try this if above will not work: Wire.beginTransmission(address << 1);
  Wire.write(&(Buffer[0]), 8);
  Wire.endTransmission();
}
 
void loop()
{
 //int num = -987;
  TwiLeDispIntAndPts(3, 0, 0, 5, 20, 2, 0b00000000); // (address, top led on/off, bottom led on/off, brightness, ? , last 4 on/off decimal points)
 num=num+1;
  delay(100);

}

If you don't need to preserve the value in num, the code can be a lot simpler.

int num1 = num/1000;
num -= num1 * 1000;
int num2 = num/100;
num -= num2 * 100;
int num3 = num/10;
num -= num3 * 10;
int num4 = num;

If you do need to preserve the value in num, make a copy, and operate on the copy.