The next thing we are going to look at is how to use the built-in Bluetooth radio, the micro bit uses a cut down version of the Bluetooth.

Transcript

Hello again, Clinton here from Core Electronics, welcome to Chapter 5 of our Micro Python for Micro Bits workshop, in this one we're going to be having a look at the radio on the Micro Bits and list structures. So, what we're going to be making is this little program that lets us choose a letter and send it to other Micro Bits boards around, so let's first have a talk about how this radio works.

The Micro Bits doesn't have very much memory for storing programs, as compared to something like the more advanced Micro Python boards like the ESP 32, so we can't maintain Micro Python and the entire Bluetooth libraries, so instead what it has is a cut-down version of the Bluetooth library, which allows us to send messages out into the world, where any Micro Bits around that a-listening will receive that message.

So, when I press the button to send, all the other Micro Bits around can receive that message and read it. This is called a one-to-many system and works much like the radio in your car hence why it's called Radio, there are ways kind of more advanced tricks but we're not going to look into those today we're just going to have a look at how we can send messages to other Micro Bits and how we read them

So let's get started by having a look at the code in this case we're going to import Micro Bits and we're also going to import the radio, the first thing we need to do is actually turn the radio on because by default to save power the Micro Bit has the radio off. So, we turn the radio on by just using "radio.on()" with the brackets then we're going to declare a list. So, this is a particular type of data structure or variable and it lets us store multiple values under the same name. I can add items to the list so if I wanted to I could have more, so I could go "d" keep going like that, I can also have things like strings in here so I can have I could have words, so I could change this to "cat" for example, which would then send that which would then store that value in there. Lists in Python are indexed from 0, so this would be the 0 element this would be 1, this would be 2 and this would be 3 what we're going to do in this program is we're going to create a variable that represents what index we're currently pointing to in this list and so to start with we want to start at the first element so we're going to go with 0, then we want to run the code, so that it will show what character we've currently got selected so we do this we just go, microbit.display.show and then we enter the character in the list that we're indexed up to do this we go alphabet which is the name of our list and then we use square brackets and the number within there will be the index we're pointing to. So "alphIndex" represents the number 0 now, so that will be showing an if I change this to a number, I could go 3, that would be pointing at the final element. If I put minus 1 that will also point at the final element in the list.

So now we're going to use another wild true loop what we're going to do from this point, is we're going to have several "if" statements that will check for several conditions the first of those is going to be if the A button is pressed. So what we're going to do when the A button is pressed is we're going to take our index our alphIndex and we're going to add 1 to it so this will move us forward in the list now something to be aware of in Python is that if we index something that's not in our list. So, if I were to index 3 in the initial condition when I only had 3 elements in the list that would be pointing to the 4th element in the list which isn't there and Python will throw an error and your code will stop working.

So, what we're going to do is we're going to put a condition in to check whether or not we've reached the end of our list and to do this we're going to use the "Len" function, so what this function does is it takes a list and returns the number of elements. So we're going to return three elements or four elements depending on if I've added if you've added a "D" to the code and what this what this means is it will be indexed it will have the number of elements in there but because we're indexing from zero we need to subtract one from that, so that it represents the final element or the final index of the list if we've exceeded our final index we're going to set it back to zero, so we're back at the start then once we've checked to make sure that we're within the range of our list, we're then going to display the character again and so this will display whatever character we're pointing at. We're then going to sleep for a moment just to save computer cycles and we're going to sleep for 125 milliseconds, which relates roughly to the frame rate of a movie.

So the next condition that we're going to check, is this if button B is pressed so when our second button is pressed what we're going to do is we're going to use the label the radio library and then we're going to send alphabet and our index. So, it's important to note here that we don't have to send a single character, we could send entire sentences or even other variables. Once we've sent, we're going to sleep for a moment because that's all we need to do. So, once we hit send that will go out to whichever Micro bits our around little listening. The hardware on the board is always listening for messages coming in and so what it does is it has a cue within itself and it'll keep checking that and grabbing those. So, when we hit send it goes out.

So once we've sent a message we need to actually receive it and so when their role as I said before there it's always listening and collecting these messages but it has a queue of messages and we need to grab those so that we can use them and the command to do that is this "radio.receive" and we're going to see this as incoming. So, most of the time if nothing else is sent a message that queue will be empty when we go to receive it, we'll grab the item from the queue. So if the queue can hold as many as three messages, so if you hold down the button or if multiple messages get sent and that queue gets full, it will stop receiving those messages but once we use this command it takes that message from the queue and opens up the slot.

So once we've received the message as a variable we're going to use this if condition and this one's kind of special in that what it does is it lets us check whether or not that variable exists, if no message is in the queue this will return us false, if there is a message in the queue then it will run this code and what this code does is it goes Microbit.displaye.scroll and this will scroll the (incoming) string so if you changed your variables to words then this would scroll the words.

In my case when I hit A and I hit B it's going to just send the single character, then after the single characters been sent we're going to reshow the character that we're currently indexed at so we know what we're going to send, then after that we're going to sleep the Micro Bit, so that we're not constantly using our processor cycles and power. So now all that remains to do is to flash our code onto the Micro Bit, so we just hit flash wait for that to run our codes there. So, if we have other Micro Bits running the same code, we can change our letter and we can press a and send that to all the other Micro Bits. There are some tricks that we can apply to this code that will make it so that we can stop other people that we don't want reading the messages or we can make it so that we filter out messages we don't want to receive, but we won't cover that in this workshop, but we may cover it in a later tutorial.

In the next workshop, where we're going to be looking is senses and functions. Functions are useful programs that let us useful parts of programs that let us repeat card without having to write it out all over again. Thanks for watching and I'll see you next time.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.