Advertisement
Guest User

Simple Arduino/LPD8806 game

a guest
May 27th, 2013
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1.  
  2. #include "LPD8806.h"
  3. #include "SPI.h"
  4.  
  5. /*
  6.  * battle thing
  7.  *
  8.  * Hook up two pushbuttons with pullup resistors to the pins listed below
  9.  * you can also hook up a piezo buzzer to pin 9 if you want sound effects
  10.  * the principal of the game is a little green ball that goes back and forth
  11.  * down the strip. you need to hit your button to switch directions when the ball
  12.  * gets to your end. if you don't there's an explosion.
  13.  *  djweis@sjdjweis.com
  14.  */
  15.  
  16. #define NUM_LEDS 240
  17. #define PIN_DATA 2
  18. #define PIN_CLOCK 3
  19.  
  20. #define PLAYER1_IN 4 // digital
  21. #define PLAYER2_IN 5 // digital
  22. #define PIEZO_OUT 9 // digital
  23.  
  24. int delay_time = 10;
  25.  
  26.  
  27. LPD8806 strip = LPD8806(NUM_LEDS, PIN_DATA, PIN_CLOCK);
  28.  
  29. uint32_t COLOR_BLACK = strip.Color(0, 0, 0);
  30. uint32_t COLOR_YELLOW = strip.Color(80, 0, 80);
  31.  
  32. // which direction is the ball moving
  33. #define DIRECTION_ASCENDING 1
  34. #define DIRECTION_DESCENDING 2
  35.  
  36. #define WARNING_ZONE_LENGTH 8
  37.  
  38. // where is the ball
  39. byte position;
  40. // which direction is it heading?
  41. byte direction;
  42.  
  43.  
  44. /*
  45.  * called on start and after an explosion
  46.  */
  47.  
  48. void start_game()
  49. {
  50.   // default ball to middle of board
  51.   position = (NUM_LEDS / 2);
  52.   // set default direction
  53.   //  direction = DIRECTION_DESCENDING;
  54.   direction = DIRECTION_ASCENDING;
  55.  
  56.  
  57.   strip.begin();
  58.  
  59.   // black out the board again
  60.   for (int i = 0; i < NUM_LEDS; i++) {
  61.     strip.setPixelColor(i, strip.Color(0, 0, 0));
  62.   }
  63.  
  64.   draw_warning_zones();
  65.  
  66.   strip.show();
  67.  
  68. }
  69.  
  70. void draw_warning_zones()
  71. {
  72.  
  73.   // warning zones
  74.   for (int i = 0; i < WARNING_ZONE_LENGTH; i++) {
  75.     strip.setPixelColor(i, COLOR_YELLOW);
  76.   }
  77.  
  78.   for (int i = NUM_LEDS; i >= NUM_LEDS - WARNING_ZONE_LENGTH; i--) {
  79.     strip.setPixelColor(i, COLOR_YELLOW);
  80.   }
  81.  
  82. }
  83.  
  84.  
  85. /*
  86.  * the explosion takes 1/4 of the led's on the losing
  87.  * player's side. we increase them as red from the end of
  88.  * the strip showing as we grow, then make every other one
  89.  * black and show.
  90.  */
  91.  
  92. void explode()
  93. {
  94.  
  95.   byte start_pos, end_pos;
  96.   byte led;
  97.  
  98.   if (DIRECTION_DESCENDING == direction) {
  99.     start_pos = 0;
  100.     end_pos = NUM_LEDS / 4;  
  101.  
  102.     // red fill
  103.     for (led = start_pos; led < end_pos; led++) {
  104.       strip.setPixelColor(led, strip.Color(127, 0, 0));
  105.       strip.show();
  106.     }
  107.  
  108.     // alternate black
  109.     for (led = start_pos; led < end_pos; led += 2) {
  110.       strip.setPixelColor(led, strip.Color(0, 0, 0));
  111.       strip.show();
  112.     }
  113.  
  114.     // black fill
  115.     for (led = end_pos; led > start_pos; led--) {
  116.       strip.setPixelColor(led, strip.Color(0, 0, 0));
  117.       strip.show();
  118.     }
  119.  
  120.   } else {
  121.     start_pos = NUM_LEDS;
  122.     end_pos = NUM_LEDS - (NUM_LEDS/4);
  123.  
  124.     for (led = start_pos; led > end_pos; led--) {
  125.       strip.setPixelColor(led, strip.Color(127, 0, 0));
  126.       strip.show();
  127.     }
  128.  
  129.     for (led = start_pos; led > end_pos; led -= 2) {
  130.       strip.setPixelColor(led, strip.Color(0, 0, 0));
  131.       strip.show();
  132.     }
  133.  
  134.     // black fill
  135.     for (led = end_pos; led <= start_pos; led++) {
  136.       strip.setPixelColor(led, strip.Color(0, 0, 0));
  137.       strip.show();
  138.     }
  139.  
  140.   }
  141.  
  142. #if 0
  143.   tone(PIEZO_OUT, 440, 200);  
  144.   delay(300);
  145.   tone(PIEZO_OUT, 300, 200);  
  146.   delay(300);
  147.   tone(PIEZO_OUT, 200, 400);  
  148.   delay(500);
  149. #endif
  150.  
  151.  
  152. }
  153.  
  154.  
  155. /*
  156.  * warning zone is considered the last 6 LED's before the end
  157.  */
  158.  
  159. byte in_warning_zone()
  160. {
  161.   byte retval = 0;
  162.  
  163.   if ((DIRECTION_ASCENDING == direction) &&
  164.       (position >= (NUM_LEDS - WARNING_ZONE_LENGTH))) {
  165.          retval = 1;
  166.   }
  167.      
  168.   if ((DIRECTION_DESCENDING == direction) &&
  169.       (position <= WARNING_ZONE_LENGTH)) {
  170.         retval = 1;
  171.   }
  172.  
  173.   return retval;
  174. }
  175.  
  176.  
  177.  
  178. void setup()
  179. {
  180.  
  181.   pinMode(PLAYER1_IN, INPUT);
  182.   pinMode(PLAYER2_IN, INPUT);
  183.  
  184.   pinMode(PIEZO_OUT, OUTPUT);
  185.  
  186.   calibrate_delay_time();
  187.   start_game();
  188.    
  189.   tone(PIEZO_OUT, 440, 1000);  
  190.   delay(1000);
  191.  
  192.  
  193. }
  194.  
  195. void loop()
  196. {
  197.  
  198.   // grab the button status at the beginning
  199.   byte button1 = digitalRead(PLAYER1_IN);
  200.   byte button2 = digitalRead(PLAYER2_IN);
  201.  
  202.   // normal movement somewhere else on the play field
  203.   byte old_position = position;
  204.    
  205.   if (DIRECTION_ASCENDING == direction) {
  206.     position ++;
  207.   } else {
  208.       position --;
  209.   }
  210.  
  211.   draw_warning_zones();
  212.  
  213.   strip.setPixelColor(old_position, strip.Color(0, 0, 0));
  214.   strip.setPixelColor(position, strip.Color(0, 0, 127));
  215.   strip.show();
  216.    
  217.   if ((position == 0) ||
  218.       (position == NUM_LEDS)) {
  219.     // hit end of track with no button
  220.    
  221.    
  222.     // boom
  223.     explode();    
  224.  
  225.     // start over
  226.     start_game();
  227.  
  228.   } else if (in_warning_zone()) {
  229.     // need to check for button press and reverse direction
  230.     // check for proper button being pressed
  231.  
  232.     if ((DIRECTION_ASCENDING == direction) &&
  233.         (LOW == button1)) {
  234.           direction = DIRECTION_DESCENDING;
  235.     } else if ((DIRECTION_DESCENDING == direction) &&
  236.                (LOW == button2)) {
  237.           direction = DIRECTION_ASCENDING;
  238.     } // direction/button check
  239.    
  240.   } // big loop if check
  241.  
  242.   // wait for next cycle
  243.   // delay(5);
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement