Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include "MicroBit.h"
  2.  
  3. MicroBit uBit;//Create an object called uBit
  4. //defining button A
  5. MicroBitButton buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A);
  6. //defining button B
  7. MicroBitButton buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B);
  8. //defining currentData string where incoming data will be stored
  9. ManagedString currentData;
  10. //Defining morse patterns
  11. ManagedString morse[26] = {"..-",     ".-...",    ".-.-.",     ".-..",     "..",     "...-.",    ".--.",     ".....",     "...",     "..---",     ".-.-",     "..-..",    ".--",    ".-.",     ".---",    "..--.",     ".--.-",     "..-.",     "....",     ".-",     "...-",     "....-",     "..--",     ".-..-",     ".-.--",    ".--.."} ;
  12. //Defining letter
  13. 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"};
  14.  
  15. //A method called translate used to translate incoming pattern into a letter
  16. void translate(){
  17.     while(1) {
  18.       //for loop to go through all 26 letters of alphabet
  19.         for(int i =0; i <= 26; i++) {
  20.           //check if the pattern in currentData matches any item in morse ManagedString
  21.             if (currentData == morse[i]) {
  22.               //If so display the letter in the same index of letter ManagedString as in Morse pattern ManagedString
  23.                 uBit.display.print(letter[i]);
  24.             }
  25.         }  
  26.     }
  27. }
  28.  
  29. int main()
  30. {
  31.     // Initialise the micro:bit runtime.
  32.     uBit.init();
  33.     //defining pressed flag
  34.     bool pressed;
  35.     uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
  36.  
  37.     while(1) {
  38.        // time of loop execution
  39.        //defining time variable
  40.       int time = 0;
  41.       //get current time
  42.       uint64_t reading = system_timer_current_time();
  43.       //if button A is pressed do the following
  44.       while (buttonA.isPressed()){
  45.         //call translate method
  46.         translate();
  47.       }
  48.       //If microbit is receiving signal from the other microbit execute while loop
  49.       while(uBit.io.P0.getDigitalValue()==1) {
  50.         pressed = true;
  51.         //Calculate time difference to find if it is a dot or a dash
  52.         time = system_timer_current_time() - reading;
  53.        }
  54.        
  55.        if(pressed){
  56.          //If time is less than 250ms
  57.            if(time < 250){
  58.              //Display "."
  59.              uBit.display.print(".");
  60.              //Add "." to currentData
  61.              currentData = currentData +".";
  62.              uBit.sleep(time);
  63.  
  64.            }
  65.            else
  66.            {
  67.              //Otherwise print "-"
  68.              uBit.display.print("-");
  69.              //Add "-" to currentData
  70.              currentData = currentData +"-";
  71.              uBit.sleep(time);
  72.            }
  73.         }
  74.         //set pressed flag to false so microbit knows button is not pressed
  75.        pressed = false;
  76.        // Clear display
  77.        uBit.display.clear();
  78.  
  79.     }
  80.     release_fiber();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement