[{"data":1,"prerenderedAt":696},["ShallowReactive",2],{"microbit-edublocks":3},[4],{"id":5,"title":6,"body":7,"description":674,"draft":677,"duration":678,"extension":679,"image":680,"kit":681,"language":685,"level":686,"meta":687,"navigation":688,"path":689,"pdf":690,"platform":691,"seo":692,"stem":693,"youtube":694,"__hash__":695},"tutorials\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002Findex.md","MicroFit - Your very own Step Counter",{"type":8,"value":9,"toc":673},"minimark",[10],[11,12,13,87,93,100,161,192,234,261,307,347,392,442,483,503,547,573],"tutorial-steps",{},[14,15,17,27,39,42],"step",{"title":16},"Overview",[18,19,20,21,26],"p",{},"Ever wondered how a smartwatch or fitness band counts your steps? In this project, you're going to build your very own step counter using a ",[22,23,25],"g",{"slug":24},"micro-bit","micro:bit","!",[18,28,29,30,33,34,38],{},"Inside your micro:bit is a clever little sensor called an ",[22,31,32],{"slug":32},"accelerometer",". It can feel when the micro:bit moves. We'll use it to spot every step you take, add them up, and show the total on the ",[22,35,37],{"slug":36},"led-matrix","LED matrix"," (the grid of lights on the front).",[18,40,41],{},"The best bit? When you're done, you'll have a real working fitness tracker you can pop on and take outside. Don't worry if something doesn't work first time, that's all part of coding. Let's give it a go!",[43,44,47,53],"callout",{"color":45,"icon":46},"green","🎯",[18,48,49],{},[50,51,52],"strong",{},"What you'll learn",[54,55,56,63,66,69,72,79],"ul",{},[57,58,59,60],"li",{},"Make and use your own ",[22,61,62],{"slug":62},"variable",[57,64,65],{},"Use the micro:bit's shake sensor to spot movement",[57,67,68],{},"Show a number on the grid of lights",[57,70,71],{},"Use the buttons to control your project",[57,73,74,75],{},"Use a ",[22,76,78],{"slug":77},"while-loop","while loop",[57,80,81,82,86],{},"Make decisions with ",[22,83,85],{"slug":84},"if-statement","if statement","s",[14,88,90],{"title":89},"What you'll need",[91,92],"what-youll-need",{},[14,94,96],{"title":95},"Key words",[97,98],"key-words",{"terms":99},"variable,while-loop,if-statement,accelerometer,led-matrix,button",[14,101,103,138,145,155],{"title":102},"Set up your micro:bit EduBlocks Editor",[104,105,106,109,120],"ol",{},[57,107,108],{},"Open your web browser. We recommend Google Chrome or Microsoft Edge.",[57,110,111,112,119],{},"In the address bar type ",[113,114,118],"a",{"href":115,"rel":116},"https:\u002F\u002Fapp.edublocks.org\u002F",[117],"nofollow","app.edublocks.org",".",[57,121,122,123,126,127,129,130,133,134,137],{},"Under ",[50,124,125],{},"Create New Project",", select ",[50,128,25],{},". Name your project ",[50,131,132],{},"MicroFit",", and make sure ",[50,135,136],{},"block"," is selected under type.",[18,139,140],{},[141,142],"img",{"alt":143,"src":144},"Creating a new EduBlocks project","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FProjectCreation.webp",[104,146,148],{"start":147},4,[57,149,150,151,154],{},"Select ",[50,152,153],{},"Create"," to open the micro:bit editor.",[18,156,157],{},[141,158],{"alt":159,"src":160},"The EduBlocks code area","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FCodeArea.webp",[14,162,164,183,186],{"title":163},"Import the MicroPython library",[18,165,150,166,169,170,173,174,178,179,182],{},[50,167,168],{},"Basic",", then ",[50,171,172],{},"Imports",", then snap a ",[175,176,177],"code",{},"from microbit import *"," block onto the ",[175,180,181],{},"#start code here"," block.",[18,184,185],{},"This imports the micro:bit library into EduBlocks so we can use all the micro:bit's functions.",[18,187,188],{},[141,189],{"alt":190,"src":191},"Importing the MicroPython library","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FS1_ImportingPythonLibrary.webp",[14,193,195,219,228],{"title":194},"Set steps to 0",[104,196,197,210],{},[57,198,150,199,202,203,206,207,119],{},[50,200,201],{},"Variables",", choose ",[50,204,205],{},"Create a Variable",", and name it ",[50,208,209],{},"steps",[57,211,212,213,216,217,182],{},"Snap a ",[175,214,215],{},"steps = 0"," block to the ",[175,218,177],{},[18,220,221,222,224,225,227],{},"This makes a ",[22,223,62],{"slug":62}," called ",[50,226,209],{}," and starts it at zero.",[18,229,230],{},[141,231],{"alt":232,"src":233},"Setting steps to zero","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FS2_SettingStepsToZero.webp",[14,235,237,249,255],{"title":236},"Create a while True loop",[18,238,150,239,169,241,173,244,178,247,182],{},[50,240,168],{},[50,242,243],{},"Loops",[175,245,246],{},"while True:",[175,248,215],{},[18,250,251,252,254],{},"This ",[22,253,78],{"slug":77}," gives us a space to put code that repeats forever.",[18,256,257],{},[141,258],{"alt":259,"src":260},"Creating a while True loop","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FS3_CreatingWhileLoop.webp",[14,262,264,296,301],{"title":263},"Detect a shake",[104,265,266,280],{},[57,267,150,268,169,270,273,274,277,278,182],{},[50,269,168],{},[50,271,272],{},"Logic",", then drag an ",[175,275,276],{},"if True:"," block inside the ",[175,279,246],{},[57,281,150,282,285,286,178,289,292,293,119],{},[50,283,284],{},"Accelerometer",", then snap an ",[175,287,288],{},"accelerometer.was_gesture(\"shake\")",[175,290,291],{},"if"," block where it says ",[50,294,295],{},"True",[18,297,251,298,300],{},[22,299,85],{"slug":84}," runs its code only when the micro:bit feels a shake.",[18,302,303],{},[141,304],{"alt":305,"src":306},"Creating the shake function","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FS4_CreatingTheShakeFunction.webp",[14,308,310,338,341],{"title":309},"Increase the steps",[104,311,312,321],{},[57,313,150,314,173,316,277,318,182],{},[50,315,201],{},[175,317,215],{},[175,319,320],{},"if accelerometer.was_gesture(\"shake\"):",[57,322,323,324,327,328,331,332,327,335,119],{},"Change the ",[50,325,326],{},"="," to ",[50,329,330],{},"+="," and the ",[50,333,334],{},"0",[50,336,337],{},"1",[18,339,340],{},"Now every detected shake adds one to your step count.",[18,342,343],{},[141,344],{"alt":345,"src":346},"Increasing the steps","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FS5_IncreasingStepsBy.webp",[14,348,350,383,386],{"title":349},"Pause the code",[18,351,150,352,169,354,173,357,360,361,364,365,367,368,370,371,373,374,376,377,327,380,119],{},[50,353,168],{},[50,355,356],{},"Statements",[175,358,359],{},"sleep (1000)"," block ",[50,362,363],{},"below"," the ",[175,366,320],{}," block, lined up with the ",[175,369,291],{}," (inside the ",[175,372,246],{}," loop, but not inside the ",[175,375,291],{},"). Change ",[50,378,379],{},"1000",[50,381,382],{},"500",[18,384,385],{},"This pauses for half a second so the loop has time to notice the button press we'll add next.",[18,387,388],{},[141,389],{"alt":390,"src":391},"Pausing the code","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FS6_PausingTheCode.webp",[14,393,395,429,436],{"title":394},"Reset the steps with button A",[104,396,397,408,420],{},[57,398,150,399,169,401,285,403,178,405,182],{},[50,400,168],{},[50,402,272],{},[175,404,276],{},[175,406,407],{},"sleep (500)",[57,409,150,410,173,413,178,416,292,418,119],{},[50,411,412],{},"Buttons",[175,414,415],{},"button_a.was_pressed()",[175,417,291],{},[50,419,295],{},[57,421,150,422,173,424,277,426,182],{},[50,423,201],{},[175,425,215],{},[175,427,428],{},"if button_a.was_pressed():",[18,430,431,432,435],{},"Pressing ",[22,433,434],{"slug":434},"button"," A now resets your step count back to zero.",[18,437,438],{},[141,439],{"alt":440,"src":441},"Resetting steps to zero","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FS7_ResettingSteps.webp",[14,443,445,471,477],{"title":444},"Show the number of steps",[104,446,447,458],{},[57,448,150,449,173,452,455,456,182],{},[50,450,451],{},"Display",[175,453,454],{},"display.scroll (\"Hello World\")"," block just below the ",[175,457,428],{},[57,459,150,460,173,462,178,464,467,468,119],{},[50,461,201],{},[175,463,209],{},[175,465,466],{},"display.scroll"," block, over the words ",[50,469,470],{},"\"Hello World\"",[18,472,473,474,476],{},"This block sits inside the loop but not inside the button A block, so your steps scroll across the ",[22,475,37],{"slug":36}," all the time, not just when you press A.",[18,478,479],{},[141,480],{"alt":481,"src":482},"Displaying the number of steps","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FS8_DisplayingSteps.webp",[14,484,486],{"title":485},"Downloading the Code",[104,487,488,491,497],{},[57,489,490],{},"Connect the micro:bit to the computer with the micro USB cable.",[57,492,150,493,496],{},[50,494,495],{},"Connect"," and follow the on-screen prompts to pair the micro:bit with your web browser.",[57,498,150,499,502],{},[50,500,501],{},"Download"," to send the code to your micro:bit.",[14,504,506,511,514,534,540,544],{"title":505},"Run it and Watch",[507,508,510],"h3",{"id":509},"in-the-web-browser","In the web browser",[18,512,513],{},"EduBlocks has a built-in micro:bit simulator, handy if you haven't got a micro:bit to hand.",[104,515,516,522,528],{},[57,517,518,519,119],{},"On the right-hand side of the screen, select ",[50,520,521],{},"Simulator",[57,523,524,525,119],{},"From the accelerometer menu below the simulator, select ",[50,526,527],{},"Shake",[57,529,150,530,533],{},[50,531,532],{},"Send",". This simulates a shake and shows a number on the simulated micro:bit.",[18,535,536],{},[141,537],{"alt":538,"src":539},"The EduBlocks simulator","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002FSimulatorPreview.webp",[507,541,543],{"id":542},"on-a-real-microbit","On a real micro:bit",[18,545,546],{},"Once your code's downloaded, give the micro:bit a shake. Each shake bumps your step count up by one on the LED matrix. Pop a battery pack on, attach it to your wrist or ankle, and head outside to watch your steps climb.",[14,548,550],{"title":549},"Try it yourself",[551,552,553],"try-it",{},[18,554,555,558,559,561,562,564,565,568,569,572],{},[50,556,557],{},"Challenge:"," Set yourself a step goal! Add another ",[175,560,291],{}," block inside your ",[175,563,246],{}," loop that checks when ",[175,566,567],{},"steps >= 20",". When you hit it, use a ",[175,570,571],{},"display.show"," block to show a happy face to celebrate, then press button A to reset and go again.",[14,574,576],{"title":575},"Stuck? Quick fixes",[577,578,579,592],"table",{},[580,581,582],"thead",{},[583,584,585,589],"tr",{},[586,587,588],"th",{},"Problem",[586,590,591],{},"Try this",[593,594,595,611,626,645,657,665],"tbody",{},[583,596,597,601],{},[598,599,600],"td",{},"The number doesn't go up when I shake it",[598,602,603,604,607,608,610],{},"Check your ",[175,605,606],{},"steps += 1"," block is inside the ",[175,609,320],{}," block. Give the micro:bit one firm, clear shake.",[583,612,613,616],{},[598,614,615],{},"The count jumps up by lots at once",[598,617,618,619,622,623,625],{},"A big wobble can look like several shakes. Try one single, definite shake, and check the ",[175,620,621],{},"sleep(500)"," block is inside your ",[175,624,246],{}," loop so each shake has time to register.",[583,627,628,631],{},[598,629,630],{},"Nothing shows on the LED matrix",[598,632,633,634,607,636,638,639,641,642,644],{},"Make sure the ",[175,635,209],{},[175,637,466],{}," block (in place of \"Hello World\"), and that ",[175,640,466],{}," is inside your ",[175,643,246],{}," loop.",[583,646,647,650],{},[598,648,649],{},"Button A won't reset the steps",[598,651,652,653,607,655,182],{},"Check the ",[175,654,215],{},[175,656,428],{},[583,658,659,662],{},[598,660,661],{},"The number doesn't update while I keep shaking",[598,663,664],{},"Don't worry, it's still counting! The micro:bit can't show the new number while it's busy scrolling the last one. Stop shaking for a moment and the latest total will scroll across.",[583,666,667,670],{},[598,668,669],{},"My code won't download to the micro:bit",[598,671,672],{},"Use a data USB cable (not a charge-only one), make sure the micro:bit is paired, and try a different USB port.",{"title":674,"searchDepth":675,"depth":675,"links":676},"",2,[],false,"10 mins","md","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002Fmicrofit-cover.png",[682,25,683,684],"The micro:bit EduBlocks editor","micro USB cable","battery pack for the micro:bit (optional)","edublocks","Beginner",{},true,"\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit","\u002Flearn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002Fmicrofit.pdf","microbit",{"title":6,"description":674},"learn\u002Fmicrobit\u002Fedublocks\u002Fmicrofit\u002Findex",null,"tzU_uk-YKS70h5kDkgwOa0ZIphgODlCg80d72CF0YJo",1785347039745]