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);
}
}
}
}
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?
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.
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?
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 ).
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!