//the curcuit is a 5 by 3 led matrix
//all of the cathodes in a column and all of the anodes in a row are connected together to pins
#define column_num 3 //number of columns
#define row_num 5 //number of rows
int column_pin[column_num] = {2, 3, 4};//cathodes in a column conected to these pin
int row_pin[row_num] = {5, 6, 7, 8, 9};//anodes in a row connected to these pins
int column_pot = A1;
int row_pot = A0;
int row, column;//holds the index for row and column
// variables for function
int count = 0;
float division, comp_value;
void setup() {
for (int i = 0; i < row_num; i++) {
pinMode(row_pin[i], OUTPUT);
}
for (int i = 0; i < column_num; i++) {
pinMode(column_pin[i], OUTPUT);
}
pinMode(row_pot, INPUT);
pinMode(column_pot, INPUT);
}
//lights the led specified by row and column
void show(int row, int column) {
//each led's anode is connected to row_pin and cathode is connected to column_pin
//for it to light up the row_pin must be high and column_pin must be low
for (int i = 0; i < row_num; i++) {
//loop through the rows and only set the specified pin high
if (i == row) {
digitalWrite(row_pin[i], HIGH);
}
else {
//set all other pins to low
digitalWrite(row_pin[i], LOW);
}
}
for (int i = 0; i < column_num; i++) {
//loop through the colum and set the specified column low
if (i == column) {
digitalWrite(column_pin[i], LOW);
}
else {
//set all other pins to high
digitalWrite(column_pin[i], HIGH);
}
}
}
//retruns a value for row or column based on the analog value given
int give_position(int value, float num) {
//number of rows or column is also needed to divide the pot into appropriate sections
count = 0;
division = 1023 / num;//here 1023 is divided
comp_value = division;//comparison value
while (comp_value != 1023) {
//add the comp_value by division and maintain count until we get to 1023
//if comp_value is greater than analog value return the number of time division had to be added (count)
if (comp_value >= value) {
return count;
}
comp_value += division;
count++;
}
}
void loop() {
row = give_position(analogRead(row_pot), row_num);
column = give_position(analogRead(column_pot), column_num);
show(row, column);
}