Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MicroBit.h"
- MicroBit uBit;//Create an object called uBit
- //defining button A
- MicroBitButton buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A);
- //defining button B
- MicroBitButton buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B);
- //defining currentData string where incoming data will be stored
- ManagedString currentData;
- //Defining morse patterns
- ManagedString morse[26] = {"..-", ".-...", ".-.-.", ".-..", "..", "...-.", ".--.", ".....", "...", "..---", ".-.-", "..-..", ".--", ".-.", ".---", "..--.", ".--.-", "..-.", "....", ".-", "...-", "....-", "..--", ".-..-", ".-.--", ".--.."} ;
- //Defining letter
- ManagedString letter[26] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K" , "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
- //A method called translate used to translate incoming pattern into a letter
- void translate(){
- while(1) {
- //for loop to go through all 26 letters of alphabet
- for(int i =0; i <= 26; i++) {
- //check if the pattern in currentData matches any item in morse ManagedString
- if (currentData == morse[i]) {
- //If so display the letter in the same index of letter ManagedString as in Morse pattern ManagedString
- uBit.display.print(letter[i]);
- }
- }
- }
- }
- int main()
- {
- // Initialise the micro:bit runtime.
- uBit.init();
- //defining pressed flag
- bool pressed;
- uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
- while(1) {
- // time of loop execution
- //defining time variable
- int time = 0;
- //get current time
- uint64_t reading = system_timer_current_time();
- //if button A is pressed do the following
- while (buttonA.isPressed()){
- //call translate method
- translate();
- }
- //If microbit is receiving signal from the other microbit execute while loop
- while(uBit.io.P0.getDigitalValue()==1) {
- pressed = true;
- //Calculate time difference to find if it is a dot or a dash
- time = system_timer_current_time() - reading;
- }
- if(pressed){
- //If time is less than 250ms
- if(time < 250){
- //Display "."
- uBit.display.print(".");
- //Add "." to currentData
- currentData = currentData +".";
- uBit.sleep(time);
- }
- else
- {
- //Otherwise print "-"
- uBit.display.print("-");
- //Add "-" to currentData
- currentData = currentData +"-";
- uBit.sleep(time);
- }
- }
- //set pressed flag to false so microbit knows button is not pressed
- pressed = false;
- // Clear display
- uBit.display.clear();
- }
- release_fiber();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement