I always use that technique (an array of pin numbers) when I build a serious program.
It has another advantage in insulating all of your logic in code from the actual circuit. If you want to move wires to different pins on the board, you only change the pin number constant in one place in the code, not every place you reference it.
digitalWrite(pin[0], HIGH); //example to set pin 5 on
This is because, pin[0] = 5.
The idea behind the array is to translate a list of numbers to another list of numbers (usually a contiguous set of numbers to a random set of numbers).
If you just want to set the pin directly, assuming you know the pin number, then just use:
digitalWrite(5, HIGH);
The same would apply if you had that pin number stored directly in a variable, such as:
pinnumber = 5;
digitalWrite(pinnumber, HIGH);
I hope I'm helping matters, and not making it confusing for you.
thanks again andy but i need a way of controlling the outputs via a interger. Im building a web form page that just outputs the pin number that needs to be set.
I was just trying to advocate for good programming practice to insulate your basic logic from the details of the hardware connection. This is probably more important for large, more complex systems than your application.
But think about the case where you might have to change the circuitry and have the Arduino program use different pin connections. That would require you also changing the web code to address the new pin number.
Instead, you could define constants in your web code the same as what I had in my example (like ledC = 2). Then , from your web code, e.g., you send the number that means "LED C" (i.e., 2) to the Arduino program. When it receives that code, it uses it as an index into the pinNumber array to figure out which pin to write the HIGH value to. In my example that would be pin [2] = 7.
In this way, your web code never has to know anything about pin numbers on your Arduino, or even what the Arduino program will do with that code. This is just good modular system design.
But, as I said, it may be overkill for your application; I was just explaining the principle.
Thanks guys i have solved it ! it was simple than i thought .
this is my solution.... it may not be right but it works fine !
int pinCount = 7;
int pin[] = {5 ,6 ,7 ,8 ,9 ,10 ,11};
void setup()
{
int thisPin;
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(pin[thisPin], OUTPUT); // this part seems to count up ok
}
}
digitalWrite(5 , HIGH); // sets 5 on
digitalWrite(4, HIGH); // sets 4 on