I would like to create several array's using the following naming convention
const int zone2[] = {11};
const int zone3[] = {12,13};
const int zone4[] = {14};
then I would like to create a loop and check get the values of the array I am doing the following.
// read the state of the pushbutton value:
byte myInput =2;
//Outer loop checks the state of the Input Pins
//Inner loop iterates through corresponding array and pulls pins high or low depending on ButtonState
for (myInput = 2;myInput<5; myInput++) {
buttonState = digitalRead(myInput);
int ArrayCount;
String zoneChk = "zone" + String(myInput);
ArrayCount = sizeof(zoneChk) / sizeof( int ) ; // sizeof( int ) is the newpart
int arrayPosition;
for (arrayPosition = 0;arrayPosition < ArrayCount ; arrayPosition++) {
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(zoneChk[arrayPosition], HIGH);
}
else {
// turn LED off:
digitalWrite(zoneChk[arrayPosition], LOW);
}
}
}
I have been told the array is invalid for C++ arduino, I am not so sure and just learning as I go. I am looking to control some house lights the Zones are effectively rooms and the contents of the array the different lights within the room. I have called them Zones'x' so I can reducr the code need to check the switches for each room.
thanks
String zoneChk = "zone" + String(myInput); ArrayCount = sizeof(zoneChk) / sizeof( int ) ; // sizeof( int ) is the newpart
Nooo no no no no.
const int * const zones[] = {zone2, zone3, zone4};
...
for (int z = 0; z < 3; ++z)
{
// access zones[z]
}
Also, consider null-terminating each zoneX
(and optionally zones
) so that you know when you've reached the end.
String zoneChk = "zone" + String(myInput);
ArrayCount = sizeof(zoneChk) / sizeof( int ) ; // sizeof( int ) is the newpart
This will not allow you to access the zone2..5 variables at runtime. You are creating a "zone2" string that has nothing to do with the zone2 variable.
The code below compiles but I don't have an Arduino with me to fully test it. It will still allow you to change the size of zone2,3,4 without any modifications to the code. You will have to add more case statements (there is commented out zone5 code included) if you want more zones.
If you want it to be fully dynamic we can use a multi-dimensional array for the zones but this should be a good start for minimal code
for (myInput = 2;myInput<5; myInput++) {
int buttonState = digitalRead(myInput);
int ArrayCount;
const int *zoneChk;
//This is the part you will have to add to if you want more zones
switch(myInput){
case 2:
ArrayCount = sizeof(zone2) / sizeof (zone2[0]);
zoneChk = zone2;
break;
case 3:
ArrayCount = sizeof(zone3) / sizeof (zone3[0]);
zoneChk = zone3;
break;
case 4:
ArrayCount = sizeof(zone4) / sizeof (zone4[0]);
zoneChk = zone4;
break;
//ex. case 5
//case 5:
// ArrayCount = sizeof(zone5) / sizeof (zone5[0]);
// zoneChk = zone5;
//break;
}
int arrayPosition;
for (arrayPosition = 0;arrayPosition < ArrayCount ; arrayPosition++) {
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(zoneChk[arrayPosition], HIGH);
}
else {
// turn LED off:
digitalWrite(zoneChk[arrayPosition], LOW);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.