# else if LEDs

ok, so i’ve got a 3 x 3 matrix of led’s, which at the moment are selected by two pot’s, one for x axis, and one for y, but that will change in a moment.

the only way i know to make it only turn on one led is like so (code to follow) and I am wondering if there is (i’m sure there is) another, shorter way to do this in the code

int g1 = 3;
int g2 = 4;
int g3 = 5;
int b1 = 6;
int b2 = 7;
int b3 = 8;
int r1 = 9;
int r2 = 10;
int r3 = 11;
int pot1 = 5;
int pot2 = 4;
int x = 0;
int y = 0;

void setup()
{
pinMode(g1, OUTPUT);
pinMode(g2, OUTPUT);
pinMode(g3, OUTPUT);
pinMode(b1, OUTPUT);
pinMode(b2, OUTPUT);
pinMode(b3, OUTPUT);
pinMode(r1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(r3, OUTPUT);

}

void loop()
{
x = x/4;
y = y/4;

if (x <= 85 && y <= 85)
{
digitalWrite(g1, HIGH);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(b1, LOW);
digitalWrite(b2, LOW);
digitalWrite(b3, LOW);
digitalWrite(r1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r3, LOW);
}

else if ((x <= 85) && (y > 85 && y <= 170))
{
digitalWrite(g1, LOW);
digitalWrite(g2, HIGH);
digitalWrite(g3, LOW);
digitalWrite(b1, LOW);
digitalWrite(b2, LOW);
digitalWrite(b3, LOW);
digitalWrite(r1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r3, LOW);
}

else if ((x <= 85) && (y > 170 && y <= 255))
{
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, HIGH);
digitalWrite(b1, LOW);
digitalWrite(b2, LOW);
digitalWrite(b3, LOW);
digitalWrite(r1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r3, LOW);
}

else if ((x > 85 && x <= 170) && (y <= 85))
{
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(b1, HIGH);
digitalWrite(b2, LOW);
digitalWrite(b3, LOW);
digitalWrite(r1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r3, LOW);
}

else if ((x > 85 && x <= 170) && (y > 85 && y <= 170))
{
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(b1, LOW);
digitalWrite(b2, HIGH);
digitalWrite(b3, LOW);
digitalWrite(r1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r3, LOW);
}

else if ((x > 85 && x <= 170) && (y > 170 && y <= 255))
{
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(b1, LOW);
digitalWrite(b2, LOW);
digitalWrite(b3, HIGH);
digitalWrite(r1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r3, LOW);
}

else if ((x > 170 && x <= 255) && (y <= 85))
{
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(b1, LOW);
digitalWrite(b2, LOW);
digitalWrite(b3, LOW);
digitalWrite(r1, HIGH);
digitalWrite(r2, LOW);
digitalWrite(r3, LOW);
}

else if ((x > 170 && x <= 255) && (y > 85 && y <= 170))
{
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(b1, LOW);
digitalWrite(b2, LOW);
digitalWrite(b3, LOW);
digitalWrite(r1, LOW);
digitalWrite(r2, HIGH);
digitalWrite(r3, LOW);
}

else if ((x > 170 && x <= 255) && (y > 170 && y <= 255))
{
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(b1, LOW);
digitalWrite(b2, LOW);
digitalWrite(b3, LOW);
digitalWrite(r1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r3, HIGH);
}

}

I changed you code around a bit, here it is if you want to test it:

[UNTESTED CODE]

/*
|| 3*3 led matrix
|| Controlled by two pots
||
|| Contributed:
|| Alexander Brevig
*/

#define G_1 0
#define G_2 1
#define G_3 2
#define B_1 3
#define B_2 4
#define B_3 5
#define R_1 6
#define R_2 7
#define R_3 8

#define NUMBER_OF_PINS 9

byte outPins[NUMBER_OF_PINS] = {3,4,5,6,7,8,9,10};

byte pot1 = 5;
byte pot2 = 4;

byte currentState = 0; //avoid flickering while in a state

int x = 0;
int y = 0;

