/* Light up all the LED's in the matrix to test them Nathan Clark */ int delayPin = 1; // delay before it turns the LED off int row[] = {10,11,12,13,A5,A4,A3,A0}; // These are the Cathodes - Send LOW to turn on LED int col[] = {2,3,4,5,6,7,8,9}; // These are the Anodes - Send HIGH to turn on LED void (*funcAnimate[2])(); // Sets up an array of functions we can call int keyboardPin = A2; // Analog input pin that the keypad is attached to int keyboardValue = 0; // value read from the keyboard int keypressed = 0; // Keypressed on keypad int manualEffect = 0; // manual effects in action int prevKeyPressed = 0; // Set this variable to the keypressed so we can continue to play the selected animation void setup() { for (int thisPin = 0; thisPin < 8; thisPin++) { pinMode(col[thisPin], OUTPUT); // Set selected pin to be an output rather then an input pin pinMode(row[thisPin], OUTPUT); // this is so we cna provide +5v or GND at any pin digitalWrite(row[thisPin], HIGH); // Set all cathodes to have positive voltage at them. Hence LED will not light up irrespective of what the anode is set to } funcAnimate[0] = dummy; funcAnimate[1] = MakeSadSmileFace; funcAnimate[2] = MakewinkingFace; } void loop() { int randnumber = 0; readkeyboard(); if (keypressed > 0 && keypressed < 12) { manualEffect = 1; prevKeyPressed = keypressed; for (int x=0;x<2;x++) { (*funcAnimate[keypressed])(); delay(1); } } else if (keypressed = 12) { manualEffect = 0; // Let the loop run through again and the final else statement will then run a random effect // as the manualEffect has been reset } else if (manualEffect = 1) { for (int x=0;x<2;x++) { (*funcAnimate[prevKeyPressed])(); delay(1); } } else { randnumber = random(1,3); for (int x=0;x<2;x++) { (*funcAnimate[randnumber])(); delay(1); } } } void dummy() {} void resetLEDS() { for (int thisPin = 0; thisPin < 8; thisPin++) { digitalWrite(row[thisPin], HIGH); // Set all cathodes to have positive voltage at them. Hence LED will not light up irrespective of what the anode is set to digitalWrite(col[thisPin], LOW); } } void onLED(int colnum,int rownum) { digitalWrite(col[colnum], HIGH); digitalWrite(row[rownum], LOW); delay(delayPin); digitalWrite(col[colnum], LOW); digitalWrite(row[rownum], HIGH); } void drawSmileyFace() { onLED(0,0); onLED(1,1); onLED(0,1); onLED(1,0); onLED(6,0); onLED(7,1); onLED(6,1); onLED(7,0); onLED(3,3); onLED(4,3); onLED(3,2); onLED(4,2); onLED(0,5); onLED(1,6); onLED(2,7); onLED(3,7); onLED(4,7); onLED(5,7); onLED(6,6); onLED(7,5); } void drawSmileyFaceWink() { onLED(0,0); onLED(1,1); onLED(0,1); onLED(1,0); onLED(3,3); onLED(4,3); onLED(3,2); onLED(4,2); onLED(0,5); onLED(1,6); onLED(2,7); onLED(3,7); onLED(4,7); onLED(5,7); onLED(6,6); onLED(7,5); } void DrawSadFace() { onLED(0,0); onLED(1,1); onLED(0,1); onLED(1,0); onLED(6,0); onLED(7,0); onLED(6,1); onLED(7,1); onLED(3,3); onLED(4,3); onLED(3,2); onLED(4,2); onLED(0,7); onLED(1,6); onLED(2,5); onLED(3,5); onLED(4,5); onLED(5,5); onLED(6,6); onLED(7,7); } void MakewinkingFace() { for (int x=0;x<40;x++) { drawSmileyFace(); delay(1); } for (int x=0;x<40;x++) { drawSmileyFaceWink(); delay(1); } } void MakeSadSmileFace() { for (int x=0;x<40;x++) { drawSmileyFace(); delay(1); } for (int x=0;x<40;x++) { DrawSadFace(); delay(1); } } void readkeyboard(){ keypressed = 0; keyboardValue = analogRead(keyboardPin); // read the value (0-1023) // delay(50); //Serial.print(" Value Pressed "); //Serial.println(keyboardValue); if (keyboardValue <25) {keypressed = 0;} // no key pressed if ((keyboardValue >25) && (keyboardValue < 45)) {keypressed = 1;} if ((keyboardValue >45) && (keyboardValue < 80)) {keypressed = 2;} if ((keyboardValue >80) && (keyboardValue < 160)) {keypressed = 3;} if ((keyboardValue >162) && (keyboardValue < 253)){keypressed = 4;} if ((keyboardValue >253) && (keyboardValue < 361)){keypressed = 5;} if ((keyboardValue >361) && (keyboardValue < 479)){keypressed = 6;} if ((keyboardValue >479) && (keyboardValue < 585)){keypressed = 7;} if ((keyboardValue >585) && (keyboardValue < 715)){keypressed = 8;} if ((keyboardValue >715) && (keyboardValue < 760)){keypressed = 9;} if ((keyboardValue >839) && (keyboardValue < 880)){keypressed = 10;} // 0 if ((keyboardValue >760) && (keyboardValue < 839)){keypressed = 11;} // * if ((keyboardValue >880) && (keyboardValue < 1000)){keypressed = 12;} // # delay(50); }