/* * last modified on: 8:13pm, april 25th, 2018 * * custom libraries used include: LedControl.h (found online @ https://github.com/wayoda/LedControl.git) and Pitches.h (as used in class) * * this program makes a ball bounce around the 8x8 matrix screen 1 the paddle is controlled with the rotation dial, the user must move the paddle to prevent the ball from travelling off the bottom of the screen every successful volley increments the score which is displayed on screen 2 after a score of 9 is reached, a celebratory lightshow occurs */ #include #include "pitches.h"; int DIN = 7; int CS = 1; int CLK = 0; int rotationPin = A0; int buzzerPin = 5; int RGBRedPin = 9; //The red RGB LED is connected pin 9 of the Arduino. int RGBGreenPin = 10; //The green RGB LED is connected pin 10 of the Arduino. int RGBBluePin = 11; //The blue RGB LED is connected pin 11 of the Arduino. byte smile[8]= {0x3C,0x42,0xA5,0x81,0xA5,0x99,0x42,0x3C}; byte frown[8]= {0x3C,0x42,0xA5,0x81,0x99,0xA5,0x42,0x3C}; byte zero[8] = {0x3C,0x42,0x81,0x81,0x81,0x81,0x42,0x3C}; byte one[8] = {0x18,0x28,0x48,0x08,0x08,0x08,0x08,0x7E}; byte two[8] = {0x1C,0x22,0x42,0x02,0x04,0x18,0x20,0x3E}; byte three[8] = {0x1C,0x22,0x02,0x04,0x38,0x04,0x02,0x3C}; byte four[8] = {0x06,0x0A,0x12,0x22,0x7E,0x02,0x02,0x02}; byte five[8] = {0x00,0x3E,0x40,0x40,0x5C,0x62,0x02,0x3C}; byte six[8] = {0x00,0x38,0x44,0x40,0x7C,0x44,0x44,0x3C}; byte seven[8] = {0x00,0xFE,0x04,0x08,0xFF,0x20,0x40,0x80}; byte eight[8] = {0x3C,0x42,0x81,0x81,0x7E,0x81,0x81,0x7E}; byte nine[8] = {0x00,0x7C,0x44,0x44,0x7C,0x04,0x04,0x7C}; int ballXPos = random(1,6); //dont let the ball have an initial X position of 0 or 7, or else it might fly offscreen int ballYPos = 6; int paddlePos = 0; int score = 0; //use boolean values to indicate direction of moving ball so that they can be changed easily using 'not' boolean yDir = false; boolean xDir = false; int timeDelay = 275; //change this value to control difficulty, values in between 250 and 350 seem to work best LedControl lc=LedControl(DIN,CLK,CS,2); void setup() { // put your setup code here, to run once: lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup lc.setIntensity(0,15); // Set the brightness to maximum value lc.clearDisplay(0); // and clear the display lc.shutdown(1,false); //The MAX72XX is in power-saving mode on startup lc.setIntensity(1,15); // Set the brightness to maximum value lc.clearDisplay(1); // and clear the display pinMode(RGBRedPin, OUTPUT); //Setup red RGB LED pin as an output pin. pinMode(RGBGreenPin, OUTPUT); //Setup green RGB LED pin as an output pin. pinMode(RGBBluePin, OUTPUT); //Setup blue RGB LED pin as an output pin. } void loop() { // map the rotationPin analog value to 0->6 (6 instead of 7 because the paddle is two lights wide) paddlePos = map(analogRead(rotationPin),0,1023,0,6); //display the ball on the first screen lc.setLed(0,ballXPos,ballYPos,1); //display the paddle on the first screen lc.setLed(0,paddlePos,0,1); lc.setLed(0,paddlePos+1,0,1); delay(timeDelay); //remove the ball's old position lc.setLed(0,ballXPos,ballYPos,0); //remove the paddle's old position lc.setLed(0,paddlePos,0,0); lc.setLed(0,paddlePos+1,0,0); //update the ball's position based on its current directions moveBallX(xDir); moveBallY(yDir); //if ball hits the sides, changes its x direction to the opposite if ((ballXPos == 0 or ballXPos ==7) and ballYPos != 1){ xDir = not xDir; tone(buzzerPin,NOTE_A4,timeDelay); } //if ball hits the top, change its y direction to the opposite if (ballYPos == 7){ yDir = not yDir; tone(buzzerPin,NOTE_A4,timeDelay); } //if ball hits the paddle, reverse its y direction if (ballYPos == 1 && (ballXPos == paddlePos || ballXPos == (paddlePos+1))){ yDir = not yDir; score++; tone(buzzerPin,NOTE_B4,timeDelay); } //if the ball hits the corner of the paddle, reverse its x and y direction if (ballYPos == 1 && (ballXPos == paddlePos - 1) && not xDir and ballXPos != 0){ yDir = not yDir; xDir = not xDir; score++; tone(buzzerPin,NOTE_C4,timeDelay); } if (ballYPos == 1 && (ballXPos == paddlePos + 2) && xDir and ballXPos != 7){ yDir = not yDir; xDir = not xDir; score++; tone(buzzerPin,NOTE_C4,timeDelay); } //if ball hits the side while the paddle is one space away, reverse its x and y direction if (ballYPos == 1 && ballXPos == 7 && paddlePos == 5){ yDir = not yDir; xDir = not xDir; tone(buzzerPin,NOTE_D4,timeDelay); score++; } if (ballYPos == 1 && ballXPos == 0 && paddlePos == 1){ yDir = not yDir; xDir = not xDir; tone(buzzerPin,NOTE_D4,timeDelay); score++; } //if ball goes offscreen, print a frowning face and play a sad sound if (ballYPos < -1){ printByte(frown,0); tone(buzzerPin,NOTE_G4,450); delay(450); tone(buzzerPin,NOTE_FS4,450); delay(450); tone(buzzerPin,NOTE_F4,600); delay(600); lc.clearDisplay(0); ballYPos = 7; ballXPos = random(6); score=0; } switch (score){ case 0: lc.clearDisplay(1); printByte(zero,1); break; case 1: lc.clearDisplay(1); printByte(one,1); break; case 2: lc.clearDisplay(1); printByte(two,1); break; case 3: lc.clearDisplay(1); printByte(three,1); break; case 4: lc.clearDisplay(1); printByte(four,1); break; case 5: lc.clearDisplay(1); printByte(five,1); break; case 6: lc.clearDisplay(1); printByte(six,1); break; case 7: lc.clearDisplay(1); printByte(seven,1); break; case 8: lc.clearDisplay(1); printByte(eight,1); break; case 9: lc.clearDisplay(1); printByte(nine,1); lc.clearDisplay(0); printByte(smile,0); for (int i=0;i<3;i++){ lightShow(); } score=0; delay(timeDelay*4); lc.clearDisplay(0); ballXPos = random(1,6); ballYPos = 6; yDir = false; xDir = false; break; } } //a function to move the ball in the X direction based on which way it is travelling void moveBallX(boolean dir){ if (dir == false){ ballXPos++; } else{ ballXPos--; } } //a function to move the ball in the Y direction based on which way it is travelling void moveBallY(boolean dir){ if (dir == false){ ballYPos++; } else{ ballYPos--; } } //the printByte functions takes an array of hex values and an integer as parameters, the integer tells which screen to display the byte void printByte(byte character [], int screen) { int i = 0; for(i=0;i<8;i++) { lc.setRow(screen,i,character[i]); } } void lightShow() { digitalWrite(RGBRedPin, HIGH); //Turn on RED delay(timeDelay); //Wait for 2 seconds digitalWrite(RGBRedPin, LOW); //Turn off Red //Display Green digitalWrite(RGBGreenPin, HIGH); //Turn on RED delay(timeDelay); //Wait for 2 seconds digitalWrite(RGBGreenPin, LOW); //Turn off Red //Display Blue digitalWrite(RGBBluePin, HIGH); //Turn on RED delay(timeDelay); //Wait for 2 seconds digitalWrite(RGBBluePin, LOW); //Turn off Red //Display Magenta (Red + Blue) digitalWrite(RGBRedPin, HIGH); //Turn on RED digitalWrite(RGBBluePin, HIGH); //Turn on RED delay(timeDelay); //Wait for 2 seconds digitalWrite(RGBRedPin, LOW); //Turn off Red digitalWrite(RGBBluePin, LOW); //Turn on RED //Display Yellow (Red + Green) digitalWrite(RGBRedPin, HIGH); //Turn on RED digitalWrite(RGBGreenPin, HIGH); //Turn on RED delay(timeDelay); //Wait for 2 seconds digitalWrite(RGBRedPin, LOW); //Turn off Red digitalWrite(RGBGreenPin, LOW); //Turn of RED //Display Cyan (Blue + Green) digitalWrite(RGBBluePin, HIGH); //Turn on BLUE digitalWrite(RGBGreenPin, HIGH); //Turn on GREEN delay(timeDelay); //Wait for 2 seconds digitalWrite(RGBBluePin, LOW); //Turn of BLUE digitalWrite(RGBGreenPin, LOW); //Turn off GREEN //Display White (Red + Blue + Green) digitalWrite(RGBRedPin, HIGH); //Turn on RED digitalWrite(RGBBluePin, HIGH); //Turn on BLUE digitalWrite(RGBGreenPin, HIGH); //Turn on GREEN delay(timeDelay); //Wait for 2 seconds digitalWrite(RGBRedPin, LOW); //Turn off RED digitalWrite(RGBBluePin, LOW); //Turn off BLUE digitalWrite(RGBGreenPin, LOW); //Turn off GREEN }