Micro:bit - Morse Transmitter |
Written by Harry Fairhead | ||||
Monday, 01 March 2021 | ||||
Page 3 of 3
A Simple Radio LinkIn the spirit of starting with something simple at both ends of the connection, what we need is something that has minimal implementation. The first thing you have to do is enable the raw radio mode by disabling the BLE mode. For both the V1 and V2 you do this in the same way. In each case you need to find the MicroBitConfig.h file and add the line: #define MICROBIT_BLE_ENABLED 0 In the case of the micro:bit V2 this is the default state, but it is worth making the change to make sure. When you next compile the project a complete rebuild will occur. The simplest pair of programs you can use to check that the radio is working are: Transmitter: #include "MicroBit.h" MicroBit uBit; int main() { uBit.init(); uBit.radio.enable(); uBit.display.print("TX"); while (1) { if (uBit.buttonA.isPressed()) { uBit.display.print("1"); uBit.radio.datagram.send("1"); } uBit.sleep(100); uBit.display.clear(); } return 0; } Receiver: #include "MicroBit.h" MicroBit uBit; void onData(MicroBitEvent e) { ManagedString s = uBit.radio.datagram.recv(); uBit.display.print(s); } int main() { uBit.init(); uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData); uBit.radio.enable(); uBit.display.print("RX"); while (1) { uBit.sleep(100); uBit.display.clear(); } } If you try this out you should see 1 displayed on both micro:bits when Button A is pressed. This may be a simple example, but it is a good starting point for more complex programs. As a more complicated example of using the radio, our final project is to implement a Morse code system using two micro:bits. To do this it would be nice to have some sound and hence a detour into how to make sound on V1 and V2 is justified. Not included in this extract:
Morse Code TransmitterAs a slightly more sophisticated example of using the radio and sound, let’s implement a Morse code transmitter. Morse code is old-fashioned but still relevant and it is worth knowing. The idea is that each letter of the alphabet is coded as a sequence of short and long tones usually called a dot and a dash, for example, the letter A is dot dash. The key idea is that there is no set length of time for a dot or a dash except for the fact that a dash should be three times longer than a dot and letters are separated by a dot length of silence. Creating a Morse code transmission program is just a matter of setting an LED on and starting a sound when Button A is pressed and turning the LED off and stopping the sound when it is released. The simplest way to do this is to make use of the button down and up events to call appropriate functions. This makes the program work on a local micro:bit. If we use the radio to broadcast the events then the exact same program will signal the code being typed to a remote micro:bit running the exact same program: #include "MicroBit.h" MicroBit uBit; void beepOn(MicroBitEvent e) { uBit.io.P0.setAnalogValue(512); uBit.io.P0.setAnalogPeriodUs(1000000 / 500); uBit.display.image.setPixelValue(2, 2, 255); } void beepOff(MicroBitEvent e) { uBit.io.P0.setAnalogValue(0); uBit.display.image.setPixelValue(2, 2, 0); } int main() { uBit.init(); uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE); uBit.radio.enable(); uBit.radio.event.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_DOWN); uBit.radio.event.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_UP); uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_DOWN, beepOn); uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_UP, beepOff); while (1) { uBit.sleep(100); } } If you try this out you will need to change P0 to speaker to run on a V2 and make sound on the built-in speaker. You might be surprised that you can redirect the button up and down events on both devices – surely this would create a feedback loop? The reason it doesn’t is that each micro:bit forwards by radio only the presses on its buttons. This makes the program a two-way Morse communication device. Summary
Micro:bit IoT In C Second EditionBy Harry FairheadBuy from Amazon. Contents
<ASIN:1871962676> <ASIN:B08XPRMK97> To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info <ASIN:B08WTBNJ5Q> <ASIN:B08NLX7773>
|
||||
Last Updated ( Wednesday, 03 March 2021 ) |