Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Declares variables required for the functioning of the microbit
- //Variable to define if the mode is counter or timer, stored in boolean for memory efficiency
- let mode = true;
- //The current time for the pause function
- let currentTime: number;
- //The score variable - starts at 0 on the microbit being turned on
- let currentScore = 0;
- //Function to display different LED pictures depending on which mode the user has selected - stored in function for code cleaness
- function modeLEDCheck() {
- //Checks whether the user has selected counter or timer based on the mode variable
- if (mode === true) {
- basic.showLeds(`
- . . # . .
- . # # # .
- # . # . #
- . . # . .
- . . # . .
- `);
- } else {
- basic.showLeds(`
- . # # # .
- # . # . #
- # . # . #
- # . . # #
- . # # # .
- `);
- }
- }
- //Variable storing functions for the timer to be started, reset and stop the timer
- const timer = {
- start: function start(savedTime: number) {
- //Code
- },
- stop: function stop() {
- //Code
- },
- reset: function reset() {
- //Code
- }
- };
- /*Due to the micrbit API lacking delisteners for events all actions are stored in functions,
- stored in the action variable for easy deactivation*/
- const action = {
- //The function for the basic counter as well as the code to reset it
- basicCounter: function basicCounter() {
- basic.showNumber(0);
- //Displays the score with one added to it
- input.onPinPressed(TouchPin.P0, () => {
- basic.showNumber(++currentScore);
- });
- //Resets all the code to do with the basic counter
- input.onButtonPressed(Button.AB, () => {
- //Displays the users score for 3 seconds
- basic.showNumber(currentScore)
- basic.pause(3000);
- //Clears the screen
- basic.clearScreen();
- //Resets the score to 0
- currentScore = 0;
- //Runs the newMode function to effectively delisten all the events
- action.modeCancel();
- //Runs the swap fucntion to allow the user to choose their mode
- action.swap();
- });
- },
- //Function to allow the user swap between the two modes
- swap: function swapMode() {
- modeLEDCheck();
- //Swaps between the selected mode and the corresponding picture
- input.onButtonPressed(Button.B, () => {
- mode = !mode;
- modeLEDCheck();
- });
- //Selects the mode to be run
- input.onButtonPressed(Button.A, () => {
- if (mode === false) {
- //Timed mode
- }
- else {
- action.modeCancel();
- action.basicCounter();
- }
- });
- },
- //Function to effectively delisten the events, then goes back to mode select
- modeCancel: function newMode() {
- input.onPinPressed(TouchPin.P0, () => {
- //Does nothing on purpose
- });
- input.onButtonPressed(Button.A, () => {
- //Does nothing on purpose
- });
- input.onButtonPressed(Button.B, () => {
- //Does nothing on purpose
- });
- input.onButtonPressed(Button.AB, () => {
- //Does nothing on purpose
- });
- }
- }
- //Runs the function to select mode on startup
- action.swap();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement