Programming Morse Code Communicators with Micro:bit Logic
Principles of Conditional Logic and Communication
The study of Boolean logic and conditionals builds upon previously established concepts, such as representing PEMDAS (order of operations) or managing account login credentials.
This lesson focuses on using conditionals and Booleans to facilitate communication between two distinct micro:bit devices.
Conditional statements are essential in this context because the receiving micro:bit must interpret specific incoming signals and decide on a corresponding response.
The core application for this program is the transmission and reception of Morse code, where individual signals represent either a "dash" or a "dot."
Developers must consider the underlying algorithm, which refers to the specific set of instructions the computer uses to understand and process the signals.
The project involves combining familiar programming strategies with new command structures and specialized tools within the micro:bit toolbox.
Documentation and Convention Planning
Documentation is a critical phase that precedes formal coding; it ensures that the programmer and future collaborators understand the logic and conventions used.
In different coding environments, documentation may take the form of inline comments directly within the code. However, when using block-based programming, it is common to maintain documentation in a separate document.
Documentation aids in maintaining consistency throughout the programming process and serves as a reference for specific mappings (e.g., which buttons or numbers correspond to specific Morse code symbols).
Established Morse Code Conventions for this Program: - The Dash: - Assigned to Button A. - Represented as a longer note in Morse code. - Transmitted as the radio signal number . - The Dot: - Assigned to Button B. - Represented as a shorter note in Morse code. - Transmitted as the radio signal number .
Micro:bit Radio Communication Setup
The program utilizes the "Radio" section of the micro:bit toolbox, enabling wireless communication between hardware devices.
Devices must be synchronized to the same frequency to send and receive data successfully. This is accomplished by assigning them to the same radio group.
Radio Group Initialization: - In the
on startblock, the commandradio set groupis used. - For this specific program, the group is set to group number . - Establishing this at the start ensures all devices are on the correct frequency the moment the program begins.
Input Triggers and Data Transmission
User interaction is managed through input triggers, represented by the pink blocks in the Micro:bit interface.
Two primary input triggers are utilized:
on button A pressedandon button B pressed.When a button is pressed, the micro:bit must broadcast a specific numerical signal to any other device in its radio group.
Trigger Logic: - If Button A is pressed: The command
radio send number 0is executed. - If Button B is pressed: The commandradio send number 1is executed.At this stage of development, the micro:bits are capable of sending values but lack the logic to interpret them or provide meaningful output.
Receiving Signals and Conditional Branching
To process incoming data, the programmer must implement a radio listener that triggers whenever a number is received.
An "if-then-else" conditional block (found in the logic toolbox) is used to distinguish between the various signals received.
Comparison Logic: - The program uses a comparison operator to evaluate if the
received numberis equal to a specific value. - First condition:if received number == 0. - This uses the logic equality operator to generate a Boolean (true/false) result.
Output Execution: Dash vs. Dot
Based on the outcome of the conditional check, the micro:bit provides both visual and auditory output.
Processing a Dash (received number ): - Visual: The micro:bit uses the
show ledscommand to display five lights across the middle row ( line). - Auditory: The device plays the tonemiddle Cfor a duration of beat.Processing a Dot (received number ): - Visual: The
show ledscommand displays a single LED directly in the center of the grid. - Auditory: The device plays the tonemiddle Cfor a duration of (one quarter) of a beat to distinguish it from the dash.
Post-Processing and Neutralizing the Display
A final command,
clear screen(found in the basic toolbox), is required to return the micro:bit to a neutral state after displaying a signal.Instruction Placement: - The
clear screenblock is placed at the very bottom of the program, outside of the "if-then-else" structure. - This placement ensures the command executes regardless of whether a dash or a dot was received. - It allows the device to await the next signal without the previous icon remaining on the display.
Practical Demonstration and Case Study
During the testing phase, two micro:bits (identified as the yellow micro:bit and the green micro:bit) were used to verify the program's efficacy.
Test Scenario 1 (Dot Transmission): - The yellow micro:bit sent three dots by pressing Button B three times. - Results: The green micro:bit successfully received the signals, displaying a center dot and emitting a quick beep for each press.
Test Scenario 2 (Dash Transmission): - The yellow micro:bit sent three dashes by pressing Button A three times. - Results: The green micro:bit displayed three long dashes and played three longer auditory notes.
Test Scenario 3 (Bidirectional Communication): - The green micro:bit attempted to send three dots to the yellow micro:bit. - Results: The transmission was successful in the opposite direction, confirming that both devices can function as both transmitters and receivers.
Theoretical Framework: Key Terminology
Bluetooth: A wireless communication standard that allows digital devices to exchange data over short distances. In the context of the micro:bit, this technology facilitates the radio-wave transmissions used in the Morse code project. It is effective for localized range but lacks the power for long-distance (e.g., global) communication.
Conditional Statement: A fundamental programming construct (such as the if-else block) that permits a computer to make decisions based on specific inputs. It directs the flow of the program based on whether conditions are met.
Boolean Values: These represent binary logic states:
trueorfalse. Because Boolean logic is binary, it eliminates ambiguity, allowing computers to operate consistently and predictably.Low-Level Hardware Context: While programmers interact with high-level code blocks, the underlying hardware operates via transistors and logic gates. These physical components direct the flow of electricity along different paths to execute the logic defined in the software. Programming at a higher level (blocks or scripts) allows developers to focus on conceptual logic rather than physical electrical implementation.