void setup()
{
for(byte i=0; i<NUMBER_OF_PINS; i++){
pinMode(outPins*, OUTPUT);*

• }*

• pinMode(pot1,INPUT);*

• pinMode(pot2,INPUT);*
}
void loop()
{

• x = x/4;*

• y = y/4;*

• //GET SET STATE*

• if (x <= 85 && y <= 85)*

• {*

• currentState=G_1;*

• digitalWrite(outPins[G_1], HIGH);*

• }*

• else if ((x <= 85) && (y > 85 && y <= 170))*

• {*

• currentState=G_2;*

• digitalWrite(outPins[G_2], HIGH);*

• }*

• else if ((x <= 85) && (y > 170 && y <= 255))*

• {*

• currentState=G_3;*

• digitalWrite(outPins[G_3], HIGH);*

• }*

• else if ((x > 85 && x <= 170) && (y <= 85))*

• {*

• currentState=B_1;*

• digitalWrite(outPins[B_1], HIGH);*

• }*

• else if ((x > 85 && x <= 170) && (y > 85 && y <= 170))*

• {*

• currentState=B_2;*

• digitalWrite(outPins[B_2], HIGH);*

• }*

• else if ((x > 85 && x <= 170) && (y > 170 && y <= 255))*

• {*

• currentState=B_3;*

• digitalWrite(outPins[B_3], HIGH);*

• }*

• else if ((x > 170 && x <= 255) && (y <= 85)) *

• {*

• currentState=R_1;*

• digitalWrite(outPins[R_1], HIGH);*

• }*

• else if ((x > 170 && x <= 255) && (y > 85 && y <= 170))*

• {*

• currentState=R_2;*

• digitalWrite(outPins[R_2], HIGH);*

• }*

• else if ((x > 170 && x <= 255) && (y > 170 && y <= 255))*

• {*

• currentState=R_3;*

• digitalWrite(outPins[R_3], HIGH);*

• }*

• //set all other than current state led off*

• for ( byte i=0; i<NUMBER_OF_PINS; i++ ){*
_ if ( i!=currentState ){ digitalWrite(outPins*, LOW); }_
_
}_
_
}_
_
[/quote]*_

ok, g_2, g_3, and r_3 don't work, but i have no idea why

Not sure why some states are not working but here is a further simplification along the lines of AlphaBeta’s neat suggestion

void loop()                     // run over and over again
{
x = x/4;
y = y/4;

//GET SET STATE
if (x <= 85)
{
if(y <= 85)
{
currentState=G_1;
digitalWrite(outPins[G_1], HIGH);
}
else if ((y > 85 && y <= 170))
{
currentState=G_2;
digitalWrite(outPins[G_2], HIGH);
}
else if (y > 170)
{
currentState=G_3;
digitalWrite(outPins[G_3], HIGH);
}
}
else if (x < 170)
{
if (y <= 85)
{
currentState=B_1;
digitalWrite(outPins[B_1], HIGH);
}
else if (y > 85 && y <= 170)
{
currentState=B_2;
digitalWrite(outPins[B_2], HIGH);
}
else
{
currentState=B_3;
digitalWrite(outPins[B_3], HIGH);
}
}
else
{
if(y <= 85)
{
currentState=R_1;
digitalWrite(outPins[R_1], HIGH);
}
else if (y > 85 && y <= 170)
{
currentState=R_2;
digitalWrite(outPins[R_2], HIGH);
}
else
{
currentState=R_3;
digitalWrite(outPins[R_3], HIGH);
}
}
}

ok, here’s the working code, out together from parts of both of those contributions. I think the reason it wasn’t working before was that AlphaBeta left a pin out of the array list.

I’m new to coding, and if anyone could give me a brief explanation of why this works, it would be so appreciated.

#define G_1 0
#define G_2 1
#define G_3 2
#define B_1 3
#define B_2 4
#define B_3 5
#define R_1 6
#define R_2 7
#define R_3 8

#define NUMBER_OF_PINS 9

byte outPins[NUMBER_OF_PINS] = {3,4,5,6,7,8,9,10,11};

byte pot1 = 5;
byte pot2 = 4;

byte currentState = 0; //avoid flickering while in a state

int x = 0;
int y = 0;

void setup()
{
for(byte i=0; i<NUMBER_OF_PINS; i++){
pinMode(outPins*, OUTPUT);*
}
}
void loop() // run over and over again
{

• x = x/4;*
• y = y/4;*
• //GET SET STATE*
• if (x <= 85) *
• {*
• if(y <= 85)*
• {*
• currentState=G_1;*
• digitalWrite(outPins[G_1], HIGH);*
• }*
• else if ((y > 85 && y <= 170))*
• {*
• currentState=G_2;*
• digitalWrite(outPins[G_2], HIGH);*
• }*
• else if (y > 170)*
• {*
• currentState=G_3;*
• digitalWrite(outPins[G_3], HIGH);*
• }*
• }*
• else if (x < 170)*
• {*
• if (y <= 85)*
• {*
• currentState=B_1;*
• digitalWrite(outPins[B_1], HIGH);*
• }*
• else if (y > 85 && y <= 170)*
• {*
• currentState=B_2;*
• digitalWrite(outPins[B_2], HIGH);*
• }*
• else*
• {*
• currentState=B_3;*
• digitalWrite(outPins[B_3], HIGH);*
• }*
• }*
• else*
• {*
• if(y <= 85) *
• {*
• currentState=R_1;*
• digitalWrite(outPins[R_1], HIGH);*
• }*
• else if (y > 85 && y <= 170)*
• {*
• currentState=R_2;*
• digitalWrite(outPins[R_2], HIGH);*
• }*
• else*
• {*
• currentState=R_3;*
• digitalWrite(outPins[R_3], HIGH);*
• }*
• }*

//set all other than current state led off
for ( byte i=0; i<NUMBER_OF_PINS; i++ ){
_ if ( i!=currentState ){ digitalWrite(outPins*, LOW);_
_
}_
_
}_
_
}[/quote]*_

I’m new to coding, and if anyone could give me a brief explanation of why this works, it would be so appreciated.

I’ve commented the code a bit for you

/*
|| 3*3 led matrix
|| Controlled by two pots
||
|| Original program:
|| WeirdyBeardy
||
|| Contributed:
|| Alexander Brevig
|| Mem
*/

//declare some constant variables using the preprocessor #define directive
#define G_1 0
#define G_2 1
#define G_3 2
#define B_1 3
#define B_2 4
#define B_3 5
#define R_1 6
#define R_2 7
#define R_3 8

#define NUMBER_OF_PINS 9

//datatype byte

//this arranges your pins as an array
byte outPins[NUMBER_OF_PINS] = {3,4,5,6,7,8,9,10,11};

byte pot1 = 5;
byte pot2 = 4;

//store current ‘position’ in order to avoid flickering while in a state
byte currentState = 0;

//variables that holds each pot value
byte x = 0;
byte y = 0;

void setup()
{
//this loops through the array of pins with the use of a for loop
//sets all pins as output
for(byte i=0; i<NUMBER_OF_PINS; i++){
pinMode(outPins*, OUTPUT);*

• }*
}
void loop()// run over and over again
{
• x = analogRead(pot1) / 4;*
• y = analogRead(pot2) / 4;*
• //use if … else if … else control structure to set the correct currentState variable*
• if (x <= 85) //currentState is in the 1st col [(255/3) == 85]*
• {*
• if(y <= 85) //currentState is in the 1st row*
• {*
• currentState=G_1;*
• digitalWrite(outPins[G_1], HIGH);*
• }*
• else if ((y > 85 && y <= 170)) //currentState is in the 2nd row*
• {*
• currentState=G_2;*
• digitalWrite(outPins[G_2], HIGH);*
• }*
• else if (y > 170) //currentState is in the 3rd row*
• {*
• currentState=G_3;*
• digitalWrite(outPins[G_3], HIGH);*
• }*
• }*
• else if (x < 170) //currentState is in the 2nd col [(255/3)+(255/3) == 170]*
• {*
• if (y <= 85) //currentState is in the 1st row*
• {*
• currentState=B_1;*
• digitalWrite(outPins[B_1], HIGH);*
• }*
• else if (y > 85 && y <= 170) //currentState is in the 2nd row*
• {*
• currentState=B_2;*
• digitalWrite(outPins[B_2], HIGH);*
• }*
• else //currentState is in the 3rd row*
• {*
• currentState=B_3;*
• digitalWrite(outPins[B_3], HIGH);*
• }*
• }*
• else //currentState is in the 3rd col*
• {*
• if(y <= 85)//currentState is in the 1st row*
• {*
• currentState=R_1;*
• digitalWrite(outPins[R_1], HIGH);*
• }*
• else if (y > 85 && y <= 170) //currentState is in the 2nd row*
• {*
• currentState=R_2;*
• digitalWrite(outPins[R_2], HIGH);*
• }*
• else //currentState is in the 3rd row*
• {*
• currentState=R_3;*
• digitalWrite(outPins[R_3], HIGH);*
• }*
• }*
• //this loops through the array of pins with the use of a for loop*
• //set all other than currentState led off*
• for ( byte i=0; i<NUMBER_OF_PINS; i++ ){*
_ if ( i!=currentState ){ digitalWrite(outPins*, LOW);_
_
}*_

}
[/quote]

Man, thank you guys so much for all the help. ok, so i pretty much understand all the parts of the comments, none of that was really news, except i don't really know preprocessor commands or how they work, and i haven't used the byte type. what i don't understand is how "currentState" is being used to control what pin is on, without a for loop or an array formula or anything else that i can see being used to define how currentState is used once defined. but this is the code I knew was out there, but didn't know how to write. thanks so much

what i don’t understand is how “currentState” is being used to control what pin is on

Ehrn one of the pins are set HIGH, that pin number/index is being stored.

Your original program sat all pin to its state for all conditions made, this new program split that up into two instructions:

[1] Find the pin to set high, and do so
[2] Set all other pins low

Find the pin to set high, and do so:
Let’s pretend that this evaluates true, then pin outPins[R_2] == 10 is set HIGH

else if (y > 85 && y <= 170) //3rd col 2rd row
{
currentState=R_2;
digitalWrite(outPins[R_2], HIGH);
}

Set all other pins low:

//set all other than current state led off
for ( byte i=0; i<NUMBER_OF_PINS; i++ ){
//if current index is not equal to the current pin that just got HIGH ;) set it LOW
if ( i!=currentState ){ digitalWrite(outPins[i], LOW);
}