#define PIN_R_X_1 (2) //0 is on, 1 is off #define PIN_R_X_2 (1) #define PIN_R_X_3 (0) #define PIN_W_X_1 (5) #define PIN_W_X_2 (4) #define PIN_W_X_3 (3) #define PIN_Y_1 (6) #define PIN_Y_2 (7) #define PIN_Y_3 (8) #define WIDTH (3) #define HEIGHT (3) #define ALL_OFF (0) #define RED_LED_ON (1) #define WHITE_LED_ON (2) #define BOTH_LEDS_ON (3) int state[WIDTH][HEIGHT] = {{ALL_OFF,ALL_OFF,ALL_OFF},{ALL_OFF,ALL_OFF,ALL_OFF},{ALL_OFF,ALL_OFF,ALL_OFF}}; int x_r_pins[WIDTH] = {PIN_R_X_1, PIN_R_X_2, PIN_R_X_3}; int x_w_pins[WIDTH] = {PIN_W_X_1, PIN_W_X_2, PIN_W_X_3}; int y_pins[HEIGHT] = {PIN_Y_1, PIN_Y_2, PIN_Y_3}; ////////////// LED FUNCTIONS ////////////// void turnOnRedColumn(unsigned int x){ //all leds with coordinate 'x' form a column at x-coordinate 'x' digitalWrite(x_r_pins[x], HIGH); } void turnOffRedColumn(unsigned int x){ digitalWrite(x_r_pins[x], LOW); } void turnOnWhiteColumn(unsigned int x){ digitalWrite(x_w_pins[x], HIGH); } void turnOffWhiteColumn(unsigned int x){ digitalWrite(x_w_pins[x], LOW); } void turnOnRow(unsigned int y){ //all leds with coordinate 'y' form a row at y-coordinate 'y' digitalWrite(y_pins[y], LOW); } void turnOffRow(unsigned int y){ digitalWrite(y_pins[y], HIGH); } void showLeds(){ for(unsigned int y=0; y < HEIGHT; y++){ turnOnRow(y); for(unsigned int x=0; x < WIDTH; x++){ switch(state[x][y]){ case WHITE_LED_ON: turnOffRedColumn(x); turnOnWhiteColumn(x); break; case RED_LED_ON: turnOnRedColumn(x); turnOffWhiteColumn(x); break; case BOTH_LEDS_ON: turnOnRedColumn(x); turnOnWhiteColumn(x); break; case ALL_OFF: //same as default, so just don't break and it will execute the default on the next line default: //turn off leds turnOffRedColumn(x); turnOffWhiteColumn(x); } } delay(1); turnOffRow(y); for(unsigned int x=0; x < WIDTH; x++){ turnOffRedColumn(x); turnOffWhiteColumn(x); } } } void clearField(){ for(unsigned int x=0; x < WIDTH; x++){ for(unsigned int y=0; y < HEIGHT; y++){ state[x][y] = ALL_OFF; } } } void setup() { for(unsigned int x=0; x < WIDTH; x++){ pinMode(x_r_pins[x], OUTPUT); pinMode(x_w_pins[x], OUTPUT); } for(unsigned int y=0; y < HEIGHT; y++){ pinMode(y_pins[y], OUTPUT); } } int counter=0; int x_t=0; int y_t=0; bool turnOn = true; void loop() { showLeds(); counter++; if(counter>100){ counter = 0; if(turnOn){ state[x_t][y_t] &= ~WHITE_LED_ON; state[x_t][y_t] |= RED_LED_ON; } else { state[x_t][y_t] |= WHITE_LED_ON; state[x_t][y_t] &= ~RED_LED_ON; } y_t++; if(y_t == HEIGHT){ y_t = 0; x_t++; if(x_t == WIDTH){ x_t = 0; turnOn = !turnOn; } } } }