Making an array to control led

Hi im working on a project with 11 led lights, and i want to simplify a method to turn them on and off with arrays. iv tried searching and using examples but i cant seem to get it to work.

i want to turn the leds on and off like this
//array 1= on 2=off
Array = 1,0,1,1,1,1,0,0,1,0,1

can someone help me setting this up? thanks in advance :slight_smile:

This is what I have so far

int Led3 = 3;
int Led4 = 4;
int Led5 = 5;
int Led6 = 6;
int Led7 = 7;
int Led8 = 8;
int Led9 = 9;
int Led10 = 10;
int Led11 = 11;
int Led12 = 12;
int Led13 = 13;

int Timer = 0;
int Pattern = 0;

char LedCont[12];

void setup(){
Serial.begin(9600);

pinMode(Led3,OUTPUT);
pinMode(Led4,OUTPUT);
pinMode(Led5,OUTPUT);
pinMode(Led6,OUTPUT);
pinMode(Led7,OUTPUT);
pinMode(Led8,OUTPUT);
pinMode(Led9,OUTPUT);
pinMode(Led10,OUTPUT);
pinMode(Led11,OUTPUT);
pinMode(Led12,OUTPUT);
pinMode(Led13,OUTPUT);

}

void loop(){

if(LedCont[1] == 1){digitalWrite(Led3,HIGH);} else {digitalWrite(Led3,LOW);}
if(LedCont[2] == 1){digitalWrite(Led4,HIGH);} else {digitalWrite(Led4,LOW);}
if(LedCont[3] == 1){digitalWrite(Led5,HIGH);} else {digitalWrite(Led5,LOW);}
if(LedCont[4] == 1){digitalWrite(Led6,HIGH);} else {digitalWrite(Led6,LOW);}
if(LedCont[5] == 1){digitalWrite(Led7,HIGH);} else {digitalWrite(Led7,LOW);}
if(LedCont[6] == 1){digitalWrite(Led8,HIGH);} else {digitalWrite(Led8,LOW);}
if(LedCont[7] == 1){digitalWrite(Led9,HIGH);} else {digitalWrite(Led9,LOW);}
if(LedCont[8] == 1){digitalWrite(Led10,HIGH);} else {digitalWrite(Led10,LOW);}
if(LedCont[9] == 1){digitalWrite(Led11,HIGH);} else {digitalWrite(Led11,LOW);}
if(LedCont[10] == 1){digitalWrite(Led12,HIGH);} else {digitalWrite(Led12,LOW);}
if(LedCont[11] == 1){digitalWrite(Led13,HIGH);} else {digitalWrite(Led13,LOW);}

}

You could put the pins in an array also:
byte pinArray[] = 3,4,5,6,7,8,9,10,11,12;

then

for (x=0; x<11; x=x+1){
pinMode (pinArray[x],  OUTPUT);
}

and 

for (x=0; x<11; x=x+1){
if (LedCont[x] == 1){
digitalWrite (pinArray[x], HIGH);
}
else{
digitalWrite (pinArray[x], LOW);}
}

hi try

int LED[1] ={3,4,5,6,7,8,9,10,11,12,13};

for (int i = 0; i<=10;i++){
pinmode (LED[i], OUTPUT);
}

for (int i = 0;i<+10){
if(LedCont[1] == 1){digitalWrite(LED[i],HIGH);} else {digitalWrite(LED[i],LOW);}

vikingsraven:
hi try

int LED[1] ={3,4,5,6,7,8,9,10,11,12,13};

for (int i = 0; i<=10;i++){
pinmode (LED[i], OUTPUT);
}

for (int i = 0;i<+10){
if(LedCont[1] == 1){digitalWrite(LED[i],HIGH);} else {digitalWrite(LED[i],LOW);}

CrossRoads:
You could put the pins in an array also:
byte pinArray[] = 3,4,5,6,7,8,9,10,11,12;

then

for (x=0; x<11; x=x+1){

pinMode (pinArray[x],  OUTPUT);
}

and

for (x=0; x<11; x=x+1){
if (LedCont[x] == 1){
digitalWrite (pinArray[x], HIGH);
}
else{
digitalWrite (pinArray[x], LOW);}
}

Hi, thx for the quick reply! :slight_smile:

how do i set the array? like if(Timer >= 200){LedCont = 1,0,1,0,0,0,0,1,1,1,0} ?
or if(Timer >= 300 && Timer <= 400){LedCont = 0,1,0,1,1,1,1,0,0,0,1}

That's what I posteed already.

vikingsraven has an error tho:

int LED[1] ={3,4,5,6,7,8,9,10,11,12,13};
will not work - how can 1 element of the array have 10 different values assigned?

for (int i = 0;i<+10){
How is i to change each pass thru the for loop?

if(LedCont[1] == 1){
that will always look at the same element, not what you want if you intend to go thru 10 elements of the array.

Ok, sorry , im new to arrays, il try this! :slight_smile: thx

You were pretty close:

if(Timer >= 200){LedCont [] = {1,0,1,0,0,0,0,1,1,1,0,}; 
if( (Timer >= 300) && (Timer <= 400)){LedCont[] = 0,1,0,1,1,1,1,0,0,0,1,};

yeah just spotted that , was on the way out of the door at work. Sorry but , it was to give you an idea, still a newbie myself but have been playing with arrays lately!

That's what I posteed already.

vikingsraven has an error tho:

int LED[1] ={3,4,5,6,7,8,9,10,11,12,13};
will not work - how can 1 element of the array have 10 different values assigned?

for (int i = 0;i<+10){
How is i to  change each pass thru the for loop?

if(LedCont[1] == 1){
that will always look at the same element, not what  you want if you intend to go thru 10 elements of the array.

im sure my fingers are dyslexic!
should have been 11 , i <= 10,i++ and LedCont square bracket i square bracket. Bugger!
i literally must have posted it the same time as no one had posted.

vikingsraven:
yeah just spotted that , was on the way out of the door at work. Sorry but , it was to give you an idea, still a newbie myself but have been playing with arrays lately!

That's what I posteed already.

vikingsraven has an error tho:

int LED[1] ={3,4,5,6,7,8,9,10,11,12,13};
will not work - how can 1 element of the array have 10 different values assigned?

for (int i = 0;i<+10){
How is i to  change each pass thru the for loop?

if(LedCont[1] == 1){
that will always look at the same element, not what  you want if you intend to go thru 10 elements of the array.




im sure my fingers are dyslexic! 
should have been 11 , i <= 10,i++ and LedCont square bracket i square bracket. Bugger!
i literally must have posted it the same time as no one had posted.

Hi :slight_smile:
sorry, I cant get it to work,

if(Timer >= 300 && Timer <= 400){int LedCont [] = {0,1,0,1,1,1,1,0,0,0,1};}

seems to work, but it doesent change the array

if(Timer >= 300 && Timer <= 400){LedCont [] = {0,1,0,1,1,1,1,0,0,0,1};}

Just gives me errors

 In function 'void loop()':
error: expected primary-expression before ']' token
error: expected primary-expression before '{' token
error: expected `;' before '{' token

:slight_smile:

I have no way to test right now.

if(Timer >= 300 && Timer <= 400)

{
  LedCont [] = {0,1,0,1,1,1,1,0,0,0,1};
}



Just gives me errors

That's because you can't initialise a whole array like that except when it is first declared.

UKHeliBob:

if(Timer >= 300 && Timer <= 400)

{
  LedCont [] = {0,1,0,1,1,1,1,0,0,0,1};
}



Just gives me errors

That's because you can't initialise a whole array like that except when it is first declared.

Hi

ok, how do i declared it? :slight_smile:

if(Timer >= 300 && Timer <= 400){LedCont [] = {0,1,0,1,1,1,1,0,0,0,1};}

Error or not, if this were a declaration (it isn't), the array would be out of scope in nanoseconds.
Using sixteen bits to hold one bit of information is uneconomical - a single int would be more sensible.

So a for:loop is needed to re-load it to change all the values then?
Have 2 copies is pre-setup code,
then a for:loop to copy one or the other?

Or, just change the outputs using the data from one or the other.

if(Timer >= 200 && Tiimer <300){
for (x=0; x<10; x=x+1){
digitalWrite (pinArray[x], LedContA[x]);
}

if( (Timer >= 300) && (Timer <400)){
for (x=0; x<10; x=x+1){
digitalWrite (pinArray[x], LedContB[x]);
}

AWOL:

if(Timer >= 300 && Timer <= 400){LedCont [] = {0,1,0,1,1,1,1,0,0,0,1};}

Error or not, if this were a declaration (it isn't), the array would be out of scope in nanoseconds.
Using sixteen bits to hold one bit of information is uneconomical - a single int would be more sensible.

Ok, iv just started learning arduino, so if you could break it down or explain it in english? :slight_smile:

CrossRoads:
So a for:loop is needed to re-load it to change all the values then?
Have 2 copies is pre-setup code,
then a for:loop to copy one or the other?

Or, just change the outputs using the data from one or the other.

if(Timer >= 200 && Tiimer <300){

for (x=0; x<10; x=x+1){
digitalWrite (pinArray[x], LedContA[x]);
}

if( (Timer >= 300) && (Timer <400)){
for (x=0; x<10; x=x+1){
digitalWrite (pinArray[x], LedContB[x]);
}

ok how does this work? If I want 10, 11 and 5 should light upp, how do i do that, with this code?

You would initialize one of the arrays as
3,4,5,6,7,8,9,10,11,12
byte LedContX [] = {0,0,1,0,0,0,0,1,1,0,};
assuming 1 = on.
or at some in your code,
LedContX[2] = 1;
LedContX[7] = 1;
LedContX[8] = 1;

before the digitalWrite for:loop that changes outputs based on LedContX[];

ok, still errors :frowning:

OK.Thanks for letting us know

ok i got it to work by making a string and converting it to a char Array using toCharArray, rather than changing the array

Thanks for all the help! :slight_smile: