Read variable name from Array

Okay, so I'm stuck at a quite small basic programming thingy.

So the situation:
I have 20 motion sensors connected to a (Controllino) mega board.
The 20 motion sensors are divided in 5 zones (arrays).
Example, zone 1:

int pir1 = CONTROLLINO_A0;                                //PIR 1
int pir2 = CONTROLLINO_A1;                                //PIR 2
int pir3 = CONTROLLINO_A2;                                //PIR 3
int pir4 = CONTROLLINO_A3;                                //PIR 4
int pir5 = CONTROLLINO_A4;                                //PIR 5
int pir6 = CONTROLLINO_A5;                                //PIR 6
int zone1[6] = {pir1, pir2, pir3, pir4, pir5, pir6};

The program have to be flexible to set up basic settings once on site. However this can also means the amount of sensors within a zone can change. or which pir# is in which zone.
Once a motion sensor is triggered, a function is called. I do this by checking each input every loop; if an input is HIGH, the function is called.
This all works fine and I'm really happy with it so far, but....

My issue:
Whenever I want to read which pir was responsible for activation, I have to manipulate what I pirNumber. In case of zone 1; + 1, in case of zone 2(which starts with pir7) I have to increment with 7.

      for (int i = 0  ; i < amountZone1; i++) {
        bool state = digitalRead(zone1[i]) ;
        int pirNumber = i + 1;     //here is my issue
      }


I use the pirNumber to log as well as sending it further in the program.

This does make complete sense, since I read the index value.
So if I read zone1[0], I'll read the input number of the pir# which is at the first position of zone1[] array.
and I should read this as boolean to read if the input is HIGH or LOW.

So yes, I need that, I can not change the int-array to string array.

In the end, my question:
can I 'capture' the variable name from the array, without changing it to a String array or anything like that.

The main thing is that I have to be able to read whether the input is high or low, but it would be way nicer if I also can read which sensor was responsible for that.

Many thanks (again!)

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Simple advanced technique…

...stand up a parallel array of nice names

char *niceName[] = {"Billiard Room", "Library", "Dining Room", "Pantry".…

When you know the index xx of a PIR, use that index to get its name.

   Serial.print("detected IR: ");
   Serial.println(niceName[xx]);

This is also where you could stretch and learn about struct variables, if you're all up to speed with arrays.

HTH

a7

1 Like

I guess you will have to put the variable name in your array in order to "capture" it.

1 Like
struct sensor
{
  uint8_t zone;
  uint8_t name[5];
  uint8_t pin[5];
};


sensor sensors [5]
{ 
  1, { 1,  2,  3,  4,  5}, {A0, A1, A2, A3, A4},
  2, { 6,  7,  8,  9,  0}, { 2,  3,  4,  5,  0},
  3, {10, 11, 12, 13, 14}, { 6,  7,  8,  9, 10},
  4, {15, 16,  0,  0,  0}, {21, 22,  0,  0,  0},
  5, {17, 18, 19, 20,  0}, {23, 24, 25, 26,  0}
};


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

  for (uint8_t x = 0; x < sizeof(sensors) / sizeof(sensor); x++)
  {
    Serial.print("Zone : ");
    Serial.println(sensors[x].zone);

    for (uint8_t y = 0; y < 5; y++)
    {
      if (sensors[x].name[y] > 0)
      {
        Serial.print(" Sensor : ");
        Serial.print(sensors[x].name[y]);
        Serial.print(" Pin : ");
        Serial.println(sensors[x].pin[y]);
      }
    }
  }

}

void loop()
{}
11:26:44.092 -> Zone : 1
11:26:44.092 ->  Sensor : 1 Pin : 14
11:26:44.092 ->  Sensor : 2 Pin : 15
11:26:44.128 ->  Sensor : 3 Pin : 16
11:26:44.128 ->  Sensor : 4 Pin : 17
11:26:44.128 ->  Sensor : 5 Pin : 18
11:26:44.128 -> Zone : 2
11:26:44.128 ->  Sensor : 6 Pin : 2
11:26:44.128 ->  Sensor : 7 Pin : 3
11:26:44.128 ->  Sensor : 8 Pin : 4
11:26:44.128 ->  Sensor : 9 Pin : 5
11:26:44.128 -> Zone : 3
11:26:44.128 ->  Sensor : 10 Pin : 6
11:26:44.128 ->  Sensor : 11 Pin : 7
11:26:44.128 ->  Sensor : 12 Pin : 8
11:26:44.128 ->  Sensor : 13 Pin : 9
11:26:44.128 ->  Sensor : 14 Pin : 10
11:26:44.128 -> Zone : 4
11:26:44.128 ->  Sensor : 15 Pin : 21
11:26:44.128 ->  Sensor : 16 Pin : 22
11:26:44.128 -> Zone : 5
11:26:44.128 ->  Sensor : 17 Pin : 23
11:26:44.128 ->  Sensor : 18 Pin : 24
11:26:44.128 ->  Sensor : 19 Pin : 25
11:26:44.128 ->  Sensor : 20 Pin : 26
1 Like

No, not really. Using the parallel string array isn't that complicated.

1 Like

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