Passing a collection of function into an array

int disp1 = 2;
int disp2 = 3;
int disp3 = 4;
int disp4 = 5;
int disp5 = 6;
int disp6 = 7;
int disp7 = 8;
int disp8 = 9;
int disp9 = 10;
int disp10= 11;
int disp11= 12;
int disp12 = 13;
int last1 = A0;
int last2 = A1;
int last3 = A2;
int variable = A4;
//pointer declaration
int *number[1000] = NULL;
void zero(void), one(void), two(void), three(void), four(void), five(void), six(void), seven(void), eight(void), nine(void);
void (*number[1000])() = {zero, one, two, three, four, five, six, seven, eight, nine};
int abc[] = {disp1, disp2, disp3, disp4, disp5, disp6, disp7, disp8, disp9, disp10, disp11, disp12, last1, last2, last3};
//function declaration for the counting numbers



void setup() {
  // put your setup code here, to run once:
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  
}
void loop() 
{
  // put your main code here, to run repeatedly:
  // read the sensor
  int input;
  input = analogRead(variable);
  if (input == HIGH)
  {
    for (int i = 0; i< sizeof(number); i++)
    {
      *number[i];
      for (int j = 0; j< sizeof(number); j++)
      {
        *number[j];
        if (*number[j]== *number[8])
        {
          *number[i]== *number[i+1];
        }
      }
    }
  }
}

void zero()
{
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(5, HIGH);
  delay(30);
  digitalWrite(8, HIGH);
  delay(30);
  digitalWrite(11, HIGH);
  delay(30);
  digitalWrite(A1, HIGH);
  delay(30);
  digitalWrite(13, HIGH);
  delay(30);
  digitalWrite(10, HIGH);
  delay(30);
  digitalWrite(7, HIGH);
  
  delay(1000);
  
}
void one()
{
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(6, HIGH);
  delay(30);
  digitalWrite(9, HIGH);
  delay(30);
  digitalWrite(12, HIGH);
  delay(30);
  digitalWrite(A1, HIGH);
  delay(30);
  delay(1000);
}
void two()
{ 
  digitalWrite(5, HIGH);
  delay(30);
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(7, HIGH);
  delay(30);
  digitalWrite(10, HIGH);
  delay(30);
  digitalWrite(12, HIGH);
  delay(30);
  digitalWrite(A0, HIGH);
  delay(30);
  digitalWrite(A1, HIGH);
  delay(30);
  digitalWrite(A2, HIGH);
  delay(1000);
}
void three()
{
  
  digitalWrite(2, HIGH);
  delay(30);
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(4, HIGH);
  delay(30);
  digitalWrite(7, HIGH);
  delay(30);
  digitalWrite(9, HIGH);
  delay(30);
  digitalWrite(10, HIGH);
  delay(30);
  digitalWrite(13, HIGH);
  delay(30);
  digitalWrite(A0, HIGH);
  delay(30);
  digitalWrite(A1, HIGH);
  delay(30);
  digitalWrite(A2, HIGH);
  delay(1000);
}
void four()
{ 
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(6, HIGH);
  delay(30);
  digitalWrite(8, HIGH);
  delay(30);
  digitalWrite(9, HIGH);
  delay(30);
  digitalWrite(10, HIGH);
  delay(30);
  digitalWrite(12, HIGH);
  delay(30);
  digitalWrite(A1, HIGH);
  delay(1000);
}
void five()
{
  
  digitalWrite(2, HIGH);
  delay(30);
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(4, HIGH);
  delay(30);
  digitalWrite(5, HIGH);
  delay(30);
  digitalWrite(6, HIGH);
  delay(30);
  digitalWrite(10, HIGH);
  delay(30);
  digitalWrite(13, HIGH);
  delay(30);
  digitalWrite(A0, HIGH);
  delay(30);
  digitalWrite(A1, HIGH);
  delay(1000);
}
void six()
{
  digitalWrite(2, HIGH); 
  delay(30);
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(7, HIGH);
  delay(30);
  digitalWrite(10, HIGH);
  delay(30);
  digitalWrite(13, HIGH);
  delay(30);
  digitalWrite(A1, HIGH);
  delay(30);
  digitalWrite(11, HIGH);
  delay(30);
  digitalWrite(9, HIGH);
  delay(1000);
  
}
void seven()
{
  
  digitalWrite(2, HIGH); 
  delay(30);
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(4, HIGH);
  delay(30);
  digitalWrite(7, HIGH);
  delay(30);
  digitalWrite(10, HIGH);
  delay(30);
  digitalWrite(12, HIGH);
  delay(30);
  digitalWrite(A0, HIGH);
  delay(1000);
  
}
void eight()
{
  
  
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(5, HIGH);
  delay(30);
  digitalWrite(7, HIGH);
  delay(30);
  digitalWrite(9, HIGH);
  delay(30);
  digitalWrite(11, HIGH);
  delay(30);
  digitalWrite(A1, HIGH);
  delay(30);
  digitalWrite(13, HIGH);
  delay(1000);
  
}
void nine()
{
  digitalWrite(3, HIGH);
  delay(30);
  digitalWrite(5, HIGH);
  delay(30);
  digitalWrite(7, HIGH);
  delay(30);
  digitalWrite(9, HIGH);
  delay(30);
  digitalWrite(10, HIGH);
  delay(30);
  digitalWrite(12, HIGH);
  delay(30);
  digitalWrite(A0, HIGH);
  delay(1000);
}


void clear()
{
  digitalWrite(2, LOW); 
  delay(30);
  digitalWrite(3, LOW);
  delay(30);
  digitalWrite(4, LOW);
  delay(30);
  digitalWrite(5, LOW);
  delay(30);
  digitalWrite(6, LOW);
  delay(30);
  digitalWrite(7, LOW);
  delay(30);
  digitalWrite(8, LOW);
  delay(30);
  digitalWrite(9, LOW);
  delay(30);
  digitalWrite(10, LOW);
  delay(30);
  digitalWrite(11, LOW);
  delay(30);
  digitalWrite(12, LOW);
  delay(30);
  digitalWrite(13, LOW);
  delay(30);
  digitalWrite(A0, LOW);
  delay(30);
  digitalWrite(A1, LOW);
  delay(30);
  digitalWrite(A2, LOW);
  delay(150);
}

I was trying to print the count of People who entered a premise using an IR sensor and display that count using the matrix of LEDs I figured out most of the part but the part where I am struggling is passing a bunch of functions into an array {Yes I can easily do it through switch cases (I understood it from my significant amount of research)} but I am trying to develop my bag of tools for good and be good at programming so I humbly ask for help in this matter (any other suggestions for making this code much more efficient would be a good learning point for me.
Yes, I accept that I made many mistakes but please, help from you all would be great.
Along with that, in the last few days, I noticed that I haven't provided the complete problem statement, I planned to first pass the collection of functions into an array and then call the function from an array for in this case to do display work using the matrix of LEDs as I have shown below. What I want to do is show the LEDs display the count (as soon as the IR sensor detects a person entering the premises) from zero to nine as soon as I have reached nine I want the other LED grid to show the digit one alongside repeat the pattern from zero to nine on the other LED grid and as soon as I reach one of the LED grid and nine on the other LED grid I want the first grid to show two and start the pattern from zero to nine and so on.
Yes, I have used the PIR sensor for the software but in the real project, I shall be using the IR sensor.
So now I have provided the complete information so please help me solving this problem

You can put functions in an array and call them like this, if that is what you really want to do

typedef void (*functionPointer)(void);

void funcA(char *x);  //function prototypes needed
void funcB(char *x);
void funcC(char *x);

functionPointer functionList[] = { &funcA, &funcB, &funcC };

void setup()
{
    Serial.begin(115200);
}

void loop()
{
    functionList[random(0, 3)]();  //call a function at random
    delay(1000);
}

void funcA()
{
    Serial.println(F("in funcA"));
}

void funcB()
{
    Serial.println(F("in funcB"));
}

void funcC()
{
    Serial.println(F("in funcC"));
}

But the char part is kind of another problem because there is no variable input in any of the functions

My understanding was that you want to call function zero() when 0 people have been counted, one() when 1 person has been counted, two() when 2 have been counted and so on.

Is that what you want to do ?

If not then please give more details

Try this.

const int dispCount = 12;
const int sensorCount = 3;
const int lastCount = 3;
const int variable = A4;

int displayPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2};
int lastPins[] = {A0, A1, A2};

int peopleCount = 0;

void zero() {
  // your implementation for zero
}

void one() {
  // your implementation for one
}

// Implement other count functions similarly

void clearDisplay() {
  for (int i = 0; i < dispCount; i++) {
    digitalWrite(displayPins[i], LOW);
  }
}

void setup() {
  for (int i = 0; i < dispCount; i++) {
    pinMode(displayPins[i], OUTPUT);
  }
}

void loop() {
  int sensorValue = analogRead(variable);
  
  if (sensorValue == HIGH) {
    peopleCount++;
    
    // Display the count using functions in the array
    for (int i = 0; i < peopleCount; i++) {
      number[i]();  // Call the corresponding function
    }

    delay(1000);  // Adjust the delay as needed
    clearDisplay();  // Clear the display after showing the count
  }
}

void (*number[])() = {zero, one, /* add other count functions here */};

This code uses arrays to store pin numbers for displays and last pins. It also simplifies the display logic and provides a more efficient way to call functions based on the count.

1 Like

thanks

It's giving an error as to 'number' was not declared in this scope

I am struggling to resolve it please help

I want the IR sensor to detect the person's presence and as soon as it detects the presence a specific pattern of LEDs should glow delineating that count of a person entering the place

but yes that's exactly what I want to do what you said earlier

But that does not really make sense. Function pointers is a very advanced programming technique that is rarely needed. All your functions do nearly the same. Create an ( 2-dimensional ) array where you define which leds should light up ( one dimension for the led-pattern, one for the state as 1...9 ). Than create one function to light up the leds according to this array and give it a parameter which pattern to use.

Actually, I am making two Arduinos connect and display the message on two matrix of LEDs let me add a picture from my tinkerCAD simulation

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.