Micro:bit Binary Communicator
by 8330728 in Circuits > Microcontrollers
561 Views, 1 Favorites, 0 Comments
Micro:bit Binary Communicator
I made a micro:bit communicator that allows people to communicate in the code binary. Micro:bit 1 works as the transmitter and Micro:bit 2 works as the receiver. This means that Micro:bit 1 can send messages to Micro:bit 2 in binary. The buttons A+B must be pressed simultaneously to activate Micro:bit 1, then Button A can be pressed to send 0 and Button B can be pressed to send 1. Hence, the Micro:bit's work as a coded communication network.
Supplies
This project was entirely constructed in TinkerCad for circuits.
Creating a New Project
- Go to the TinkerCad website at; https://www.tinkercad.com
- Log in or create account
- Go to the circuits section
- Press create new circuit
Setting Up the Circuits
- Drag and drop two Micro:bits into the workspace
- Name the Micro:bits 1 & 2 so that they don't get confused
- Enter the code section
- Change the code from blocks to text
- Check it says ''# Python code'' at the top
Micro:bit 1 - Coding the Start Pt1
Micro:bit 1 is exclusively a transmitter which can be used to send messages to Micro:bit 2 which is a receiver. It can be adapted to be both my adding the receiver code from Micro:bit 2 to Micro:bit 1.
- Select Micro:bit 1 and enter the code section
- Here we are going to write the lines of code that start the program.
- The first line starts a forever loop
- The next ensures that it keeps going so long as the variable remains true(which can't be changed, so it will stay true).
- After this the Micro:bit shows a smiley face (basic.show_icon(IconNames.Happy)) to prove its on and the program is working.
- After a couple seconds, the micro:bit provides the instructions; PRESS AB TO START. (basic.show_string("PRESS AB TO START"))
- Lastly, it sets the radio group to 14 (radio.set_group(14)) so that the two micro:bits are on the same radio group.
- On a new line, with no hashtag, type;
def on_forever():
if True:
basic.show_icon(IconNames.Happy)
basic.pause (1000)
basic.show_string("PRESS AB TO START")
radio.set_group(14)
#(If It doesn't work check the indentation)
Micro:bit 1 - Coding the Start Pt2
This section of code displays the remainder of the instructions
- The if statement, says that if the buttons A & B are pressed simultaneously the program begins
- It then displays the letter A (basic.show_string("A")) and number 0 (basic.show_number(0)), This shows that the button A will send the number 0 to micro:bit 2
- The micro:bit then shows the letter B (basic.show_string("B")) and number 1(basic.show_number(1)), This shows that the button B will send the number 1 to micro:bit 2
- basic.pause(1000), means that the program will wait for 1 second.
- After the previous line of code type;
if input.button_is_pressed(Button.AB):
basic.show_string("A")
basic.pause(1000)
basic.show_number(0)
basic.pause(1000)
basic.show_string("B")
basic.pause(1000)
basic.show_number(1)
basic.pause(2000)
#(If It doesn't work check the indentation)
Micro:bit 1 - Coding Button A
The following section of code programs button A to send 0 when pressed then display a tick to show it was sent
- if input.button_is_pressed(Button.A): - Checks that button A was pressed and starts this section of code
- basic.clear_screen() - Clears what is currently on the micro:bit's screen
- basic.show_string("0") - Displays the 0 which will be sent to micro:bit 2
- basic.pause(2000) - Waits for 2 seconds to make sure you see what will be sent
- radio.send_string("text") - Sends the message to micro:bit 2
- basic.clear_screen() - Clears the screen
- basic.show_icon(IconNames.Yes) - Shows a tick to confirm the message has sent.
- Type;
if input.button_is_pressed(Button.A):
basic.clear_screen()
basic.show_string("0")
basic.pause(2000)
radio.send_string("text")
basic.clear_screen()
basic.show_icon(IconNames.Yes)
Micro:bit 1 - Programming Button B
The following section of code programs button B to send 1 when pressed then display a tick to show it was sent
- if input.button_is_pressed(Button.B): - Checks that button B was pressed and starts this section of code
- basic.clear_screen() - Clears what is currently on the micro:bit's screen
- basic.show_number(1) - Displays the 1 which will be sent to micro:bit 2
- basic.pause(2000) - Waits for 2 seconds to make sure you see what will be sent
- radio.send_number(1) - Sends the message to micro:bit 2
- The reason that button A sends a string and button B sends a number is because if they are the same micro:bit can occasionally confuse the signals, doing it this way prevents any chance of this happening.
- basic.clear_screen() - Clears the screen
- basic.show_icon(IconNames.Yes) - Shows a tick to confirm the message has sent.
- Type;
if input.button_is_pressed(Button.B):
basic.clear_screen()
basic.show_number(1)
basic.pause(2000)
radio.send_number(1)
basic.clear_screen()
basic.show_icon(IconNames.Yes)
Micro:bit 1 - Last Step
This last bit of code shows if the program has failed and activates the code
- else: - This means that the line of indented code after will only occur if the initial if statement if not True
- basic.show_icon(IconNames.Sad) - This displays a sad face so that the user can visually see that the program has failed.
- basic.forever(on_forever) - This calls/activates the forever loop that was defined earlier. All of the code is within this loop, without this call line nothing will work.
- Type;
else:
basic.show_icon(IconNames.Sad)
basic.forever(on_forever)
Micro:bit 2 - Programming the Start
This section of code is used to define variables and the forever loop
In this section the forever loop is exclusively for the radio group as this micro:bit is a receiver not a transmitter. It can be adapted to be both by adding the transmitter code from Micro:bit 1 to Micro:bit 2.
- Change the Micro:bit being coded from Micro:bit 1 to Micro:bit 2 using the drop down in the right hand corner.
- Change the code style from blocks to text
- value = 1 - This defines the value variable as 1, this is what Micro:bit 1 will be sending via radio when it wants the number 1 displayed
- text = "text" - This defines the text variable as text, this is what Micro:bit 1 will be sending via radio when it wants the number 0 displayed
- def on_forever(): - Creates a forever loop for the radio signal to be in so that it never changes
- radio.set_group(14) - Sets the radio group to 14 so that Micro:bit 1 & 2 are on the same radio group.
- basic.forever(on_forever) - Calls/activates the forever loop
- Type;
value = 1
text = "text"
def on_forever():
radio.set_group(14)
basic.forever(on_forever)
Micro:bit 2 - Programming the Receiver of Button A
This section of code is used to define what will happen when the button A signal is received
- def on_radio_received_number(arg0): - This starts the definition of what will happen when button A's signal is recieved
- global value, value = arg0 - This defines value as 'global' or the same across the whole program and states that value is equal to arg0
- basic.show_number(1) - displays the number that button A sent, which is number 1 on Micro:bit 2
- basic.pause(2000) - Pauses the program for 2 seconds so that the user can see the display
- basic.clear_screen() -Clears the screen
- basic.show_icon(IconNames.Yes) - Displays a Tick to show that the message was received
- basic.clear_screen() - Clears the screen
- radio.on_received_number(on_radio_received_number) - Calls/activates the code block
- Type;
def on_radio_received_number(arg0):
global value
value = arg0
basic.show_number(1)
basic.pause(2000)
basic.clear_screen()
basic.show_icon(IconNames.Yes)
basic.pause(2000)
basic.clear_screen()
radio.on_received_number(on_radio_received_number)
Micro:bit 2 - Programming the Receiver of Button B
This section of code is used to define what will happen when the button B signal is received
- def on_radio_received_string(arg0): - This starts the definition of what will happen when button B's signal is recieved
- global text, text = arg0 - This defines value as 'global' or the same across the whole program and states that text is equal to arg0
- basic.show_number(0) - displays the number that button A sent, which is number 0 on Micro:bit 2
- basic.pause(2000) - Pauses the program for 2 seconds so that the user can see the display
- basic.clear_screen() - Clears the screen
- basic.show_icon(IconNames.Yes) -
- basic.pause(2000) - Pauses the program for 2 seconds so that the user can see the display
- basic.clear_screen() - Clears the screen
- radio.on_received_string(on_radio_received_string) - Calls/activates the code block just defined
- Type;
def on_radio_received_string(arg0):
global text
text = arg0
basic.show_number(0)
basic.pause(2000)
basic.clear_screen()
basic.show_icon(IconNames.Yes)
basic.pause(2000)
basic.clear_screen()
radio.on_received_string(on_radio_received_string)