Advertisement
ChipSkylarkk

LED Pong complete

Jun 16th, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.10 KB | None | 0 0
  1. /*
  2. Arduino controlled LED Pong
  3.  
  4. Designed and created by Schuyler Sowa
  5. http://www.instructables.com/member/Chip+Fixes/
  6. */
  7.  
  8. //You need to install these libraries on your computer
  9. #include "LPD8806.h"
  10. #include "SPI.h"
  11. #include <Wire.h>
  12. #include <LiquidCrystal_I2C.h>
  13.  
  14. //Scoreboard shift registers
  15. #define data1 5 //14
  16. #define clock1 7 //11
  17. #define latch1 6 //12
  18.  
  19. //hex values of scoreboard numbers (0-9)
  20. byte digitOne[10]= {0x6F, 0x09, 0x73, 0x3B, 0x1D, 0x3E, 0x7C, 0x0B, 0x7F, 0x1F}; //Player 1 numbers
  21. byte digitTwo[10]= {0x6F, 0x09, 0x73, 0x3B, 0x1D, 0x3E, 0x7C, 0x0B, 0x7F, 0x1F}; //Player 2 numbers
  22.  
  23. //Player 1 shift register
  24. #define data2 8
  25. #define clock2 9
  26. #define latch2 10
  27.  
  28. //Player 2 shift register
  29. #define data3 11
  30. #define clock3 12
  31. #define latch3 13
  32.  
  33. #define player1 1 //Player 1 push button
  34. #define player2 2 //Player 2 push button
  35. #define pot A2 //Adjusts speed of 'ball'
  36.  
  37. int nLEDs = 30; //Number of LEDs
  38. int dataPin  = 3; //LED Strip
  39. int clockPin = 4; //LED Strip
  40. LPD8806 strip = LPD8806(32, dataPin, clockPin);
  41.  
  42. char turn = 'A';
  43. char flag = 'N';
  44. char cheat = 'N';
  45. int i = 0;
  46. int j = 0;
  47. byte player1Points = 0;
  48. byte player2Points = 0;
  49. int score;
  50.  
  51. //LCD
  52. //Wire SDA to A4 and SCL to A5
  53. LiquidCrystal_I2C lcd(0x20,16,2);  //set the LCD address to 0x20 for a 16 chars and 2 line display
  54.  
  55. void setup()
  56. {
  57.   lcd.init(); //initialize lcd
  58.   lcd.backlight(); //turn on backlight
  59.  
  60.   pinMode(latch1, OUTPUT);
  61.   pinMode(clock1, OUTPUT);
  62.   pinMode(data1, OUTPUT);
  63.  
  64.   pinMode(latch2, OUTPUT);
  65.   pinMode(clock2, OUTPUT);
  66.   pinMode(data2, OUTPUT);
  67.  
  68.   pinMode(latch3, OUTPUT);
  69.   pinMode(clock3, OUTPUT);
  70.   pinMode(data3, OUTPUT);
  71.  
  72.   //Sets scoreboards to '0'
  73.   digitalWrite(latch1, LOW);
  74.   shiftOut(data1, clock1, MSBFIRST, ~digitOne[0]);
  75.   shiftOut(data1, clock1, MSBFIRST, ~digitTwo[0]);
  76.   digitalWrite(latch1, HIGH);
  77.  
  78.   //Turns off player1 and player2 LEDs
  79.   digitalWrite(latch2, LOW);
  80.   shiftOut(data2, clock2, MSBFIRST, 255);  
  81.   digitalWrite(latch2, HIGH);
  82.   digitalWrite(latch3, LOW);
  83.   shiftOut(data3, clock3, MSBFIRST, 255);  
  84.   digitalWrite(latch3, HIGH);
  85.  
  86.   pinMode(pot, INPUT);
  87.   strip.begin();
  88.   strip.show();
  89.   pinMode(player1, INPUT);
  90.   digitalWrite(player1, HIGH);
  91.   pinMode(player2, INPUT);
  92.   digitalWrite(player2, HIGH);
  93.  
  94.   score = selectScore(); //Sets the winning score
  95.  
  96.   //Turns on player1 LEDs
  97.   digitalWrite(latch2, LOW);
  98.   shiftOut(data2, clock2, MSBFIRST, 0);  
  99.   digitalWrite(latch2, HIGH);
  100. }
  101.  
  102.  
  103. void loop()
  104. {
  105.   digitalWrite(latch1, LOW);
  106.   shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
  107.   shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
  108.   digitalWrite(latch1, HIGH);
  109.  
  110.   if(player1Points >= score)
  111.   {
  112.     player1Celebrate();
  113.   }
  114.  
  115.   if(player2Points >= score)
  116.   {
  117.     player2Celebrate();
  118.   }
  119.  
  120.   int val = analogRead(pot);
  121.   val = map(val, 0, 1023, 1, 100);
  122.  
  123.   if(flag == 'F')
  124.   {
  125.     chaseForward(strip.Color(127, 0, 0), val);
  126.   }
  127.   if(flag == 'R')
  128.   {
  129.     chaseReverse(strip.Color(0, 0, 127), val);
  130.   }
  131.  
  132.   int AbuttonState = digitalRead(player1);
  133.   if(AbuttonState == LOW && turn == 'A' && flag != 'F' && flag != 'R')
  134.   {
  135.     i = 0;
  136.    
  137.     //Turns off player 1 LEDs
  138.     digitalWrite(latch2, LOW);
  139.     shiftOut(data2, clock2, MSBFIRST, 255);  
  140.     digitalWrite(latch2, HIGH);
  141.    
  142.   chaseForward(strip.Color(0, 127, 0), val); // White
  143.   }
  144.  
  145.   int BbuttonState = digitalRead(player2);
  146.   if(BbuttonState == LOW && turn == 'B' && flag != 'F' && flag != 'R')
  147.   {
  148.     i = 30;
  149.    
  150.     //Turns off player 2 LEDs
  151.     digitalWrite(latch3, LOW);
  152.     shiftOut(data3, clock3, MSBFIRST, 255);  
  153.     digitalWrite(latch3, HIGH);
  154.    
  155.   chaseReverse(strip.Color(127,   0,   127), val); // Red
  156.   }
  157. }
  158.  
  159. uint32_t Wheel(uint16_t WheelPos)
  160. {
  161.   byte r, g, b;
  162.   switch(WheelPos / 128)
  163.   {
  164.     case 0:
  165.       r = 127 - WheelPos % 128;   //Red down
  166.       g = WheelPos % 128;      // Green up
  167.       b = 0;                  //blue off
  168.       break;
  169.     case 1:
  170.       g = 127 - WheelPos % 128;  //green down
  171.       b = WheelPos % 128;      //blue up
  172.       r = 0;                  //red off
  173.       break;
  174.     case 2:
  175.       b = 127 - WheelPos % 128;  //blue down
  176.       r = WheelPos % 128;      //red up
  177.       g = 0;                  //green off
  178.       break;
  179.   }
  180.   return(strip.Color(r,g,b));
  181. }
  182.  
  183. void chaseForward(uint32_t c, uint8_t wait)
  184. {
  185.   lcd.clear();
  186.  
  187.   for(i; i < 30; i++)
  188.   {
  189.     if(digitalRead(player2) == LOW)
  190.     cheat = 'Y';
  191.     strip.setPixelColor(i, c); // Set new pixel 'on'
  192.     strip.show();              // Refresh LED states
  193.     strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
  194.     delay(wait);
  195.    
  196.     if(i > 27 && cheat != 'Y')
  197.     {
  198.       int BbuttonState = digitalRead(player2);
  199.       if(BbuttonState == LOW)
  200.       {
  201.         turn = 'B';
  202.         flag = 'R';
  203.         return;
  204.       }
  205.     }
  206.   }
  207.   player1Points++;
  208.   digitalWrite(latch1, LOW);
  209.   shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
  210.   shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
  211.   digitalWrite(latch1, HIGH);
  212.   if(player1Points < score)
  213.   lcd.print("PLAYER 1 SCORES!");
  214.   strip.show();
  215.  
  216.   //Turn on player1 LEDs
  217.   digitalWrite(latch2, LOW);
  218.   shiftOut(data2, clock2, MSBFIRST, 0);  
  219.   digitalWrite(latch2, HIGH);
  220.  
  221.   flag = 'S';
  222.   cheat = 'N';
  223. }
  224.  
  225. void chaseReverse(uint32_t c, uint8_t wait)
  226. {
  227.   lcd.clear();
  228.  
  229.   for(i; i >= 0; i--)
  230.   {
  231.     if(digitalRead(player1) == LOW)
  232.     cheat = 'Y';
  233.     strip.setPixelColor(i, c); // Set new pixel 'on'
  234.     strip.show();              // Refresh LED states
  235.     strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
  236.     delay(wait);
  237.    
  238.     if(i < 2 && cheat != 'Y')
  239.     {
  240.       int BbuttonState = digitalRead(player1);
  241.       if(BbuttonState == LOW)
  242.       {
  243.         turn = 'A';
  244.         flag = 'F';
  245.         return;
  246.       }
  247.     }
  248.   }
  249.   player2Points++;
  250.   digitalWrite(latch1, LOW);
  251.   shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
  252.   shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
  253.   digitalWrite(latch1, HIGH);
  254.   strip.show();
  255.   if(player2Points < score)
  256.   lcd.print("PLAYER 2 SCORES!");
  257.  
  258.   //Turn on player2 LEDs
  259.   digitalWrite(latch3, LOW);
  260.   shiftOut(data3, clock3, MSBFIRST, 0);  
  261.   digitalWrite(latch3, HIGH);
  262.  
  263.   flag = 'S';
  264.   cheat = 'N';
  265. }
  266.  
  267. void player1Celebrate()
  268. {
  269.   lcd.print("PLAYER 1 WINS!");
  270.   rainbowCyclePlayer1(0);
  271.   player1Points = 0;
  272.   player2Points = 0;
  273.   digitalWrite(latch1, LOW);
  274.   shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
  275.   shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
  276.   digitalWrite(latch1, HIGH);
  277. }
  278.  
  279. void player2Celebrate()
  280. {
  281.   lcd.print("PLAYER 2 WINS!");
  282.   rainbowCyclePlayer2(0);
  283.   player1Points = 0;
  284.   player2Points = 0;
  285.   digitalWrite(latch1, LOW);
  286.   shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
  287.   shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
  288.   digitalWrite(latch1, HIGH);
  289. }
  290.  
  291. void rainbowCyclePlayer1(uint8_t wait)
  292. {
  293.   int m = 32;
  294.   uint16_t k, j;
  295.  
  296.   //Turns off player 1 LEDs
  297.   digitalWrite(latch2, LOW);
  298.   shiftOut(data2, clock2, MSBFIRST, 255);  
  299.   digitalWrite(latch2, HIGH);
  300.    
  301.   for (j=0; j < 384 * 3; j++) {     // 5 cycles of all 384 colors in the wheel
  302.     for (k=0; k < strip.numPixels(); k++) {
  303.       // tricky math! we use each pixel as a fraction of the full 384-color wheel
  304.       // (thats the i / strip.numPixels() part)
  305.       // Then add in j which makes the colors go around per pixel
  306.       // the % 384 is to make the wheel cycle around
  307.       strip.setPixelColor(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
  308.     }  
  309.     strip.show();   // write all the pixels out
  310.     delay(wait);
  311.   }
  312.   for(m; m >= 0; m--)
  313.   {
  314.   strip.setPixelColor(m, 0);
  315.   strip.show();
  316.   }
  317.   //Turn on player1 LEDs
  318.   digitalWrite(latch2, LOW);
  319.   shiftOut(data2, clock2, MSBFIRST, 0);  
  320.   digitalWrite(latch2, HIGH);
  321. }
  322.  
  323. void rainbowCyclePlayer2(uint8_t wait)
  324. {
  325.   int m = 0;
  326.   uint16_t k, j;
  327.  
  328.    //Turns off player 2 LEDs
  329.    digitalWrite(latch3, LOW);
  330.    shiftOut(data3, clock3, MSBFIRST, 255);  
  331.    digitalWrite(latch3, HIGH);
  332.    
  333.    for (j=384 * 3; j > 0; j--) {     // 5 cycles of all 384 colors in the wheel
  334.     for (k=0; k < strip.numPixels(); k++) {
  335.       // tricky math! we use each pixel as a fraction of the full 384-color wheel
  336.       // (thats the i / strip.numPixels() part)
  337.       // Then add in j which makes the colors go around per pixel
  338.       // the % 384 is to make the wheel cycle around
  339.       strip.setPixelColor(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
  340.     }  
  341.     strip.show();   // write all the pixels out
  342.     delay(wait);
  343.   }
  344.   for(m=0; m < 32; m++)
  345.   {
  346.   strip.setPixelColor(m, 0);
  347.   strip.show();
  348.   }
  349.   //Turn on player2 LEDs
  350.   digitalWrite(latch3, LOW);
  351.   shiftOut(data3, clock3, MSBFIRST, 0);  
  352.   digitalWrite(latch3, HIGH);
  353. }
  354.  
  355. int selectScore()
  356. {
  357.   lcd.print("Player 1, select");
  358.   lcd.setCursor(0, 1);
  359.   lcd.print("score limit by");
  360.   delay(3000);
  361.   lcd.setCursor(1,0);
  362.   lcd.clear();
  363.   lcd.print("pressing button.");
  364.   delay(3000);
  365.   lcd.setCursor(1,0);
  366.   lcd.clear();
  367.   lcd.print("Player 2");
  368.   lcd.setCursor(0,1);
  369.   lcd.print("confirms by");
  370.   delay(3000);
  371.   lcd.setCursor(1,0);
  372.   lcd.clear();
  373.   lcd.print("pressing their");
  374.   lcd.setCursor(0,1);
  375.   lcd.print("button.");
  376.   delay(3000);
  377.   lcd.clear();
  378.  
  379.   int i;
  380.   int buttonState;
  381.   int lastButtonState;
  382.   int buttonPushCounter=1;
  383.   int confirm=1;
  384.  
  385.   while(confirm==1) //Stay in this loop
  386.   {
  387.     delay(100); //The number will look really dim if you take this out
  388.     lcd.clear();
  389.     lcd.print(buttonPushCounter);
  390.     buttonState = digitalRead(player1);
  391.     confirm = digitalRead(player2);
  392.     if (buttonState != lastButtonState)
  393.     {
  394.       if (buttonState == LOW)
  395.       {
  396.         buttonPushCounter++;
  397.       }
  398.     }
  399.     lastButtonState = buttonState;
  400.     if (buttonPushCounter > 9) //start over at 1
  401.     {
  402.       lcd.clear();
  403.       lcd.print(1);
  404.       buttonPushCounter = 1;
  405.     }
  406.     if (confirm == LOW)
  407.     {
  408.       lcd.clear();
  409.       return buttonPushCounter;
  410.     }
  411.   }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement