//LED Matrix POV control //Ben Katz, June 15, 2011 //www.build-its.blogspot.com int row[] = {5, 6, 7}; int col[] = {2, 3, 4}; int row1[] = {-1, -1, -1}; //This set of array's represents the grid of LED's. The program checks through these lists to se int row2[] = {-1, -1, -1}; int row3[] = {-1, -1, -1}; //int light[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; int counter = 0; int ledpins[] = {0,0}; int *ptr; void setup(){ for(int i = 2 ; i < 8 ; i++){ //Initializes all the pins pinMode(i, OUTPUT); digitalWrite(i, HIGH); } Serial.begin(9600); } void loop(){ animate(); readInput(); } void animate(){ //The 'animate' function takes the arrays for each row of LED's and uses them to light one row of LED's at a time for (int r = 0 ; r < 3 ;r ++){ switch(r){ //The switch tells the function which row to read case(0): ptr = &row1[0]; break; case(1): ptr = &row2[0]; break; case(2): ptr = &row3[0]; break; } for (int i = 0 ; i < 3 ; i ++){ if (*(ptr + i) == 1){ digitalWrite(i + 2, HIGH); } else{ digitalWrite(i + 2, LOW); } } if (row[r] != 5){ //Rows are lit by setting the ground pin to 'LOW' and the positiv pins to 'HIGH' digitalWrite(row[r]-1, HIGH); } else{ digitalWrite(row[2], HIGH); } digitalWrite(row[r], LOW); delay(5); // You can increase the delay if you want to see how the LED's are cycled. 100 is a good value for demonstrating } } int updateFrame(int led){ //This function updates arrays 'row1', 'row2', and 'row3' according to the input int counter = 0; // input is in the form if an integer, from 0-8, corrensponding to the 9 LED's for (int y = 0 ; y < 3 ; y++){ //The for loops convert the integer input to the LED's coordinates, from (0,0) to (8,8) for (int x = 0 ; x < 3 ; x ++){ if(counter == led){ int ledpins[] = {x, y}; switch(y){ case 0: row1[x] = row1[x] * -1; return 1; case 1: row2[x] = row2[x] * -1; return 1; case 2: row3[x] = row3[x] * -1; return 1; } counter ++; } counter ++; } } } void readInput(){ //This function interprets the serial input and, depending on the input, passes it through the function 'updateFrame' if (Serial.available() > 0) { int input = Serial.read(); if(input == 33){ //Inputting '!' (ascii 33) resets all the LED's to "off" for(int x = 0 ; x < 3 ; x++){ row1[x] = -1; row2[x] = -1; row3[x] = -1; } } else{ updateFrame(int((input)) - 48); } //Serial.println(input); } }