One digital input many high/low outputs

Hi

I am trying to write some code that will take one input and turn on/off many outputs. This will be fleshed out to have several inputs controlling many outputs, so I am trying to use arrays and Loops.

It looks ok to me and I cant see what I am doing wrong. Also when I press the switch I hear a high pitched whistle noise. That goes when I turn the switch off.

I have naming convention for the array so it they match up with the input pin, then I loop through the contents of the array and pull pins high/low.

//Currently switch is on pin 2, LED is on 11
const int zone2[] = {11};     // group like pins together number is the same as the input pin that controls
int buttonState = 0;         // variable for reading the pushbutton status

void setup() 
{
  // initialize the output pins that will control lights
  pinMode(11, OUTPUT);// when more lights added will change to a loop to configure pins as output      
  
  
  // initialize the pushbutton pin as an input:
  //set all light switches to the same block ie pins 2 - 10
  
  byte i;
  
  
  //this loop sets all the pins as inputs
  for (i=2;i< 3;i++) {
    pinMode(i, INPUT);  
    digitalWrite(i,HIGH);  // this makes it connect to the internal resistor
  }
}

void loop()
{
  // 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<3; myInput++) {
  buttonState = digitalRead(myInput);

    
    int ArrayCount;
    
    String zoneChk = "zone" + String(myInput);
    ArrayCount = sizeof(zoneChk);
    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);
              }
        } 
    }
}
    String zoneChk = "zone" + String(myInput);
    ArrayCount = sizeof(zoneChk);

Since myInput is 2, zoneChk will be “zone2”. The sizeof the element is NOT the number of characters in the String. Even if it were, it makes no sense to then:

    for (arrayPosition = 0;arrayPosition < ArrayCount ; arrayPosition++)
        {
          if (buttonState == HIGH) {     
          // turn LED on:    
            digitalWrite(zoneChk[arrayPosition], HIGH);

What possible sense is it to write the ‘z’ pin HIGH?

Hi

I will eventually have more than one Zone so the outer loop will check the state of each zone. On each pass I want to concatenate zone and myInput.

So I would like to check the state of pin 2 and then loop through the zone2 array contents and pulling the pins high or low depending on the value of buttonState

I thought Sizeof was the number of elements in an array, and wanted to use the to control the iterations the loop performs.

jasemilly:
I thought Sizeof was the number of elements in an array, and wanted to use the to control the iterations the loop performs.

sizeof returns a size in bytes of storage used, not elements ( sizeof can be used with non array types ).
How to get the size of an array

Also, pauls was pointing out that 'zoneChk' is not even an array, its a String object. However you can use zoneChk.length(), FYI this does not include the null character.

Thanks

I declared Zone2 at the top of the script

const int zone2 = {11};

String zoneChk = "zone" + String(myInput);
ArrayCount = sizeof(zoneChk);

I thought declaring the above code would get the size of each array zone1,zone2,zone3 when they are eventually created using loop counter.

should zoneChk = "zone" + String(myinput) + "";

would that mean I was now referencing an array leaving me to figure how to get the correct size of an array.

Hi, were is the noise coming from and what are you outputting to.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom.... :slight_smile:

jasemilly:
would that mean I was now referencing an array leaving me to figure how to get the correct size of an array.

Nope, C/C++ function nothing like php or javascript ( what your code looks like ).

You have the id of the item you want (myinput), so you could use a 2d array and simply address it for each zone:

const int zones[][2]={
  { 3,4},
  { 5,6 },
  { 11, 12 }
};


//...

  ArrayCount = sizeof( zones[ myinput] ) / sizeof( *zones[ myinput] );

    for (arrayPosition = 0;arrayPosition < ArrayCount ; arrayPosition++)
        {
          if (buttonState == HIGH) {     
          // turn LED on:    
            digitalWrite(zones[ myinput][ arrayPosition ], HIGH);

I am outputting to a 16 channel relay It's a very high pitched whistle.

When it didn't happen when I did a simple switch with no arrays and just pulled output pin high/low.

At work at the moment won't be able to photograph until later on.

Thanks for that pYro_65

I see the structure of that but am a little confused are there any links to explain this type of array?

I have seen examples of arrays look like

int myarray = {1,2,3,4}

but not your example.

Give this a read:

http://www.cplusplus.com/doc/tutorial/arrays/

The empty '' in my example can be set if you want, however the compiler will count the first dimension in the structure you provide ( if you do not initialize it, you will need to provide the value ).

I am confused why do I need a 2d array, the relay is controlling LED lights for my home.

A zone is a room and zone1 is the kitchen. I have one light in there. Zone2 is a valid name for a variable,
and an array declared

int zone2[] = {11} ;

is valid name for an array

x = zone2[0]

is valid

so wouldn't the line

String zoneChk = "zone" + String(myInput);
    ArrayCount = sizeof(zoneChk);

the same as saying

    ArrayCount = sizeof(zone2)/sizeof( int );

so I can loop through and as myInput changes I can check each individual array called zone(myinput)

So original mistake I have made isn't getting the correct number of items in the array?

so wouldn't the line ... the same as saying

No. In the compiled code, arrays have addressed. They do NOT have names. You need to purge your mind of languages like PHP that are interpreted. C and C++ are compiled languages. No names!

Ah, I understand what you are saying thanks.