[{"data":1,"prerenderedAt":548},["ShallowReactive",2],{"microbit-python":3},[4],{"id":5,"title":6,"body":7,"description":526,"draft":529,"duration":530,"extension":531,"image":532,"kit":533,"language":537,"level":538,"meta":539,"navigation":540,"path":541,"pdf":542,"platform":543,"seo":544,"stem":545,"youtube":546,"__hash__":547},"tutorials\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002Findex.md","MicroFit - Your very own Step Counter",{"type":8,"value":9,"toc":525},"minimark",[10],[11,12,13,87,93,100,158,190,212,249,270,315,333,369,401,428],"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,135,142,152],{"title":102},"Set up your micro:bit Python Editor",[104,105,106,109,122,128],"ol",{},[57,107,108],{},"Open your web browser. We recommend Google Chrome or Microsoft Edge.",[57,110,111,112,121],{},"In the address bar type ",[50,113,114],{},[115,116,120],"a",{"href":117,"rel":118},"https:\u002F\u002Fpython.microbit.org",[119],"nofollow","python.microbit.org",".",[57,123,124,125,121],{},"Give your project a name: select the project name box at the top of the editor and type ",[50,126,127],{},"MicroFit",[57,129,130,131,134],{},"Close the left-hand panel by selecting the ",[50,132,133],{},"arrow"," pointing to the left.",[18,136,137],{},[138,139],"img",{"alt":140,"src":141},"Closing the left-hand panel","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FClosingPanel.webp",[104,143,145],{"start":144},5,[57,146,147,148,151],{},"Delete the code on lines ",[50,149,150],{},"5 to 9"," in the main code area.",[18,153,154],{},[138,155],{"alt":156,"src":157},"The Python editor code area","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FCodeArea.webp",[14,159,161,184],{"title":160},"Create a variable",[18,162,163,164,168,169,171,172,175,176,179,180,183],{},"On line 3, type ",[165,166,167],"code",{},"steps = 0"," to create a ",[22,170,62],{"slug":62}," called ",[50,173,174],{},"steps",", set to ",[50,177,178],{},"0"," for when the code starts. Press ",[50,181,182],{},"Enter"," to make a new line.",[18,185,186],{},[138,187],{"alt":188,"src":189},"Creating the steps variable","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FS1_CreatingVariable.webp",[14,191,193,206],{"title":192},"Create a while True loop",[18,194,195,196,199,200,202,203,205],{},"Type ",[165,197,198],{},"while True:"," and press ",[50,201,182],{},". This creates a ",[22,204,78],{"slug":77},", so anything inside it runs while the condition stays true. Notice your cursor automatically indents, that's how Python shows what's inside the loop.",[18,207,208],{},[138,209],{"alt":210,"src":211},"Creating a while True loop","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FS2_CreatingWhileLoop.webp",[14,213,215,230,243],{"title":214},"Detect a shake",[104,216,217,225],{},[57,218,195,219,199,222,224],{},[165,220,221],{},"if accelerometer.was_gesture('shake'):",[50,223,182],{},". The cursor indents again.",[57,226,195,227,121],{},[165,228,229],{},"steps += 1",[18,231,232,233,235,236,239,240,242],{},"This ",[22,234,85],{"slug":84}," adds ",[50,237,238],{},"1"," to your ",[50,241,174],{}," variable every time the micro:bit detects a shake.",[18,244,245],{},[138,246],{"alt":247,"src":248},"Detecting a shake","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FS3_DetectingAShake.webp",[14,250,252,264],{"title":251},"Pause the code",[18,253,195,254,257,258,260,261,263],{},[165,255,256],{},"sleep(500)",". This pauses the code for half a second, giving the micro:bit time to do other things, like check for the button press we'll add next. Press ",[50,259,182],{},", then delete the indent so the cursor lines back up with the ",[165,262,221],{}," line.",[18,265,266],{},[138,267],{"alt":268,"src":269},"Pausing the code","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FS4_PausingTheCode.webp",[14,271,273,309],{"title":272},"Reset the steps with button A",[104,274,275,287,302],{},[57,276,195,277,199,280,282,283,286],{},[165,278,279],{},"if button_a.was_pressed():",[50,281,182],{},". This detects a ",[22,284,285],{"slug":285},"button"," press, and the cursor indents.",[57,288,195,289,291,292,294,295,297,298,301],{},[165,290,167],{}," to reset the ",[50,293,174],{}," variable back to ",[50,296,178],{}," when button ",[50,299,300],{},"A"," is pressed.",[57,303,304,305,260,307,263],{},"Press ",[50,306,182],{},[165,308,279],{},[18,310,311],{},[138,312],{"alt":313,"src":314},"Resetting the steps to zero","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FS5_ResettingSteps.webp",[14,316,318,327],{"title":317},"Show the number of steps",[18,319,195,320,323,324,326],{},[165,321,322],{},"display.scroll(steps)",". This scrolls the number of steps across the ",[22,325,37],{"slug":36}," on the front of the micro:bit.",[18,328,329],{},[138,330],{"alt":331,"src":332},"Displaying the number of steps","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FS6_DisplayingSteps.webp",[14,334,336,363],{"title":335},"Downloading Your Code",[104,337,338,341,351,358],{},[57,339,340],{},"Connect the micro:bit to the computer with the micro USB cable.",[57,342,343,344,347,348,121],{},"Select the ",[50,345,346],{},"three dots"," next to ",[50,349,350],{},"Send to micro:bit",[57,352,353,354,357],{},"Select ",[50,355,356],{},"Connect"," and follow the on-screen prompts.",[57,359,353,360,362],{},[50,361,350],{}," to download the code.",[18,364,365],{},[138,366],{"alt":367,"src":368},"Downloading your code","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FDownloadingCode.webp",[14,370,372,377,388,394,398],{"title":371},"Run it and Watch",[373,374,376],"h3",{"id":375},"in-the-web-browser","In the web browser",[18,378,379,380,383,384,387],{},"The Python editor has a built-in micro:bit simulator, handy if you haven't got a micro:bit to hand. Below the simulator you'll see the ",[50,381,382],{},"shake"," function is already selected. Select the ",[50,385,386],{},"play"," button next to it to simulate a shake and show your step count on the LED matrix.",[18,389,390],{},[138,391],{"alt":392,"src":393},"The Python editor simulator","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002FSimulatorDemo.webp",[373,395,397],{"id":396},"on-a-real-microbit","On a real micro:bit",[18,399,400],{},"Once your code's downloaded, give the micro:bit a shake and watch the step counter climb. Why not go further? Attach a battery pack, strap the micro:bit to you or pop it in your pocket, and head outside to see how many steps you can rack up.",[14,402,404],{"title":403},"Try it yourself",[405,406,407],"try-it",{},[18,408,409,412,413,416,417,419,420,423,424,427],{},[50,410,411],{},"Challenge:"," Set yourself a step goal! Add another ",[165,414,415],{},"if"," statement inside your ",[165,418,198],{}," loop that checks ",[165,421,422],{},"if steps >= 20:",". When you hit it, type ",[165,425,426],{},"display.show(Image.HAPPY)"," to show a happy face and celebrate, then press button A to reset and go again.",[14,429,431],{"title":430},"Stuck? Quick fixes",[432,433,434,447],"table",{},[435,436,437],"thead",{},[438,439,440,444],"tr",{},[441,442,443],"th",{},"Problem",[441,445,446],{},"Try this",[448,449,450,465,479,496,504,517],"tbody",{},[438,451,452,456],{},[453,454,455],"td",{},"The number doesn't go up when I shake it",[453,457,458,459,461,462,464],{},"Check your ",[165,460,229],{}," line is indented inside the ",[165,463,221],{}," statement. Give the micro:bit one firm, clear shake.",[438,466,467,470],{},[453,468,469],{},"The count jumps up by lots at once",[453,471,472,473,475,476,478],{},"A big wobble can look like several shakes. Try one single, definite shake, and check the ",[165,474,256],{}," line is inside your ",[165,477,198],{}," loop so each shake has time to register.",[438,480,481,484],{},[453,482,483],{},"Nothing shows on the LED matrix",[453,485,486,487,489,490,492,493,495],{},"Make sure you've typed ",[165,488,322],{}," with ",[165,491,174],{}," inside the brackets, and that the line is indented inside your ",[165,494,198],{}," loop.",[438,497,498,501],{},[453,499,500],{},"The number doesn't change straight away when I shake fast",[453,502,503],{},"That's okay, it's still counting! The micro:bit can't show the new number while the last one is still scrolling. Stop shaking for a moment and your latest total will pop up.",[438,505,506,509],{},[453,507,508],{},"Button A won't reset the steps",[453,510,511,512,461,514,516],{},"Check the ",[165,513,167],{},[165,515,279],{}," statement.",[438,518,519,522],{},[453,520,521],{},"My code won't download to the micro:bit",[453,523,524],{},"Use a data USB cable (not a charge-only one), make sure the micro:bit is paired, and try a different USB port.",{"title":526,"searchDepth":527,"depth":527,"links":528},"",2,[],false,"10 mins","md","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002Fmicrofit-cover.png",[534,25,535,536],"The micro:bit Python editor","micro USB cable","battery pack for the micro:bit (optional)","python","Beginner",{},true,"\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit","\u002Flearn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002Fmicrofit.pdf","microbit",{"title":6,"description":526},"learn\u002Fmicrobit\u002Fpython\u002Fmicrofit\u002Findex",null,"p_3jg7SGKcFENlFKrxBMxq-YhNgUFCq2Z23TeC2Iv7s",1785347039830]