Help me set up an Array please.

To start, here is my code:

//set integers for switches
const int S1 = A3;
const int S2 = A2;
const int S3 = A1;
const int S4 = A0;
//set integers for 7 segment display
const int SegA = 11;
const int SegB = 10;
const int SegC = 9;
const int SegD = 8;
const int SegE = 5;
const int SegF = 4;
const int SegG = 3;
const int SegDP = 2; //decimal point
const int SegCOL = 1; //colon, ":"
//set integers for digits.(i.e D1 is equal to digit 1 and also T1, transistor 1, on board.
const int D1 = 13;
const int D2 = 12;
const int D3 = 7;
const int D4 = 6;
//track swith read value
int buttonState1 = HIGH;
int buttonState2 = HIGH;
int buttonState3 = HIGH;
int buttonState4 = HIGH;
int counterD1 = 0;
int counterD2 = 0;
int counterD3 = 0;
int counterD4 = 0;




void (*DigitFunctions[10])( int );

/*
void turnOffAllExcept(int targetDigit)
{
  byte digitPin[] = {
    13, 12, 7, 6
  };

  byte selectedDigit = targetDigit; // any of 0 .. 3

  for (byte n = 0; n < 4; n++) {

    digitalWrite(digitPin[n], LOW); // all off
  }
  digitalWrite(digitPin[selectedDigit], HIGH);
}



void turnOnNumberForDigit(int value, int targetDigit)
{
  void (*turnOnNumber)(int targetDigit);
  turnOnNumber = DigitFunctions[value];//get function N# where # == value
  turnOnNumber(targetDigit);
}

*/
bool didPressButtonDown(int button, int buttonState)
{
  int buttonStatus = digitalRead(button);
  if (buttonStatus == LOW &&  buttonStatus != buttonState) {
    buttonState = LOW;
    return true;
  }
  else if (buttonStatus == HIGH && buttonStatus != buttonState) {
    buttonState = HIGH;
  }
  return false;
}

//N1
void turnOnNumber1(int targetDigit) {
  digitalWrite(SegA, HIGH);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, HIGH);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, HIGH);
  digitalWrite(SegG, HIGH);
  digitalWrite(SegDP, HIGH);
  //turnOffAllExcept(targetDigit);

}

//N2
void turnOnNumber2(int targetDigit) {
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, HIGH);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, LOW);
  digitalWrite(SegF, HIGH);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
//  turnOffAllExcept(targetDigit);
}

void turnOnNumber3(int targetDigit) {
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, HIGH);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  //turnOffAllExcept(targetDigit);
}

void turnOnNumber4(int targetDigit) {
  digitalWrite(SegA, HIGH);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, HIGH);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  //turnOffAllExcept(targetDigit);
}

void turnOnNumber5(int targetDigit) {
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, HIGH);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  //turnOffAllExcept(targetDigit);
}

void turnOnNumber6(int targetDigit) {
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, HIGH);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, LOW);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  //turnOffAllExcept(targetDigit);
}

void turnOnNumber7(int targetDigit) {
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, HIGH);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, HIGH);
  digitalWrite(SegG, HIGH);
  digitalWrite(SegDP, HIGH);
  //turnOffAllExcept(targetDigit);
}

void turnOnNumber8(int targetDigit) {
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, LOW);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  //turnOffAllExcept(targetDigit);
}
void turnOnNumber9(int targetDigit) {
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, HIGH);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  //turnOffAllExcept(targetDigit);
}

void turnOnNumber0(int targetDigit) {
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, LOW);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, HIGH);
  digitalWrite(SegDP, HIGH);
 // turnOffAllExcept(targetDigit);
}




void setup() {
  //set switches as inputs with internal pullup resistor
  pinMode(S1, INPUT_PULLUP);
  pinMode(S2, INPUT_PULLUP);
  pinMode(S3, INPUT_PULLUP);
  pinMode(S4, INPUT_PULLUP);
  pinMode(SegA, OUTPUT);
  pinMode(SegB, OUTPUT);
  pinMode(SegC, OUTPUT);
  pinMode(SegD, OUTPUT);
  pinMode(SegE, OUTPUT);
  pinMode(SegF, OUTPUT);
  pinMode(SegG, OUTPUT);
  pinMode(SegDP, OUTPUT);
  pinMode(SegCOL, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);

  DigitFunctions[0] = &turnOnNumber0;
  DigitFunctions[1] = &turnOnNumber1;
  DigitFunctions[2] = &turnOnNumber2;
  DigitFunctions[3] = &turnOnNumber3;
  DigitFunctions[4] = &turnOnNumber4;
  DigitFunctions[5] = &turnOnNumber5;
  DigitFunctions[6] = &turnOnNumber6;
  DigitFunctions[7] = &turnOnNumber7;
  DigitFunctions[8] = &turnOnNumber8;
  DigitFunctions[9] = &turnOnNumber9;



  //Serial.begin(9600);
}




void loop() {





  //Keeps colon sign on always
  digitalWrite(SegCOL, LOW);

  if (didPressButtonDown(S1, buttonState1)) {
    ++counterD1;
    if (counterD1 == 9) {
      counterD1 = -1;// it will be incremented to 0 on the next line
    }
   
    //digitalWrite(D1,HIGH);
    delay(5);

  }

  if (didPressButtonDown(S2, buttonState2)) {
    if (counterD2 == 9) {
      counterD2 = -1;// it will be incremented to 0 on the next line
    }
    ++counterD2;
    //digitalWrite(D2,HIGH);
    delay(5);

  }
  if (didPressButtonDown(S3, buttonState3)) {
    if (counterD3 == 9) {
      counterD3 = -1;// it will be incremented to 0 on the next line
    }
    ++counterD3;
    //digitalWrite(D3,HIGH);
    delay(5);
  }
  if (didPressButtonDown(S4, buttonState4)) {
    if (counterD4 == 9) {
      counterD4 = -1;// it will be incremented to 0 on the next line
    }
    ++counterD4;
    //digitalWrite(D4,HIGH);
    delay(5);
  }
  turnOnNumber1(D1);
  digitalWrite(D1, HIGH);
  delay(1);
  digitalWrite(D1, LOW);
  turnOnNumber2(D2);
  digitalWrite(D2, HIGH);
  delay(1);
  digitalWrite(D2, LOW);
  turnOnNumber3(D3);
  digitalWrite(D3, HIGH);
  delay(1);
  digitalWrite(D3, LOW);
  turnOnNumber4(D4);
  digitalWrite(D4, HIGH);
  delay(1);
  digitalWrite(D4, LOW);
  //Serial.println(counterD1);
  delay(5);
}

So the problem, I can’t get the multiplexing to work right on my 7 segment 4 digit LED. I think I have a solution though. At the very end of my code I added this:

  turnOnNumber1(D1);
  digitalWrite(D1, HIGH);
  delay(1);
  digitalWrite(D1, LOW);
  turnOnNumber2(D2);
  digitalWrite(D2, HIGH);
  delay(1);
  digitalWrite(D2, LOW);
  turnOnNumber3(D3);
  digitalWrite(D3, HIGH);
  delay(1);
  digitalWrite(D3, LOW);
  turnOnNumber4(D4);
  digitalWrite(D4, HIGH);
  delay(1);
  digitalWrite(D4, LOW);
  //Serial.println(counterD1);
  delay(5);

That will display 1 2 3 4 but I now need to change those numbers according the number of times I press a button. There is a separate button for each number. I have a counter system set up and I want to create an Array for those counters that will call my number functions.
The functions are:

turnOnNumber1(int target digit);
turnOnNumber2 (int target digit);
etc.

So instead of

turnOnNumber1(D1);
  digitalWrite(D1, HIGH);
  delay(1);
  digitalWrite(D1, LOW);

The turnOnNumber1(D1); will instead be something that calls to an Array and that array will reference one of my 10 number functions and that will be determined by where the counter is at.

This

digitalWrite(SegA, HIGH); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, HIGH); digitalWrite(SegE, HIGH); digitalWrite(SegF, HIGH); digitalWrite(SegG, HIGH); digitalWrite(SegDP, HIGH);

Looks just like this, B10011111, So an array of these will make your code much easier to work with.

const byte myDigits[10] = {
  B00000011, // 0
  B10011111, // 1

  .
  .
  .
  B00011001 // 9
};

All you need now is the bitRead() function and a FOR loop, and maybe an array of your displays pins along with the digitalWrite() function.

Great, thank you. I get the conversion to the bytes. I’m still pretty new to all this stuff. This would only be the second program I’ve ever written for myself.

So would bitRead look like:

bitRead(0,0);

That would read a number 0 or:

bitRead(1,0);

That would read a number 1

So if that’s is correct then a function would like:

void num1(){
bitRead(1,0);
}

Then the for loop?

for (myDigits[10]=0; myDigits<10; myDigits++{
digitalWrite(myDigits, HIGH);
}

Also, and most importantly as this was what my post is actually about, what about the Array for the display pins? I’ve called them D1-D4 but I don’t think you can use that in Arrays can you? So it would look like:

displayPins[4] = {13,12,7,6};

More like this.

const byte myPins[] = { SegA, SegB, SegC ... SegDP}; // to make things easier for you, just replace the Seg... with the pins themselves and add this at the top of your code.
const byte myDigits[10] = { /* I showed you this earlier */};

void DisplayDigit(byte D) // enter a 0, the displays shows a 0. Enter a 7, the display shows a 7
{
  for(byte i = 0; i < 8; i++)
  {
     digitalWrite(myPins[ i ], bitRead(myDigits[ D ], i));
  }
}

No need for Num1(), Num2() … Num9() when this here will do them all in one clean function.

I’m still not sure what to do with this code. All I have in my void loop is :

 //Keeps colon sign on always
  digitalWrite(SegCOL, LOW);

void DisplayDigit(byte 0);{ // enter a 0, the displays shows a 0. Enter a 7, the display shows a 7

  for(byte i = 0; i < 8; i++)
  {
     digitalWrite(myPins[ i ], bitRead(myDigits[ 0 ], i));
  }
}

That just gives me error:

Counter.ino: In function ‘void loop()’:
Counter.ino:80:24: error: expected ‘,’ or ‘…’ before numeric constant
Error compiling.

Yea, I walked right into that one. :confused:

It should be like so.

//set integers for switches
const byte Switches[] = {A3, A2, A1, A0};

//set integers for 7 segment display
//const byte myPins[] = { SegA, SegB, SegC, SegD, SegE, SegF, SegG, SegDP, SegCOL}; // to make things easier for you
const byte myPins[] =   {   11,   10,    9,    8,    5,    4,    3,     2,      1};

//set integers for digits.(i.e D1 is equal to digit 1 and also T1, transistor 1, on board.
const byte Digits[4] = {13, 12, 7, 6};

//track swith read value
byte buttonState[4] = {HIGH};
int counterD[4] = {0};

byte cycle = 0;

const byte myDigits[10] = {
  B00000011,
  B10011111,
  B00100101,
  B00001101,
  B10011001,
  B01001001,
  B01000001,
  B00011111,
  B00000001,
  B00011001
};

void DisplayDigit(byte D) // enter a 0, the displays shows a 0. Enter a 7, the display shows a 7
{
  if (D > 9) D = 9; // a variable of type byte, will rollover when it goes past 0 or 255

  for (byte i = 0; i < 8; i++)
  {
    digitalWrite(myPins[ i ], bitRead(myDigits[ D ], i));
  }
}

/*
void turnOffAllExcept(int targetDigit)
{
  byte digitPin[] = {
    13, 12, 7, 6
  };

  byte selectedDigit = targetDigit; // any of 0 .. 3

  for (byte n = 0; n < 4; n++) {

    digitalWrite(digitPin[n], LOW); // all off
  }
  digitalWrite(digitPin[selectedDigit], HIGH);
}



void turnOnNumberForDigit(int value, int targetDigit)
{
  void (*turnOnNumber)(int targetDigit);
  turnOnNumber = DigitFunctions[value];//get function N# where # == value
  turnOnNumber(targetDigit);
}

*/

bool didPressButtonDown(int button, int buttonState)
{
  int buttonStatus = digitalRead(button);
  if (buttonStatus == LOW &&  buttonStatus != buttonState) {
    buttonState = LOW;
    return true;
  }
  else if (buttonStatus == HIGH && buttonStatus != buttonState) {
    buttonState = HIGH;
  }
  return false;
}

void setup()
{
  //set switches as inputs with internal pullup resistor
  for (byte i = 0; i < 4; i++)
  {
    pinMode(Switches[i], INPUT_PULLUP);
    pinMode(Digits[i], OUTPUT);
  }

  for (byte j = 0; j < 9; j++)
  {
    pinMode(myPins[j], OUTPUT);
  }
  //Serial.begin(9600);
}

void loop()
{
  //Keeps colon sign on always
  digitalWrite(myPins[8], LOW); // SegCOL = myPins[8]

  if (didPressButtonDown(Switches[cycle], buttonState[cycle]))
  {
    ++counterD[cycle];
    if (counterD[cycle] == 9)
      counterD[cycle] = 0;// it will be incremented to 0 on the next line
  }
  if (cycle > 3) cycle = 0;

  //Im not sure what you are doing here
  /*
  turnOnNumber1(D1);
  digitalWrite(D1, HIGH);
  delay(1);
  digitalWrite(D1, LOW);
  turnOnNumber2(D2);
  digitalWrite(D2, HIGH);
  delay(1);
  digitalWrite(D2, LOW);
  turnOnNumber3(D3);
  digitalWrite(D3, HIGH);
  delay(1);
  digitalWrite(D3, LOW);
  turnOnNumber4(D4);
  digitalWrite(D4, HIGH);
  delay(1);
  digitalWrite(D4, LOW);
  //Serial.println(counterD1);
  delay(5);
  */
}

Thank you very much for continuing to help me. There are no compile errors anymore, but I'm not sure how to implement the function to actually display the numbers. As currently nothing happens.

void DisplayDigit(byte D) // enter a 0, the displays shows a 0. Enter a 7, the display shows a 7

So....

void DisplayDigit(byte 0); // enter a 0, the displays shows a 0. Enter a 7, the display shows a 7

entering a 0 there by placing that in my void loop should display a 0 but I just get more errors.

I also don't see how

if (didPressButtonDown(Switches[cycle], buttonState[cycle]))
  {
    ++counterD[cycle];
    if (counterD[cycle] == 9)
      counterD[cycle] = 0;// it will be incremented to 0 on the next line
  }
  if (cycle > 3) cycle = 0;

is going to keep track of when I do something like press button 1 three times, button 2 once, button 4 five times, etc.

Currently only the first button works.

//set integers for switches
const byte Switches[] = {A3, A2, A1, A0};

//set integers for 7 segment display
//const byte myPins[] = { SegA, SegB, SegC, SegD, SegE, SegF, SegG, SegDP, SegCOL}; // to make things easier for you
const byte myPins[] =   {   11,   10,    9,    8,    5,    4,    3,     2,      1};

//set integers for digits.(i.e D1 is equal to digit 1 and also T1, transistor 1, on board.
const byte Digits[4] = {13, 12, 7, 6};

//track swith read value
byte buttonState[4] = {HIGH};
int counterD[4] = {0};

byte cycle = 0;

const byte myDigits[10] = {
  B00000011,
  B10011111,
  B00100101,
  B00001101,
  B10011001,
  B01001001,
  B01000001,
  B00011111,
  B00000001,
  B00011001
};

void DisplayDigit(byte D) // enter a 0, the displays shows a 0. Enter a 7, the display shows a 7
{
  if (D > 9) D = 9; // a variable of type byte, will rollover when it goes past 0 or 255

  for (byte i = 0; i < 8; i++)
  {
    digitalWrite(myPins[ i ], bitRead(myDigits[ D ], i));
  }
}

/*
void turnOffAllExcept(int targetDigit)
{
  byte digitPin[] = {
    13, 12, 7, 6
  };

  byte selectedDigit = targetDigit; // any of 0 .. 3

  for (byte n = 0; n < 4; n++) {

    digitalWrite(digitPin[n], LOW); // all off
  }
  digitalWrite(digitPin[selectedDigit], HIGH);
}



void turnOnNumberForDigit(int value, int targetDigit)
{
  void (*turnOnNumber)(int targetDigit);
  turnOnNumber = DigitFunctions[value];//get function N# where # == value
  turnOnNumber(targetDigit);
}

*/

bool didPressButtonDown(int button, int buttonState)
{
  int buttonStatus = digitalRead(button);
  if (buttonStatus == LOW &&  buttonStatus != buttonState) {
    buttonState = LOW;
    return true;
  }
  else if (buttonStatus == HIGH && buttonStatus != buttonState) {
    buttonState = HIGH;
  }
  return false;
}

void setup()
{
  //set switches as inputs with internal pullup resistor
  for (byte i = 0; i < 4; i++)
  {
    pinMode(Switches[i], INPUT_PULLUP);
    pinMode(Digits[i], OUTPUT);
  }

  for (byte j = 0; j < 9; j++)
  {
    pinMode(myPins[j], OUTPUT);
  }
  //Serial.begin(9600);
}

void loop()
{
  //Keeps colon sign on always
  digitalWrite(myPins[8], LOW); // SegCOL = myPins[8]

  if (didPressButtonDown(Switches[cycle], buttonState[cycle]))
  {
    ++counterD[cycle];
    if (counterD[cycle] == 9)
      counterD[cycle] = 0;// it will be incremented to 0 on the next line
  }

  cycle++; // this is needed.
  if (cycle > 3) cycle = 0;
  
  //Im not sure what you are doing here
  /*
  turnOnNumber1(D1);
  digitalWrite(D1, HIGH);
  delay(1);
  digitalWrite(D1, LOW);
  turnOnNumber2(D2);
  digitalWrite(D2, HIGH);
  delay(1);
  digitalWrite(D2, LOW);
  turnOnNumber3(D3);
  digitalWrite(D3, HIGH);
  delay(1);
  digitalWrite(D3, LOW);
  turnOnNumber4(D4);
  digitalWrite(D4, HIGH);
  delay(1);
  digitalWrite(D4, LOW);
  //Serial.println(counterD1);
  delay(5);
  */
}

How many 7 segment displays do you have, 1 or 4?

It is only one display containing four digits

They are all individually activated by their own button though and, as you probably know, due to the nature of 7 segment displays; they are all linked together. So when you display a '1' it shows '1' on all 4 digits. That is unless you only turn on the first digit and then show the number, turn that digit off and then turn on the next digit, change the number to be displayed to what you want to show next, turn off that digit and so forth and then do it faster than 50hz. That is what I had accomplished by doing this:

 turnOnNumber1(D1);
  digitalWrite(D1, HIGH);
  delay(1);
  digitalWrite(D1, LOW);
  turnOnNumber2(D2);
  digitalWrite(D2, HIGH);
  delay(1);
  digitalWrite(D2, LOW);
  turnOnNumber3(D3);
  digitalWrite(D3, HIGH);
  delay(1);
  digitalWrite(D3, LOW);
  turnOnNumber4(D4);
  digitalWrite(D4, HIGH);
  delay(1);
  digitalWrite(D4, LOW);
  //Serial.println(counterD1);
  delay(5);

But then I needed to be able to change the numbers as the button was pressed. So the function that was simply telling it to turn on a single number 'turnOnNumber1' for instance, needed instead to be something of an array that was telling it to 'turnOnNumber(myDigits[10]);'...well you get the idea.

Post a link to your display please.

This is the best I can come up with for how I imagine it should work.

// to track which number is being displayed on digit 1
int counterMD1[9] = {0};

void displayDigit1(byte Digits[1]) {
  digitalWrite(Digits[1], HIGH);
  digitalWrite(myDigits[counterMD1[0]], HIGH);
  if (counterMD1 < 9) {
    ++counterMD1;
  }

  if (counterMD1 > 9) {
    counterMD1 = 0;
  }

}

It doesn’t work though, tells me this:
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
error: lvalue required as increment operand
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
error: incompatible types in assignment of ‘int’ to ‘int [9]’

But you get the idea. I figure if I have a counter set up individually for each digit and then I have 4 functions for 4 digits. Then it’s just a matter of determining timing between each function, with delay();

Oh ya and the link you requested.
7- Segment Display - 4-digits

I did a quick search on your display and modified the code, try this.

//set integers for switches
const byte Switches[] = {A3, A2, A1, A0};

//set integers for 7 segment display
//const byte myPins[] = { SegA, SegB, SegC, SegD, SegE, SegF, SegG, SegDP, SegCOL}; // to make things easier for you
const byte myPins[] =   {   11,   10,    9,    8,    5,    4,    3,     2,      1};

//set integers for digits.(i.e D1 is equal to digit 1 and also T1, transistor 1, on board.
const byte Digits[4] = {13, 12, 7, 6};

//track swith read value
byte buttonState[4] = {HIGH};
int counterD[4] = {0};

byte cycle = 0;

const byte myDigits[10] = {
  B00000011, // 0
  B10011111, // 1
  B00100101, // 2
  B00001101,
  B10011001,
  B01001001,
  B01000001,
  B00011111,
  B00000001,
  B00011001 // 9
};

void DisplayDigit(byte D) // enter a 0, the displays shows a 0. Enter a 7, the display shows a 7
{
  if (D > 9) D = 9; // a variable of type byte, will rollover when it goes past 0 or 255

  for (byte i = 0; i < 8; i++)
  {
    digitalWrite(myPins[ i ], bitRead(myDigits[ D ], i));
  }
}

/*
void turnOffAllExcept(int targetDigit)
{
  byte digitPin[] = {
    13, 12, 7, 6
  };

  byte selectedDigit = targetDigit; // any of 0 .. 3

  for (byte n = 0; n < 4; n++) {

    digitalWrite(digitPin[n], LOW); // all off
  }
  digitalWrite(digitPin[selectedDigit], HIGH);
}



void turnOnNumberForDigit(int value, int targetDigit)
{
  void (*turnOnNumber)(int targetDigit);
  turnOnNumber = DigitFunctions[value];//get function N# where # == value
  turnOnNumber(targetDigit);
}

*/

bool didPressButtonDown(byte button, byte buttonState)
{
  byte buttonStatus = digitalRead(button);
  if (buttonStatus != buttonState)
  {
    buttonState = buttonStatus;

    if (buttonStatus == LOW)
      return true;
  }

  return false;
}

void setup()
{
  //set switches as inputs with internal pullup resistor
  for (byte i = 0; i < 4; i++)
  {
    pinMode(Switches[i], INPUT_PULLUP);
    pinMode(Digits[i], OUTPUT);
  }

  for (byte j = 0; j < 9; j++)
  {
    pinMode(myPins[j], OUTPUT);
  }

  //Keeps colon sign on always
  digitalWrite(myPins[8], LOW); // SegCOL = myPins[8]

  //Serial.begin(9600);
}

void loop()
{
  if (didPressButtonDown(Switches[cycle], buttonState[cycle]))
  {
    ++counterD[cycle];
    if (counterD[cycle] == 9)
      counterD[cycle] = 0;// it will be incremented to 0 on the next line
    
    DisplayDigit(counterD[cycle]); // this shows the digit

    digitalWrite(Digits[cycle], HIGH);
    delay(1);
    digitalWrite(Digits[cycle], LOW);
  }

  cycle++; // this is needed.

  if (cycle > 3)
    cycle = 0;
}

Let me know what happens.

It displays most of the number 8, the top segment is missing. Then when I release the button, it goes away. The other 3 don't do anything when I press them. Did you see my suggestion above? I also include a link to the display.

Edit: I corrected a mistake I made with this,

//Turn off all digits
DisplayDigit(10); // Should be 10, not 0x0

I took a look at your example. Please keep in mind I dont have a display to do any tests on.

Try this.

//set integers for switches
const byte Switches[] = {A3, A2, A1, A0};

//set integers for 7 segment display
//const byte myPins[] = { SegA, SegB, SegC, SegD, SegE, SegF, SegG, SegDP, SegCOL}; // to make things easier for you
const byte myPins[] =   {   11,   10,    9,    8,    5,    4,    3,     2,      1};

//set integers for digits.(i.e D1 is equal to digit 1 and also T1, transistor 1, on board.
const byte Digits[4] = {13, 12, 7, 6};

//track swith read value
byte buttonState[4] = {HIGH};
int counterD[4] = {0};

byte cycle = 0;

const byte myDigits[11] = {
  B00000011, // 0
  B10011111, // 1
  B00100101, // 2
  B00001101,
  B10011001,
  B01001001,
  B01000001,
  B00011111,
  B00000001,
  B00011001, // 9
  0x0 // Display off
};

void SetDigit(byte D) // enter a 0, the displays shows a 0. Enter a 7, the display shows a 7
{
  if (D > 10) D = 10; // a variable of type byte, will rollover when it goes past 0 or 255

  for (byte i = 0; i < 8; i++)
  {
    digitalWrite(myPins[ i ], bitRead(myDigits[ D ], i));
  }
}

/*
void turnOffAllExcept(int targetDigit)
{
  byte digitPin[] = {
    13, 12, 7, 6
  };

  byte selectedDigit = targetDigit; // any of 0 .. 3

  for (byte n = 0; n < 4; n++) {

    digitalWrite(digitPin[n], LOW); // all off
  }
  digitalWrite(digitPin[selectedDigit], HIGH);
}



void turnOnNumberForDigit(int value, int targetDigit)
{
  void (*turnOnNumber)(int targetDigit);
  turnOnNumber = DigitFunctions[value];//get function N# where # == value
  turnOnNumber(targetDigit);
}

*/

bool didPressButtonDown(byte C)
{
  byte buttonStatus = digitalRead(Switches[C]);
  if (buttonStatus != buttonState[C])
  {
    buttonState[C] = buttonStatus;

    if (buttonStatus == LOW)
      return true;
  }

  return false;
}

void setup()
{
  //set switches as inputs with internal pullup resistor
  for (byte i = 0; i < 4; i++)
  {
    pinMode(Switches[i], INPUT_PULLUP);
    pinMode(Digits[i], OUTPUT);
  }

  for (byte j = 0; j < 9; j++)
  {
    pinMode(myPins[j], OUTPUT);
  }

  //Keeps colon sign on always
  digitalWrite(myPins[8], LOW); // SegCOL = myPins[8]

  //Serial.begin(9600);
}

void loop()
{
  if (didPressButtonDown(cycle))
  {
    counterD[cycle]++;
    if (counterD[cycle] > 9)
      counterD[cycle] = 0;// it will be incremented to 0 on the next line
  }
  
  SetDigit(counterD[cycle]);
  ShowDigits(cycle);
  delayMicroseconds(500); //Display this digit for a fraction of a second (between 1us and 5000us, 500 is pretty good)

  // Uncomment if you need these.
  /*
  //Turn off all segments
  AllOff();

  //Turn off all digits
  DisplayDigit(10); // made a mistake here
  */
  
  cycle++; // this is needed.

  if (cycle > 3)
    cycle = 0;
}

void ShowDigits(byte C)
{
  digitalWrite(Digits[C], HIGH);
  delay(1);
  digitalWrite(Digits[C], LOW);
}

void AllOff()
{
  for (byte i = 0; i < 4; i++)
    digitalWrite(Digits[i], LOW);
}

I had the bytes backwards for the myDigits so I got that straightened out.

B1000000, // 0
  B1111001, // 1
  B0100100, // 2
  B0110000,
  B0011001,
  B0010010,
  B0000010,
  B1111000,
  B0000000,
  B0011000, // 9

It sort of works now. Making progress!! :D

If I press a button, it remembers how many times I press it and will keep that number separate from the other 3 digits. So they all work independent of each other. Which is perfect. But the screen goes blank after pressing the buttons. I want all the numbers to be displaying all at the same time. Is there something we can do to just display everything? Everytime I try something like that it displays the same number across all four digits.

perhaps putting this

digitalWrite(Digits[cycle], HIGH);
    delay(5);
    digitalWrite(Digits[cycle], LOW);

at the end of the void loop. If I leave it like that at the end of the void loop it displays the last number remembered by the last button pressed across all four digits. So what about having a separate counter for each digit so it would be like:

digitalWrite(Digits[counterD1], HIGH);
    delay(5);
    digitalWrite(Digits[counterD1], LOW);
digitalWrite(Digits[counter2], HIGH);
    delay(5);
    digitalWrite(Digits[counter2], LOW);
digitalWrite(Digits[counter3], HIGH);
    delay(5);
    digitalWrite(Digits[counter3], LOW);
digitalWrite(Digits[counter4], HIGH);
    delay(5);
    digitalWrite(Digits[counterD4], LOW);

No, you shouldn’t need to do that.

These two should be all you need.

SetDigit(counterD[cycle]); // This one sets the digit to be displayed
ShowDigits(cycle); // This one turns on the corresponding display and shows the number

If it is showing the number on all the displays then something else is wrong, possible with the wiring.

HazardsMind:
No, you shouldn’t need to do that.

These two should be all you need.
If it is showing the number on all the displays then something else is wrong, possible with the wiring.

Oh, I meant if I take
digitalWrite(Digits[cycle], HIGH);
delay(5);
digitalWrite(Digits[cycle], LOW);

and add it in right before the end of the void loop it will then display the same number across all digits. But that was me changing the program around you gave me. The way you gave it to me it would only show numbers when the button is pressed and then the numbers go away and screen goes blank. Which is a step forward in the right direction. :slight_smile:

So where do I place:

SetDigit(counterD[cycle]);  // This one sets the digit to be displayed
ShowDigits(cycle); // This one turns on the corresponding display and shows the number

What does the code in Reply #14 do? Keep in mind you will need to fix this part below, with your corrected values.

const byte myDigits[11] = { B00000011, // 0 B10011111, // 1 B00100101, // 2 B00001101, B10011001, B01001001, B01000001, B00011111, B00000001, B00011001, // 9 0x0 // Display off };