Now that we have everything set up it’s time to have some fun! we are going to write some code that will let us interact with the micro:bit through buttons and an LED.
Hello again, Clinton here from Core Electronics, welcome to chapter 3 of our Micro Python for Micro Bit workshop. In this one, we're finally going to start doing some actual Micro Python so this should be fun, we're going to wire up a switch, so that we can turn on and off an LED by pressing the "A" button like this.
So, let's get started by having a look at what we're going to need and wiring up the circuit. So what we're going to need is, we're going to need two alligator clips, an LED and a 330 ohm resistor and what we want to do is we want to wire our first alligator clip from pad zero to the positive leg, which will be the long leg on your LED, then from the short leg of the LED we want to go to our 330 ohm resistor, then from the bottom of our resistor we want to go to the second alligator clip which we will take to ground, there is a picture of this in the tutorial description, so if you get lost don't worry. So, once we have our circuit wired together, we now need to have a look at the code required to make this work. So, let's get straight into that.
Okay, so the codes available in the thing and you can either copy and paste that in and go through with me or you can write it out as I explain what the code is. So, the first couple of lines here we're going to be importing libraries for Python or in this case Micro Python and this works in very much the same way that it does in normal Python. So, we just import Micro Bit which this is the library associated with the hardware we're using, we're also going to import time which is a library for dealing with managing real time as human see it not as time as a computer season which is in clock cycles. The next couple of lines we're going to be declaring variables and variables are containers for code, so they hold different bits of information in Python. They can hold any information we can assign them to numbers to letters to strings of letters or as if we've done here, we can assign them to objects and these objects are our button "A" which represents this button here and pin zero which represents this pin here. So now that we've saved those as these names, we no longer need to write this Micro Bit pin zero we can just write output pin the next line here is wild true and the while true loop just continues to run that code forever. So, anything that's tabbed in from that loop will keep running, if I were to put another line down here and put another line of code and this piece of code would never run because the wild true loop would just continue to run forever.
So, inside this tab section of code where we're running inside the while loop, we've created some other code and the first one we're going to look at is an, "if" statement. So an "if" statement will have a look at whatever is evaluated here and if this term is true, evaluates to true it will run the code that's tabbed in from it, if it's false it will ignore this block of code and move on to this next step, where we have "microbit.sleep", which this will sleep for 125 milliseconds. If in this case, our code does evaluate true, we then get to this second option which is another if statement, but this one's slightly different in that it has another statement associated with it. So, what this piece of code does is says, if this evaluates to true, I want you to do this thing if it's not true I want you to do this thing. This is useful logic for programming and for you'll use it a lot and if you've already been programming you will be very familiar with these structures.
The next thing that we'll look at here is these functions here what we're evaluating, so this my button is pressed equals 1, what we're doing is we're checking this my button so the Micro Bit button A and we're having a look and seeing if it's pressed because we'll ever the buttons pressed this will return 1, whenever it's not pressed it will return 0. So, if my button is pressed is equal to 1, then we run this code we can then read from our output pin, so what we're doing here is we're saying is the pin already turned on. So is digital output 1, this digital output 0 equal to 1 which is its logic state, if it's equal to 1 we're going to then switch to write mode and so this will then write a 0 to the output pin, which will turn off the LED. If the LED is already off this code will go, oh the LEDs not on so I'm going to turn it on, so this digital.write, this outputPin.write_digital will write 1 and then turn the LED on.
So, this concept of a state-based switch is important in programming, so this program is aware of when the light is on and off and the program changes behaviour based on that switch, this is called a latching switch and is quite important for computer science and is how memory works in a lot of computers. We then have this time. sleep which functions in a very similar way to the Microbit.sleep except where in seconds so 0.5 is equivalent to 500 down here. I thought it was important to point this out because if you're used to Python and you're coming to Micro Python and you use the Microbit.sleep function you might try to use the other way or vice-versa, so I thought it was worth including. Now that we have our code all written in, we can then simply flash this to the Micro Bit, once that codes flashed on that you should be able to press your A button and turn on the LED pressing it again and turn off the LED.
So, I hope you've enjoyed and learned something from this tutorial and in the next tutorial we're going to start looking at analog inputs and outputs, so I'll see you next time and thanks again for watching.