Need to make part of a string a variable

Hello,

I am working on one of my first Arduino projects, which is to make a 4 digit 7-segment LED function as a clock. I've been enjoying working through the challenges, but I have run into bit of a problem. In the program, I have created functions that can be called to display a particular number value from 0-9:

//make a digit read "2" (turn specific segments of the display on or off)
int number2() {
  digitalWrite(1, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
}

The trouble I am having is in calling the function. I have put together a loop that counts millisecond, second, minutes and hours, and I would like to change the function "number__()" to respond accordingly. i.e., if minute = 3, I would like to call the number3() function for the minute representation.

My main question is this, is there a way to make the digit inside the function "number__()" a variable so that it changes from number1() to number2() to number3() when it is appropriate? I've tried searching around for a solution, but to be honest, I wasn't really sure what keywords to use to describe this issue. Thanks guys, I really do value your time and help.

Hello,

If I understand your question exactly, are you looking for a way to have [u]only one function for all possible numbers[/u]?

Well not quite. The way my code is setup now, I need 10 functions for the 10 digits (0-9). Each function turns on the specific segments of the 7-segment LED display to give a 0-9 representation on the display.

I would like a way to call the function (whether it be number1(), number2(), etc..) without having to use the digit within the function name specifically. In other words, I would like the 1, 2, 3, etc. of number1(), number2(), number 3(), etc. to be variable. Maybe it would look something like:

number" "()

Thanks for the reply, I hope this helps to clarify.

yvodili:
Well not quite. The way my code is setup now, I need 10 functions for the 10 digits (0-9). Each function turns on the specific segments of the 7-segment LED display to give a 0-9 representation on the display.

I would like a way to call the function (whether it be number1(), number2(), etc…) without having to use the digit within the function name specifically. In other words, I would like the 1, 2, 3, etc. of number1(), number2(), number 3(), etc. to be variable. Maybe it would look something like:

number" "()

Thanks for the reply, I hope this helps to clarify.

That’s really not a good idea. It’s better to use a lookup table, like this:

bool numbers[][] = {
{1, 1, 0, 0, 1}, // number 0 is pin 1 HIGH, pin 2 HIGH, pin 3 LOW, pin 4 LOW, pin 5 HIGH
{0, 1, 0, 1, 1}, // number 1 is ...
{0, 0, 0, 0, 0}, // number 2 is ...
{1, 1, 1, 1, 1},
// Continue for rest of numbers
};

const uint8_t numPins = 5;
uint8_t pinNums[numPins]= {1, 2, 3, 4, 5}; // The physical pin numbers in the same order as above

void number(int num) {
// Writes a number to a 7-segment display
  for (uint8_t i=0; i < numPins; i++) {
    digitalWrite(pinNums[i], numbers[num][i]);
  }
}

You could have an array of function pointers:

void (*func[])() = {
   &number_0,  &number_1, &number_2, &number_3, &number_4,
   &number_5,  &number_6, &number_7, &number_8, &number_9
};

And you can then call the function you want with:

func[6]();

or

func[hours]();

(note: untested)

Ok great, both of those ideas seem like much better approaches. Thanks!

I think you have a good possible answer from WizenedEE :)

My answer is the correct one for the question...

However, the question is wrong ;)

WizenedEE's answer is the correct answer for the correct question.

My answer is the correct one for the question...

However, the question is wrong

WizenedEE's answer is the correct answer for the correct question.

Hey majenko,

May be you are right as well for the question what he asked first. But I am new for C language, especailly for pointers :)

But I wonder if your code or that of WizenedEE would be compacter. if I understand that pointers correctly, there are 10 function to call in your code?!

void (*func[])() = {
   &number_0,  &number_1, &number_2, &number_3, &number_4,
   &number_5,  &number_6, &number_7, &number_8, &number_9
};

I think the guy wants to have one function for all and avoid having 10 functions! But for his original question, your answer is also good.

Cheers