Advertisement
skizziks_53

Arduino game skeleton v1.0

Aug 15th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.39 KB | None | 0 0
  1. /*
  2.   Reddit multi-event timing code skellington (for an Arduino-based game)
  3.  
  4.   Hardware: 1. input controls
  5.             2. LED display (game play area)
  6.             3. LCD display (for timer and score)
  7.             4. A seconds timer for in-game use
  8.  
  9.   This sketch has four different functions to update four different sections of a game program.
  10.   Each section gets executed at its own time interval.
  11. */
  12.  
  13. int input_controls__update_interval = 50; // This is the time interval in milliseconds that the buttons will be checked for presses. (50ms = 20x per second, which is about as fast as is useful)
  14.  
  15. int led_display__update_interval = 50; // This is the time interval in milliseconds that the LED display will be updated. (50ms = 20x per second)
  16.  
  17. int lcd_display__update_interval = 100; // This is the time interval in milliseconds that the LCD display will be updated.
  18. /*
  19.   If you want a seconds counter, then you can't set the LCD update time interval any longer than 1000 milliseconds.
  20.   If you did that however it might suffer timing creep, due to other events occurring slightly before it.
  21.   Liquid crystal displays update relatively slowly, a general rule of thumb is 1/5th of a second at room temperature, and as they get colder they can take a half-second or more to update.
  22.   Updating them faster than they can really change doesn't harm them, but they appear to just display faint 8's and can't be read--so there is not much good reason to do it.
  23.  
  24.   It is okay to call the update function more often than the presumed update time, because the LCD is only going to be changed every 1 second.
  25. */
  26.  
  27. // Below is related variables that the ones above need.
  28. unsigned long input_controls__previous_time = 0;
  29. unsigned long input_controls__current_time = 0;
  30. unsigned long led_display__previous_time = 0;
  31. unsigned long led_display__current_time = 0;
  32. unsigned long lcd_display__previous_time = 0;
  33. unsigned long lcd_display__current_time = 0;
  34.  
  35.  
  36. bool game_clock_in_use = false; // To make the game clock work, you set this variable to 'true'.
  37. unsigned long game_clock__previous_time = 0;
  38. unsigned long game_clock__current_time = 0;
  39. int game_clock__seconds_counter = 0; // The total elapsed seconds will be stored in here.
  40.  
  41. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  42.  
  43.  
  44.  
  45.  
  46.  
  47. // (you need to add in all the other variables yourself)
  48.  
  49.  
  50.  
  51.  
  52.  
  53. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  54.  
  55. void setup() {
  56.   // put your setup code here, to run once:
  57.   Serial.begin(9600);
  58.  
  59.  
  60.   // (you need to add in whatever goes here yourself)
  61.  
  62.  
  63.   Serial.println("Exiting setup()"); // Keep this as the final line of setup().
  64. }
  65.  
  66.  
  67. void loop() {
  68.   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  69.   // The main loop code is finished.
  70.   // Do not put anything else in here for the input controls, the LED display or the LCD display.
  71.   // Other things *might* belong in here, if they need to be done at their own time interval.
  72.   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  73.   check_input_controls_timer();
  74.   check_led_display_timer();
  75.   check_lcd_display_timer();
  76.   check_game_clock();
  77. }
  78.  
  79.  
  80.  
  81. void check_input_controls_timer() {
  82.   // This function does not check the buttons,,,,, it only checks if it is time to check the buttons.
  83.   // This function is completed. Don't put anything else in here.
  84.   input_controls__current_time = millis();
  85.   if (input_controls__current_time >= input_controls__previous_time) {
  86.     if (input_controls__current_time >= (input_controls__previous_time + input_controls__update_interval)) {
  87.       check_the_input_controls(); // This function actually checks the input buttons.
  88.       input_controls__previous_time = millis(); // reset to the current time
  89.     }
  90.   }
  91.   else {
  92.     input_controls__previous_time = millis(); // millis() rollover condition
  93.   }
  94. }
  95.  
  96. void check_led_display_timer() {
  97.   // This function does not update the LED display, it only checks if it is time to update the LED display.
  98.   // This function is completed. Don't put anything else in here.
  99.   led_display__current_time = millis();
  100.   if (led_display__current_time >= led_display__previous_time) {
  101.     if (led_display__current_time >= (led_display__previous_time + led_display__update_interval)) {
  102.       update_the_led_display(); // This function actually updates the LED display.
  103.       led_display__previous_time = millis(); // reset to the current time
  104.     }
  105.   }
  106.   else {
  107.     led_display__previous_time = millis(); // millis() rollover condition
  108.   }
  109. }
  110.  
  111. void check_lcd_display_timer() {
  112.   // This function does not update the LCD display, it only checks if it is time to update the LCD display.
  113.   // This function is completed. Don't put anything else in here.
  114.   lcd_display__current_time = millis();
  115.   if (lcd_display__current_time >= lcd_display__previous_time) {
  116.     if (lcd_display__current_time >= (lcd_display__previous_time + lcd_display__update_interval)) {
  117.       update_the_lcd_display(); // This function actually updates the LED display.
  118.       lcd_display__previous_time = millis(); // reset to the current time
  119.     }
  120.   }
  121.   else {
  122.     led_display__previous_time = millis(); // millis() rollover condition
  123.   }
  124. }
  125.  
  126. void start_game_clock() {
  127.   // This function begins the game clock.
  128.   game_clock_in_use = true;
  129.   game_clock__previous_time = millis();
  130. }
  131.  
  132. void check_game_clock() {
  133.   // This function is just for incrementing the game clock seconds counter.
  134.   if (game_clock_in_use == true) {
  135.     game_clock__current_time = millis();
  136.     if (game_clock__current_time >= game_clock__previous_time) {
  137.       if (game_clock__current_time >= (game_clock__previous_time + 1000)) {
  138.         game_clock__seconds_counter++; // This line increments the game clock (seconds counter).
  139.         game_clock__previous_time = millis(); // reset to the current time
  140.       }
  141.     }
  142.     else {
  143.       game_clock__previous_time = millis(); // millis() rollover condition
  144.     }
  145.   }
  146. }
  147.  
  148. void stop_game_clock() {
  149.   //game_clock__seconds_counter = 0; // You can reset the seconds counter here or have some other line to do that elsewhere.
  150.   // If you want to be able to pause the game, then you don't want to reset the game clock to zero automatically here (y1ou would set it to zero when the player begins a new game).
  151.   game_clock_in_use = false;
  152. }
  153.  
  154.  
  155.  
  156. void check_the_input_controls() {
  157.   // This function is where the input buttons are checked. This function will be called 20 times per second.
  158.   // All of the code for checking the buttons, must be done inside this function.
  159.   // (you need to add all that)
  160.  
  161. }
  162.  
  163. void update_the_led_display() {
  164.   // This function is where the LED display is updated. This function will be called 20 times per second.
  165.   // All of the code for updating the LED display, must be done inside this function.
  166.   // (you need to add all that)
  167.  
  168.   // Note: since this function is called at an interval that is longer than button bounce lasts, this means that you don't need to add any code to debounce the buttons.
  169.   // You can just call a digitalRead() command on them and use the returned value.
  170.  
  171. }
  172.  
  173. void update_the_lcd_display() {
  174.   // This function is where the LCD display is updated. This function will be called 10 times per second.
  175.   // All of the code for updating the LCD display, must be done inside this function.
  176.   // (you need to add all that)
  177.  
  178. }
  179.  
  180.  
  181. // ~~~~~~~~ [end] ~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement