Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Declares variables required for the functioning of the microbit
  2. //Variable to define if the mode is counter or timer, stored in boolean for memory efficiency
  3. let mode = true;
  4. //The current time for the pause function
  5. let currentTime: number;
  6. //The score variable - starts at 0 on the microbit being turned on
  7. let currentScore = 0;
  8.  
  9. //Function to display different LED pictures depending on which mode the user has selected - stored in function for code cleaness
  10. function modeLEDCheck() {
  11.     //Checks whether the user has selected counter or timer based on the mode variable
  12.     if (mode === true) {
  13.         basic.showLeds(`
  14.         . . # . .
  15.         . # # # .
  16.         # . # . #
  17.         . . # . .
  18.         . . # . .
  19.         `);
  20.     } else {
  21.         basic.showLeds(`
  22.         . # # # .
  23.         # . # . #
  24.         # . # . #
  25.         # . . # #
  26.         . # # # .
  27.         `);
  28.     }
  29. }
  30.  
  31. //Variable storing functions for the timer to be started, reset and stop the timer
  32. const timer = {
  33.     start: function start(savedTime: number) {
  34.         //Code
  35.     },
  36.     stop: function stop() {
  37.         //Code
  38.     },
  39.     reset: function reset() {
  40.         //Code
  41.     }
  42. };
  43.  
  44. /*Due to the micrbit API lacking delisteners for events all actions are stored in functions,
  45. stored in the action variable for easy deactivation*/
  46. const action = {
  47.     //The function for the basic counter as well as the code to reset it
  48.     basicCounter: function basicCounter() {
  49.         basic.showNumber(0);
  50.         //Displays the score with one added to it
  51.         input.onPinPressed(TouchPin.P0, () => {
  52.             basic.showNumber(++currentScore);
  53.         });
  54.  
  55.         //Resets all the code to do with the basic counter
  56.         input.onButtonPressed(Button.AB, () => {
  57.             //Displays the users score for 3 seconds
  58.             basic.showNumber(currentScore)
  59.             basic.pause(3000);
  60.             //Clears the screen
  61.             basic.clearScreen();
  62.             //Resets the score to 0
  63.             currentScore = 0;
  64.             //Runs the newMode function to effectively delisten all the events
  65.             action.modeCancel();
  66.             //Runs the swap fucntion to allow the user to choose their mode
  67.             action.swap();
  68.         });
  69.     },
  70.     //Function to allow the user swap between the two modes
  71.     swap: function swapMode() {
  72.         modeLEDCheck();
  73.  
  74.         //Swaps between the selected mode and the corresponding picture
  75.         input.onButtonPressed(Button.B, () => {
  76.             mode = !mode;
  77.             modeLEDCheck();
  78.         });
  79.  
  80.         //Selects the mode to be run
  81.         input.onButtonPressed(Button.A, () => {
  82.             if (mode === false) {
  83.                 //Timed mode
  84.             }
  85.             else {
  86.                 action.modeCancel();
  87.                 action.basicCounter();
  88.             }
  89.         });
  90.     },
  91.     //Function to effectively delisten the events, then goes back to mode select
  92.     modeCancel: function newMode() {
  93.         input.onPinPressed(TouchPin.P0, () => {
  94.             //Does nothing on purpose
  95.         });
  96.         input.onButtonPressed(Button.A, () => {
  97.             //Does nothing on purpose
  98.         });
  99.         input.onButtonPressed(Button.B, () => {
  100.             //Does nothing on purpose
  101.         });
  102.         input.onButtonPressed(Button.AB, () => {
  103.             //Does nothing on purpose
  104.         });
  105.     }
  106. }
  107.  
  108. //Runs the function to select mode on startup
  109. action.swap();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement