Advertisement
Badjer1983

Star Wars Chest Box Pong Game Vader Music

Dec 7th, 2017
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.14 KB | None | 0 0
  1. // Modified Pong for Star Wars by GJ Schaefer
  2.  
  3. #include "LedControl.h"
  4.  
  5.  
  6. byte MSG[8*20];
  7.  
  8. byte SPACE[8] = {
  9.   B00000000,
  10.   B00000000,
  11.   B00000000,
  12.   B00000000,
  13.   B00000000,
  14.   B00000000,
  15.   B00000000,
  16.   B00000000  
  17. };
  18.  
  19.  
  20. byte T[8] = { //TIE FIGHTER
  21.   B00000000,
  22.   B10000001,
  23.   B10011001,
  24.   B11100111,
  25.   B10011001,
  26.   B10000001,
  27.   B00000000,
  28.   B00000000  
  29. };
  30.  
  31.  
  32. byte L[8] = { //LASER PATTERN
  33.   B00000000,
  34.   B00100000,
  35.   B00100100,
  36.   B00000100,
  37.   B00000000,
  38.   B00010000,
  39.   B00010000,
  40.   B00010000  
  41. };
  42.  
  43. byte B[8] = { //BLASTER PATTERN
  44.   B00000000,
  45.   B00000010,
  46.   B00000010,
  47.   B00000000,
  48.   B00100000,
  49.   B00000000,
  50.   B00000000,
  51.   B00000000  
  52. };
  53.  
  54. byte XW[8] = { //X-WING PATTERN
  55.   B00000000,
  56.   B00011000,
  57.   B00011000,
  58.   B00011000,
  59.   B10011001,
  60.   B10100101,
  61.   B11111111,
  62.   B00011000  
  63. };
  64.  
  65.  
  66. byte P[8] = { //Tie Fighter small PATTERN
  67.   B10010000,
  68.   B11110000,
  69.   B10010000,
  70.   B00000000,
  71.   B00000000,
  72.   B00001010,
  73.   B00001110,
  74.   B00001010  
  75. };
  76.  
  77. byte E[8] = {
  78.   B00000000,
  79.   B00111100,
  80.   B00100100,
  81.   B00100000,
  82.   B00111000,
  83.   B00100000,
  84.   B00100100,
  85.   B00111100  
  86. };
  87.  
  88. byte R[8] = {
  89.   B00000000,
  90.   B00000010,
  91.   B00000100,
  92.   B00001000,
  93.   B01010000,
  94.   B00100000,
  95.   B01010000,
  96.   B10000000  
  97. };
  98.  
  99.  
  100. byte K[8] = { //Death Star PATTERN
  101.   B00111100,
  102.   B01001110,
  103.   B10010011,
  104.   B10010011,
  105.   B10001101,
  106.   B10000001,
  107.   B01000010,
  108.   B00111100  
  109. };
  110.  
  111.  
  112. byte W[8] = {
  113.   B00000000,
  114.   B01000000,
  115.   B00100000,
  116.   B00010000,
  117.   B00001010,
  118.   B00000100,
  119.   B00001010,
  120.   B00000001  
  121. };
  122.  
  123. byte J[8] = {
  124.   B00000000,
  125.   B00111100,
  126.   B00001000,
  127.   B00001000,
  128.   B00001000,
  129.   B00001000,
  130.   B00101000,
  131.   B00111000  
  132. };
  133.  
  134. byte I[8] = {
  135.   B00000000,
  136.   B00111100,
  137.   B00001000,
  138.   B00001000,
  139.   B00001000,
  140.   B00001000,
  141.   B00001000,
  142.   B00111100
  143. };
  144.  
  145. byte D[8] = {
  146.   B00000000,
  147.   B00110000,
  148.   B00101000,
  149.   B00100100,
  150.   B00100100,
  151.   B00100100,
  152.   B00100100,
  153.   B00111000
  154. };
  155. int PongByPass = 0;
  156. int VaderMusic = 0;
  157. int ledPin = 13;
  158. int speakerPin = 7;//turn the 2 to 7 to turn on speaker !!!
  159. int blueled = 2;
  160. int greenled = 8;
  161. int redled = 9;
  162.  
  163. #define N_c 261
  164. #define N_d 294
  165. #define N_e 329
  166. #define N_f 349
  167. #define N_g 391
  168. #define N_gS 415
  169. #define N_a 440
  170. #define N_aS 455
  171. #define N_b 466
  172. #define N_cH 523
  173. #define N_cSH 554
  174. #define N_dH 587
  175. #define N_dSH 622
  176. #define N_eH 659
  177. #define N_fH 698
  178. #define N_fSH 740
  179. #define N_gH 784
  180. #define N_gSH 830
  181. #define N_aH 880
  182.  
  183.  
  184.  
  185. int step_col;
  186. int step_row;
  187. int subidas;
  188. boolean showBars_enabled = false;
  189. int row_bar = 0;
  190.  
  191. LedControl LC=LedControl(12,11,10,2);   // 2 =  two LED Matric Modules
  192. //LedControl LC2=LedControl(speakerPin,6,5,1);
  193.  
  194. int X = 4,OLD_X,sensor;
  195.  
  196. int row,col;
  197. int sc,sr;
  198.  
  199. int cnt = 0;
  200. int desarma = 0;
  201.  
  202. boolean INTRO_SHOWED = false;  // internal logic switch for intro movie to run
  203.  
  204. int dly; // END OF INITIALIZATION ..........................................
  205.  
  206. void setup() {
  207.  
  208.   Serial.begin(9600);
  209.  
  210.   pinMode(A0,INPUT);
  211.   pinMode(3,INPUT);
  212.   pinMode(5,INPUT);
  213.   pinMode(speakerPin, OUTPUT);
  214.   pinMode(blueled, OUTPUT);
  215.   pinMode(greenled, OUTPUT);
  216.   pinMode(redled, OUTPUT);
  217.  
  218.   LC.shutdown(0,false);
  219.   LC.setIntensity(0,8);
  220.   LC.clearDisplay(0);
  221.  
  222.   LC.shutdown(1,false);
  223.   LC.setIntensity(1,4);
  224.   LC.clearDisplay(1);
  225.  
  226.    randomSeed(analogRead(1)*millis());
  227.  
  228.   if(INTRO_SHOWED == false) // Run Star Wars Intro movie then clear screen with Lose flash
  229.   {
  230.      digitalWrite(greenled, HIGH);
  231.      copyLetter(SPACE,0);
  232.      copyLetter(SPACE,1);
  233.      
  234.      copyLetter(T,2);
  235.      copyLetter(L,3);
  236.      copyLetter(B,4);  
  237.      copyLetter(XW,5);
  238.  
  239.      copyLetter(SPACE,6);
  240.  
  241.      copyLetter(XW,7);
  242.      copyLetter(L,8);  
  243.      copyLetter(B,9);  
  244.      copyLetter(T,10);
  245.      copyLetter(SPACE,11);
  246.      copyLetter(P,12);
  247.      copyLetter(SPACE,13);
  248.      copyLetter(K,14);
  249.      showMSG(14,50);
  250.  digitalWrite(greenled, LOW);
  251.      INTRO_SHOWED = true;
  252.    }
  253.  
  254.    lose();
  255.  
  256.    dly = 500;
  257.    
  258.    setLED(15,X,true);setLED(15,X+1,true);setLED(15,X+2,true);
  259.    OLD_X = -1;
  260.    subidas = 0;
  261.  
  262.    step_col = 1;
  263.    step_row = 1;
  264.  
  265.    sc = step_col;
  266.    sr = step_row;
  267.  
  268. //   row = -step_row;
  269.    row = -1+random(3);
  270.    col = random(8);
  271.    
  272.    showBars(false);    
  273. }
  274.  
  275. void ledflash() // Sequence if you do not want Pong Game Active.............
  276. {        
  277.           digitalWrite(blueled,HIGH);
  278.           delay(500);
  279.           digitalWrite(greenled,HIGH);
  280.           digitalWrite(redled,LOW);
  281.           delay(2000);
  282.           digitalWrite(redled,HIGH);
  283.           digitalWrite(greenled,LOW);
  284.           delay(250);
  285.          
  286. }
  287.  
  288. void loop() //Core program routine checks paddle position and switch states...
  289. {
  290.  
  291.    sensor = analogRead(A0); //Read the pot for position of paddle
  292.    VaderMusic = digitalRead(3);  //Read state of music request pushbutton
  293.    PongByPass = digitalRead(5); // Read state of Pong Bypass switch
  294.    // This section below states the pong bypass and Vader March............
  295.       if(VaderMusic == HIGH){
  296.           digitalWrite(blueled, HIGH);winner();}
  297.       if(PongByPass == HIGH)ledflash();
  298.    // This section above is the bypass and march ..............................
  299. //Serial.println(sensor);
  300.    X = map(sensor,0,980,1,6);
  301.  
  302. //   X = int(sensor / 127);
  303. //   if(X < 1) X = 1;
  304. //   if(X > 6) X = 6;
  305.    
  306.    if(X != OLD_X)
  307.    {
  308.       OLD_X = X;
  309.       setROW(15,0x00);  
  310.       setLED(15,X-1,true);setLED(15,X,true);setLED(15,X+1,true);
  311.    }
  312.  
  313.  
  314.    if(cnt == 0)
  315.     {
  316.      setLED(row,col,false);
  317. digitalWrite(blueled, HIGH);
  318.      
  319.      if((subidas==1 || random(2)==1) && showBars_enabled == false && row == 0) {showBars(true);subidas=0;desarma=2+random(5);};
  320.      if(subidas==desarma && showBars_enabled == true) {showBars(false);subidas=0;};
  321.  
  322.      if(col == 7) {sc = -step_col;tone(speakerPin,1000,20);digitalWrite(greenled, HIGH);};
  323.  
  324.      if(col == 0) {sc = step_col;tone(speakerPin,1000,20);};
  325.  
  326.      if(row == 0) {sr = step_row;subidas++;dly-=5;tone(speakerPin,1000,20);};
  327.      
  328.      if(dly <= 200) winner(); //Difficulty adjustment to become a winner
  329.  
  330.      if(showBars_enabled == true && sr > 0 && row < row_bar)
  331.      {
  332.          if(row == row_bar - 1) {sr=-step_row;tone(speakerPin,1000,20);digitalWrite(greenled, LOW);};
  333.      }  
  334.  
  335.      if(row == 14)
  336.      {
  337.         if(col >= X-1 && col <= X+1) {sr = -step_row;tone(speakerPin,1500,20);}
  338.         else if(col == X-2 && sc > 0) {sr = -step_row; sc = -step_col;tone(speakerPin,1500,20);}
  339.         else if(col == X+2 && sc < 0) {sr = -step_row; sc = step_col;tone(speakerPin,1500,20);};
  340.      
  341.      }
  342.      
  343.      row += sr;
  344.      col += sc;
  345.  
  346.      if(col == 8) col = 7;
  347.      if(col == -1) col = 0;
  348.  
  349.      setLED(row,col,true);
  350.  
  351.      if(row == 15) setup();
  352.  
  353.    }
  354.    
  355.    cnt++;
  356.    if(cnt == dly) cnt = 0;
  357.    
  358.   }
  359.  
  360.  
  361. void setLED(int l,int c, boolean state)
  362. {
  363.     int disp=0;
  364.     if(l > 7) {l = l-8;disp=1;};
  365.     LC.setLed(disp,l,c,state);
  366. }
  367.  
  368. void setROW(int r,char ch)
  369. {
  370.    int disp = 0;
  371.    if(r > 7) {r = r - 8; disp=1;};  
  372.    LC.setRow(disp,r,ch);
  373.  
  374. }
  375.  
  376. void lose() //Ball misses the paddle
  377. {
  378.     delay(80);
  379.  
  380.     for(row = 0;row<16;row++)
  381.     {
  382.        setROW(row,0xFF);
  383.        tone(speakerPin,row*500,10);
  384.        digitalWrite(redled, LOW);
  385.        digitalWrite(blueled, HIGH);
  386.        delay(20);      
  387.     }
  388.  
  389.     delay(30);//Time the full screen flashes on a miss of the ball
  390.    
  391.     for(row = 15;row>=0;row--)
  392.     {
  393.        setROW(row,0x00);
  394.        tone(speakerPin,row*500,10);
  395.        delay(30); //Time it takes to decay each row for thescreen flash on a missed ball.  
  396.        digitalWrite(blueled, LOW);  
  397.     }
  398. }
  399.  
  400.  
  401.  
  402. void showBars(boolean show)
  403. {
  404.    showBars_enabled = show;
  405.  
  406.    if(show) row_bar = 2+random(5);
  407.    setROW(row_bar,(show == true ? 0xFF : 0x00) );
  408. }
  409.  
  410. void showLetter(int r,byte *ch)
  411. {
  412.     int i;
  413.     for(i = 0;i < 8;i++) setROW(r+i,*(ch+i));
  414. }
  415.  
  416. void copyLetter(byte *ch,int pos)
  417. {
  418.     int i;
  419.     for(i = 0;i<8;i++) *(MSG+i+(pos*8)) = *(ch+i);
  420. }
  421.  
  422. void showMSG(int qtde,int tempo)
  423. {
  424.    LC.clearDisplay(0);
  425.    LC.clearDisplay(1);
  426.  
  427.    int idx;  
  428.    for(idx = 0; idx < 8*qtde;idx ++)
  429.    {
  430.       for(row = 0;row<16;row++) setROW(row,*(MSG+row+idx));
  431.       delay(tempo);
  432.    }
  433.  
  434.   LC.clearDisplay(0);
  435.   LC.clearDisplay(1);
  436.  
  437. }
  438.  
  439. void winner()
  440. {
  441.  
  442.      copyLetter(SPACE,0);
  443.      copyLetter(XW,1);
  444.      copyLetter(SPACE,2);
  445.    
  446.      copyLetter(W,3);
  447.      copyLetter(J,4);
  448.      copyLetter(E,5);  
  449.      copyLetter(D,6);
  450.      copyLetter(I,7);
  451.      copyLetter(R,8);
  452.  
  453.      copyLetter(SPACE,9);
  454.      copyLetter(XW,10);
  455.      copyLetter(SPACE,11);
  456.  
  457.      showMSG(11,80);
  458.  
  459.      march();
  460.      setup();  
  461. }
  462.  
  463.  
  464. void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
  465. {
  466.     digitalWrite(ledPin, HIGH);  
  467.     //use led to visualize the notes being played
  468.    
  469.     int x;  
  470.     long delayAmount = (long)(1000000/frequencyInHertz);
  471.     long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
  472.  
  473.     for(row = 0;row<16;row++) setROW(row,0xFF);
  474.  
  475.     for (x=0;x<loopTime;x++)  
  476.     {    
  477.         digitalWrite(speakerPin,HIGH);
  478.         digitalWrite(redled, HIGH);
  479.         delayMicroseconds(delayAmount);
  480.         digitalWrite(speakerPin,LOW);
  481.         delayMicroseconds(delayAmount);
  482.         digitalWrite(redled, LOW);
  483.     }    
  484.    
  485.     digitalWrite(ledPin, LOW);
  486.    //set led back to low
  487.    
  488.     LC.clearDisplay(0);
  489.     LC.clearDisplay(1);    
  490.  
  491.     delay(20);
  492.     //a little delay to make all notes sound separate
  493. }    
  494.  
  495.  
  496.  
  497. void march()
  498. {    
  499.     beep(speakerPin, N_a, 500);
  500.     beep(speakerPin, N_a, 500);    
  501.     beep(speakerPin, N_a, 500);
  502.     beep(speakerPin, N_f, 350);
  503.     beep(speakerPin, N_cH, 150);
  504.    
  505.     beep(speakerPin, N_a, 500);
  506.     beep(speakerPin, N_f, 350);
  507.     beep(speakerPin, N_cH, 150);
  508.     beep(speakerPin, N_a, 1000);
  509.     //first bit
  510.    
  511.     beep(speakerPin, N_eH, 500);
  512.     beep(speakerPin, N_eH, 500);
  513.     beep(speakerPin, N_eH, 500);    
  514.     beep(speakerPin, N_fH, 350);
  515.     beep(speakerPin, N_cH, 150);
  516.    
  517.     beep(speakerPin, N_gS, 500);
  518.     beep(speakerPin, N_f, 350);
  519.     beep(speakerPin, N_cH, 150);
  520.     beep(speakerPin, N_a, 1000);
  521.  
  522.     //second bit...    
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement