// Create a 'rain' animation: // Move the top line of LED values down 1 position every frame // Randomly turn on one of the top LED's // Hold the current frame in an array which will be used to // turn on each of the LED's in the Row Column scan MATRIX int matrix[4][4]; //Hold all the current frame data in here //Anodes start at p2 and go to p5 int a1=2; //x int a2=3; //x int a3=4; //x int a4=5; //x //Cathodes start at p8 and go to p11 int c1=8; //y int c2=9; //y int c3=10; //y int c4=11; //y int frame = 1; long oldTime = 0; long currentTime = 0; long timeOut = 100; //100 milliseconds int oldX = 0; int newX = 0; void setup() { for (int i=0; i<=3;i++) { pinMode(i+2,OUTPUT); // the anodes start at 2 pinMode(i+8,OUTPUT); // the cathodes start at 8 } // Initialize all the values in the 2D array to 0 for (int i=0;i<4;i++) { for (int j=0;j<4;j++) { matrix[i][j] = 0; } } Serial.begin(9600); //Create a random seed at the start -> use the analogRead to help randomSeed(analogRead(0)); //init the timer values currentTime=millis(); oldTime = currentTime; Serial.println("STARTED"); } void loop() { currentTime = millis(); if (currentTime-oldTime >= timeOut) { //drag the leds down. oldTime = currentTime; for (int y = 3; y>0; y--) { for (int x = 0; x<4; x++) { matrix[x][y] = matrix[x][y-1]; } } for (int x=0;x<4;x++) { matrix[x][0] = 0; } oldX = newX; while(oldX == newX) { newX = random(4); if (newX > 3) newX = 3; } matrix[newX][0] = 1; } displayMatrix(); } void displayMatrix() { for (int x=0;x<4;x++) { for (int y=0;y<4;y++) { if (matrix[x][y] == 1) { off(); digitalWrite(x+2,HIGH); digitalWrite(y+8,LOW); } } } } void off() { for (int i=0;i<4;i++) { digitalWrite(i+2,LOW); digitalWrite(i+8,HIGH); } }