Array index with variable

Would this be PIN_ZONE_1 is now HIGH?

int relays[] = {PIN_PUMP_1, PIN_ZONE_1, PIN_ZONE_2, PIN_ZONE_3, PIN_ZONE_4, PIN_ZONE_5, PIN_ZONE_6, PIN_ZONE_7, PIN_ZONE_8, PIN_ZONE_9, PIN_ZONE_10, PIN_ZONE_11, PIN_ZONE_12};

int currentZone = 0;

currentZone = currentZone + 1;

digitalWrite(relays[currentZone], HIGH);

digitalWrite(relays[index], HIGH);

The above is valid, what is the actual question that you have ?

I guess what I'm trying to do is a little bit different than above.
I'm using the app RemoteXY.
In code without using the array it would be done like:
RemotXY.zone_1 = 1;

I'm trying to do this with the variable "currentZone" changing in the code and then sets the corresponding zone to 1.

uint8_t zone_1;             // =1 if switch ON and =0 if OFF

int pumpORzone[] = {RemoteXY.zone_1, RemoteXY.zone_2, RemoteXY.zone_3, RemoteXY.zone_4, RemoteXY.zone_5, RemoteXY.zone_6, RemoteXY.zone_7, RemoteXY.zone_8, RemoteXY.zone_9, RemoteXY.zone_10, RemoteXY.zone_11, RemoteXY.zone_12};

int currentZone = 0;

pumpORzone[currentZone] = 1;

The above makes array element zero (0) equal to 1.


You know you can print things to prove a hypothesis.

Serial.println(pumpORzone[currentZone]);

This doesn't seem to be working.
When I ran the code, it got stuck in a repetitive Serial.print of another line.
Which may be another issue, I'm not sure yet.

I did however track down some other info which I think is related to the Remote.XY and from the looks of it, the variables have to be re- "struct" and done a bit different.
I haven't tested this yet but will give it a shot.

I thank you for the help so far!

Here's the link:

You're not confusing pin values with pin numbers here? Or values with variables?

Replacing values in the array do not affect pin status, as the compiler doesn't know the values are pins, to it they are just integers.

The values inside the array initialization are just that; values. They are not variables, and so the code you propose is not in any way equivalent to the code line

RemoteXY.zone_1 = 1;

This 'zone_1' seems to be a variable (or field, really) inside a struct or an object, while the entries inside the pumpOrZone array are just values.

If you want to make an array of "variables", so that you can modify the original variable, you need an array of pointers:

int *arr[] = { &RemoteXY.zone_1, &RemoteXY.zone_2, ..};

*arr[0]=1;

The assignment now stores value 1 inside the memory location that is pointed to in array position 0. The "*" means just that. This means the assignment does not modify the array content, just the memory location allocated to the variable RemoteXYZone.zone_1 at the time of array initialization.

The &something means "get address of something in memory".

:slight_smile:

Yes.

Sorry for the delay in responding.
Thank you for your explanation, your help worked like it should.
Yes, the zone_1 was in a struct like you were thinking.
I am not very familiar with arrays, so thanks for explaining how it is supposed to work in this situation.
I now have all of this part working as it should.
I have a variable that changes in the code that is used as the pointer in the different arrays.
Still not bug free yet but this part is working fine now.

A little snippit:

int zPlusOne = currentZone - 1; // Add 1 for Pump is at Zero Index
      int tempZone = currentZone - 2;
      digitalWrite(relays[zPlusOne], HIGH);
      *pumpORzone[tempZone] = 0;
      *pumpORzoneLED[tempZone] = 0;