# use array in analogWrite?

Hello programmers

I am very new to coding / electronics. (about 3 weeks) I have a question about array and analogWrite

Basically I want to use an array of number as power input to control another array of pins:

analogWrite (pin[11], power[11]);

then I found my self writing for loops within for loops and got lost quickly.

I want to make sure that the number from item 1 of power[11] goes into item 1 of pin[11], then item 2 of power[11] goes into item 2 of pin[11].... so on:

Power Pin
#1 200 ---> 1
#2 155 ---> 2
#3 45 ---> 3
#4 22 ---> 4

....

is there an easier way to do this than writing for loop within for loop?

``````byte pin[] = {0,1,2,3,4,5,);

// then in loop() :
for (x=0; x<6; x=x+1){ // analog pins A0 to A5
analogWrite (pin[x], power[x]);
}
``````

Note:

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

``````byte pin[] = {0,1,2,3,4,5,);
``````

// then in loop() :
for (x=0; x<6; x=x+1){ // analog pins A0 to A5
analogWrite (pin, power);
}

I’m a bit confused about variable x, if it’s used in both pin and power, then wouldn’t it fail at the first iteration: 10<6?

Since there are 6 (0-5) elements in his arrays you will iterate from 0 to 5 only.
x is used to pull 'that' element out of the array.

Example if x was 4 then this would pull out element 5 out of the arrays. (0 relative)

.

oh you are right. it seems I misunderstood how array works!

Yes, my pin array should have been

byte pin = {3,5,6,9,10,11,};

so with x of 4, pin 10 would have its output changed.