Advertisement
Guest User

Arduino LED Game

a guest
Jan 11th, 2017
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. /**
  2.  *  Arduino LED game
  3.  *  by: Michael Schuppenies
  4.  *  http://www.ezplugins.de/
  5.  *  
  6.  *  Have fun, drink responsibly, etc.
  7.  */
  8. const int latchPin = 8;
  9. const int clockPin = 12;
  10. const int dataPin = 11;
  11. const int buttonPin = 2;
  12.  
  13. int button_state = 0;
  14. int button_state_last = 0;
  15. unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  16. unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
  17.  
  18. byte leds = 0;
  19. unsigned long int timer = 0;
  20. int timer_update = 200;
  21.  
  22. int current_anim_step = 0;
  23. int anim_length;
  24.  
  25. int delay_mode[4];
  26. int mode = 3; // 0 = easy, mid = 1, hard = 2, wtf = 3
  27. int r;
  28. int state = 0; // 0 = menu, 1 = start, 2 = running, 3 = end
  29. int state_last = -1;
  30.  
  31. const int STATE_MENU = 0;
  32. const int STATE_START = 1;
  33. const int STATE_RUNNING = 2;
  34. const int STATE_END = 3;
  35.  
  36. int game_led_win;
  37. int game_led;
  38. int game_leds = 8;
  39.  
  40. void setup()
  41. {
  42.   Serial.begin(9600);
  43.   Serial.println("Setup");
  44.  
  45.   pinMode(latchPin, OUTPUT);
  46.   pinMode(dataPin, OUTPUT);  
  47.   pinMode(clockPin, OUTPUT);
  48.   pinMode(buttonPin, INPUT);
  49.  
  50.   delay_mode[0] = 300;  // easy
  51.   delay_mode[1] = 150;  // mid
  52.   delay_mode[2] = 100;  // hard
  53.   delay_mode[3] = 50;   // wtf
  54.  
  55.   r = 0;
  56. }
  57.  
  58. void loop()
  59. {
  60.   button_update();
  61.  
  62.   if (state_last != state) {
  63.     current_anim_step = 0;
  64.     r = 0;
  65.    
  66.     switch (state) {
  67.       case 1: game_start(); break;
  68.       case 2: game_run(); break;
  69.       case 3: game_end(); break;
  70.       default: game_menu(); break;
  71.     }
  72.  
  73.     state_last = state;
  74.   }
  75.  
  76.   if ((unsigned long) (millis() - timer) >= timer_update) {
  77.     timer_update = 400;
  78.    
  79.     switch (state) {
  80.       case 1: game_start_update(r); break;
  81.       case 2: timer_update = delay_mode[mode]; game_run_update(r); break;
  82.       case 3: game_end_update(r); break;
  83.       default: game_menu_update(r); break;
  84.     }
  85.  
  86.     r++;
  87.     timer = millis();
  88.     updateShiftRegister();
  89.   }
  90. }
  91.  
  92. void animate(int anim_array[]) {
  93.   if (current_anim_step < anim_length) {  
  94.     int tmp_led = anim_array[current_anim_step];
  95.  
  96.     if (tmp_led == -1) {
  97.       all_off();
  98.     }
  99.     else {
  100.       update_led(anim_array[current_anim_step]);
  101.     }
  102.    
  103.     current_anim_step++;
  104.   }
  105.   else {
  106.     current_anim_step = 0;
  107.     all_off();
  108.   }
  109. }
  110.  
  111. void game_menu() {
  112. }
  113. void game_menu_update(int rr) {
  114.   int anim[] = { 0, 1, 2, 7, 6, 5, 3, 4 };
  115.   anim_length = sizeof(anim) / sizeof(anim[0]);
  116.  
  117.   animate(anim);
  118. }
  119.  
  120. void game_start() {
  121.   game_led_win = 0;
  122.   game_led = 7;
  123. }
  124. void game_start_update(int rr) {
  125.   int anim[] = { 7, -1, 7, -1, 7, -1, 7, -1 };
  126.   anim_length = sizeof(anim) / sizeof(anim[0]);
  127.   animate(anim);
  128.  
  129.   if (rr == 9) {
  130.     state = STATE_RUNNING;
  131.   }
  132. }
  133. void game_run() {}
  134. void game_run_update(int rr) {
  135.   if (game_led == 0) {
  136.     game_led = game_leds - 1;
  137.   }
  138.   else {
  139.     game_led--;
  140.   }
  141.  
  142.   all_off();
  143.   update_led(game_led);
  144.  
  145.   //log(String(game_led));
  146. }
  147. void game_end() {}
  148. void game_end_update(int rr) {
  149.   if (game_led == game_led_win) {
  150.     int anim[] = { game_led };
  151.     anim_length = 1;
  152.     animate(anim);
  153.   }
  154. }
  155.  
  156. void update_led(byte led) {
  157.   leds = bitSet(leds, led);
  158. }
  159.  
  160. void all_off() {
  161.   leds = 0;
  162.   updateShiftRegister();
  163. }
  164.  
  165. void updateShiftRegister() {
  166.    digitalWrite(latchPin, LOW);
  167.    shiftOut(dataPin, clockPin, LSBFIRST, leds);
  168.    digitalWrite(latchPin, HIGH);
  169. }
  170.  
  171. void button_press() {
  172.   if (state == STATE_MENU) {
  173.     state = STATE_START;
  174.   }
  175.   else if (state == STATE_RUNNING) {
  176.     state = STATE_END;
  177.   }
  178.   else if (state == STATE_END) {
  179.     state = STATE_MENU;
  180.     all_off();
  181.   }
  182. }
  183.  
  184. void button_release() {
  185. }
  186.  
  187. void button_update() {
  188.   int button_read = digitalRead(buttonPin);
  189.  
  190.   // If the switch changed, due to noise or pressing:
  191.   if (button_read != button_state_last) {
  192.     // reset the debouncing timer
  193.     lastDebounceTime = millis();
  194.   }
  195.  
  196.   if ((millis() - lastDebounceTime) > debounceDelay) {
  197.     if (button_read != button_state) {
  198.       button_state = button_read;
  199.  
  200.       if (button_state == HIGH) {
  201.         button_press();
  202.       }
  203.       else {
  204.         button_release();
  205.       }
  206.     }
  207.   }
  208.  
  209.   button_state_last = button_read;
  210. }
  211.  
  212. void log(String str) {
  213.   Serial.println(String(millis()) + ": " + str);
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement