Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Modified Pong for Star Wars by GJ Schaefer
- #include "LedControl.h"
- byte MSG[8*20];
- byte SPACE[8] = {
- B00000000,
- B00000000,
- B00000000,
- B00000000,
- B00000000,
- B00000000,
- B00000000,
- B00000000
- };
- byte T[8] = { //TIE FIGHTER
- B00000000,
- B10000001,
- B10011001,
- B11100111,
- B10011001,
- B10000001,
- B00000000,
- B00000000
- };
- byte L[8] = { //LASER PATTERN
- B00000000,
- B00100000,
- B00100100,
- B00000100,
- B00000000,
- B00010000,
- B00010000,
- B00010000
- };
- byte B[8] = { //BLASTER PATTERN
- B00000000,
- B00000010,
- B00000010,
- B00000000,
- B00100000,
- B00000000,
- B00000000,
- B00000000
- };
- byte XW[8] = { //X-WING PATTERN
- B00000000,
- B00011000,
- B00011000,
- B00011000,
- B10011001,
- B10100101,
- B11111111,
- B00011000
- };
- byte P[8] = { //Tie Fighter small PATTERN
- B10010000,
- B11110000,
- B10010000,
- B00000000,
- B00000000,
- B00001010,
- B00001110,
- B00001010
- };
- byte E[8] = {
- B00000000,
- B00111100,
- B00100100,
- B00100000,
- B00111000,
- B00100000,
- B00100100,
- B00111100
- };
- byte R[8] = {
- B00000000,
- B00000010,
- B00000100,
- B00001000,
- B01010000,
- B00100000,
- B01010000,
- B10000000
- };
- byte K[8] = { //Death Star PATTERN
- B00111100,
- B01001110,
- B10010011,
- B10010011,
- B10001101,
- B10000001,
- B01000010,
- B00111100
- };
- byte W[8] = {
- B00000000,
- B01000000,
- B00100000,
- B00010000,
- B00001010,
- B00000100,
- B00001010,
- B00000001
- };
- byte J[8] = {
- B00000000,
- B00111100,
- B00001000,
- B00001000,
- B00001000,
- B00001000,
- B00101000,
- B00111000
- };
- byte I[8] = {
- B00000000,
- B00111100,
- B00001000,
- B00001000,
- B00001000,
- B00001000,
- B00001000,
- B00111100
- };
- byte D[8] = {
- B00000000,
- B00110000,
- B00101000,
- B00100100,
- B00100100,
- B00100100,
- B00100100,
- B00111000
- };
- int PongByPass = 0;
- int VaderMusic = 0;
- int ledPin = 13;
- int speakerPin = 7;//turn the 2 to 7 to turn on speaker !!!
- int blueled = 2;
- int greenled = 8;
- int redled = 9;
- #define N_c 261
- #define N_d 294
- #define N_e 329
- #define N_f 349
- #define N_g 391
- #define N_gS 415
- #define N_a 440
- #define N_aS 455
- #define N_b 466
- #define N_cH 523
- #define N_cSH 554
- #define N_dH 587
- #define N_dSH 622
- #define N_eH 659
- #define N_fH 698
- #define N_fSH 740
- #define N_gH 784
- #define N_gSH 830
- #define N_aH 880
- int step_col;
- int step_row;
- int subidas;
- boolean showBars_enabled = false;
- int row_bar = 0;
- LedControl LC=LedControl(12,11,10,2); // 2 = two LED Matric Modules
- //LedControl LC2=LedControl(speakerPin,6,5,1);
- int X = 4,OLD_X,sensor;
- int row,col;
- int sc,sr;
- int cnt = 0;
- int desarma = 0;
- boolean INTRO_SHOWED = false; // internal logic switch for intro movie to run
- int dly; // END OF INITIALIZATION ..........................................
- void setup() {
- Serial.begin(9600);
- pinMode(A0,INPUT);
- pinMode(3,INPUT);
- pinMode(5,INPUT);
- pinMode(speakerPin, OUTPUT);
- pinMode(blueled, OUTPUT);
- pinMode(greenled, OUTPUT);
- pinMode(redled, OUTPUT);
- LC.shutdown(0,false);
- LC.setIntensity(0,8);
- LC.clearDisplay(0);
- LC.shutdown(1,false);
- LC.setIntensity(1,4);
- LC.clearDisplay(1);
- randomSeed(analogRead(1)*millis());
- if(INTRO_SHOWED == false) // Run Star Wars Intro movie then clear screen with Lose flash
- {
- digitalWrite(greenled, HIGH);
- copyLetter(SPACE,0);
- copyLetter(SPACE,1);
- copyLetter(T,2);
- copyLetter(L,3);
- copyLetter(B,4);
- copyLetter(XW,5);
- copyLetter(SPACE,6);
- copyLetter(XW,7);
- copyLetter(L,8);
- copyLetter(B,9);
- copyLetter(T,10);
- copyLetter(SPACE,11);
- copyLetter(P,12);
- copyLetter(SPACE,13);
- copyLetter(K,14);
- showMSG(14,50);
- digitalWrite(greenled, LOW);
- INTRO_SHOWED = true;
- }
- lose();
- dly = 500;
- setLED(15,X,true);setLED(15,X+1,true);setLED(15,X+2,true);
- OLD_X = -1;
- subidas = 0;
- step_col = 1;
- step_row = 1;
- sc = step_col;
- sr = step_row;
- // row = -step_row;
- row = -1+random(3);
- col = random(8);
- showBars(false);
- }
- void ledflash() // Sequence if you do not want Pong Game Active.............
- {
- digitalWrite(blueled,HIGH);
- delay(500);
- digitalWrite(greenled,HIGH);
- digitalWrite(redled,LOW);
- delay(2000);
- digitalWrite(redled,HIGH);
- digitalWrite(greenled,LOW);
- delay(250);
- }
- void loop() //Core program routine checks paddle position and switch states...
- {
- sensor = analogRead(A0); //Read the pot for position of paddle
- VaderMusic = digitalRead(3); //Read state of music request pushbutton
- PongByPass = digitalRead(5); // Read state of Pong Bypass switch
- // This section below states the pong bypass and Vader March............
- if(VaderMusic == HIGH){
- digitalWrite(blueled, HIGH);winner();}
- if(PongByPass == HIGH)ledflash();
- // This section above is the bypass and march ..............................
- //Serial.println(sensor);
- X = map(sensor,0,980,1,6);
- // X = int(sensor / 127);
- // if(X < 1) X = 1;
- // if(X > 6) X = 6;
- if(X != OLD_X)
- {
- OLD_X = X;
- setROW(15,0x00);
- setLED(15,X-1,true);setLED(15,X,true);setLED(15,X+1,true);
- }
- if(cnt == 0)
- {
- setLED(row,col,false);
- digitalWrite(blueled, HIGH);
- if((subidas==1 || random(2)==1) && showBars_enabled == false && row == 0) {showBars(true);subidas=0;desarma=2+random(5);};
- if(subidas==desarma && showBars_enabled == true) {showBars(false);subidas=0;};
- if(col == 7) {sc = -step_col;tone(speakerPin,1000,20);digitalWrite(greenled, HIGH);};
- if(col == 0) {sc = step_col;tone(speakerPin,1000,20);};
- if(row == 0) {sr = step_row;subidas++;dly-=5;tone(speakerPin,1000,20);};
- if(dly <= 200) winner(); //Difficulty adjustment to become a winner
- if(showBars_enabled == true && sr > 0 && row < row_bar)
- {
- if(row == row_bar - 1) {sr=-step_row;tone(speakerPin,1000,20);digitalWrite(greenled, LOW);};
- }
- if(row == 14)
- {
- if(col >= X-1 && col <= X+1) {sr = -step_row;tone(speakerPin,1500,20);}
- else if(col == X-2 && sc > 0) {sr = -step_row; sc = -step_col;tone(speakerPin,1500,20);}
- else if(col == X+2 && sc < 0) {sr = -step_row; sc = step_col;tone(speakerPin,1500,20);};
- }
- row += sr;
- col += sc;
- if(col == 8) col = 7;
- if(col == -1) col = 0;
- setLED(row,col,true);
- if(row == 15) setup();
- }
- cnt++;
- if(cnt == dly) cnt = 0;
- }
- void setLED(int l,int c, boolean state)
- {
- int disp=0;
- if(l > 7) {l = l-8;disp=1;};
- LC.setLed(disp,l,c,state);
- }
- void setROW(int r,char ch)
- {
- int disp = 0;
- if(r > 7) {r = r - 8; disp=1;};
- LC.setRow(disp,r,ch);
- }
- void lose() //Ball misses the paddle
- {
- delay(80);
- for(row = 0;row<16;row++)
- {
- setROW(row,0xFF);
- tone(speakerPin,row*500,10);
- digitalWrite(redled, LOW);
- digitalWrite(blueled, HIGH);
- delay(20);
- }
- delay(30);//Time the full screen flashes on a miss of the ball
- for(row = 15;row>=0;row--)
- {
- setROW(row,0x00);
- tone(speakerPin,row*500,10);
- delay(30); //Time it takes to decay each row for thescreen flash on a missed ball.
- digitalWrite(blueled, LOW);
- }
- }
- void showBars(boolean show)
- {
- showBars_enabled = show;
- if(show) row_bar = 2+random(5);
- setROW(row_bar,(show == true ? 0xFF : 0x00) );
- }
- void showLetter(int r,byte *ch)
- {
- int i;
- for(i = 0;i < 8;i++) setROW(r+i,*(ch+i));
- }
- void copyLetter(byte *ch,int pos)
- {
- int i;
- for(i = 0;i<8;i++) *(MSG+i+(pos*8)) = *(ch+i);
- }
- void showMSG(int qtde,int tempo)
- {
- LC.clearDisplay(0);
- LC.clearDisplay(1);
- int idx;
- for(idx = 0; idx < 8*qtde;idx ++)
- {
- for(row = 0;row<16;row++) setROW(row,*(MSG+row+idx));
- delay(tempo);
- }
- LC.clearDisplay(0);
- LC.clearDisplay(1);
- }
- void winner()
- {
- copyLetter(SPACE,0);
- copyLetter(XW,1);
- copyLetter(SPACE,2);
- copyLetter(W,3);
- copyLetter(J,4);
- copyLetter(E,5);
- copyLetter(D,6);
- copyLetter(I,7);
- copyLetter(R,8);
- copyLetter(SPACE,9);
- copyLetter(XW,10);
- copyLetter(SPACE,11);
- showMSG(11,80);
- march();
- setup();
- }
- void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
- {
- digitalWrite(ledPin, HIGH);
- //use led to visualize the notes being played
- int x;
- long delayAmount = (long)(1000000/frequencyInHertz);
- long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
- for(row = 0;row<16;row++) setROW(row,0xFF);
- for (x=0;x<loopTime;x++)
- {
- digitalWrite(speakerPin,HIGH);
- digitalWrite(redled, HIGH);
- delayMicroseconds(delayAmount);
- digitalWrite(speakerPin,LOW);
- delayMicroseconds(delayAmount);
- digitalWrite(redled, LOW);
- }
- digitalWrite(ledPin, LOW);
- //set led back to low
- LC.clearDisplay(0);
- LC.clearDisplay(1);
- delay(20);
- //a little delay to make all notes sound separate
- }
- void march()
- {
- beep(speakerPin, N_a, 500);
- beep(speakerPin, N_a, 500);
- beep(speakerPin, N_a, 500);
- beep(speakerPin, N_f, 350);
- beep(speakerPin, N_cH, 150);
- beep(speakerPin, N_a, 500);
- beep(speakerPin, N_f, 350);
- beep(speakerPin, N_cH, 150);
- beep(speakerPin, N_a, 1000);
- //first bit
- beep(speakerPin, N_eH, 500);
- beep(speakerPin, N_eH, 500);
- beep(speakerPin, N_eH, 500);
- beep(speakerPin, N_fH, 350);
- beep(speakerPin, N_cH, 150);
- beep(speakerPin, N_gS, 500);
- beep(speakerPin, N_f, 350);
- beep(speakerPin, N_cH, 150);
- beep(speakerPin, N_a, 1000);
- //second bit...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement