MicroBit Project #10 for INF 294

Project description:

Using code for MicroBit built in C/C++, display current temperature in Celsius when button A is clicked and in Fahrenheit when button B is clicked. These temperature readings should be transmitted to a second MicroBit via the radio signal.

I used the MakeCode editor to test the algorithm in the MicroBit framework, then wrote the code in the mbed compiler to create the HEX file and test the program.

/https:/os.mbed.com/media/uploads/tsfarber/basic_logic_for_project_10.png

Following is the JavaScript that corresponds to the blocks in makeCode. This serves as a pretty good algorithm when the image of the blocks cannot be viewed. However, in C this code was not working with event handlers for the button presses so I went with if statements on the code for sending the data. It was also necessary to set the receiving code up with a while loop to execute while value of the transmission was not null. I feel like this code could be expanded to allow for multiple message packets to be sent between units.

let tempF = 0
input.onButtonPressed(Button.A, function () {
    basic.showNumber(input.temperature())
    basic.showString("C")
    radio.sendNumber(input.temperature())
    basic.pause(500)
    basic.clearScreen()
})
input.onButtonPressed(Button.B, function () {
    tempF = Math.round(input.temperature() * 1.8 + 32)
    basic.showNumber(tempF)
    basic.showString("F")
    radio.sendNumber(tempF)
    basic.pause(500)
    basic.clearScreen()
})
radio.onReceivedNumber(function (receivedNumber) {
    basic.showNumber(receivedNumber)
})

https://os.mbed.com/users/tsfarber/code/microbit-10-radioTempData/

There was a problem with committing this file because of changes to some of the libraries. One change was made to disable the BLE to enable the radio, but the commit wants to change a library file and University of Lancaster rather than fork it. Until I can fix this, I will paste the code directly into the notebook. *update, had to create new project and still had some problems with the libraries. If the existing file won't work because libraries are private, the code below works. Just keep in mind the BLE must be disabled when you compile in order for the program to work as expected on the MicroBit.

#include "MicroBit.h"

MicroBitStorage storage;
MicroBitThermometer thermometer(storage);
MicroBit uBit;

float tempC; 
float tempF;
char tempToShare[6];

  
void onData(MicroBitEvent)
{
    while(onData != 0){
        ManagedString s = uBit.radio.datagram.recv();
        uBit.display.scroll(s);
        //ManagedString unit = uBit.radio.datagram.recv();
        //uBit.display.scroll(unit);
    }
}



int main()
{
    // Initialise the micro:bit runtime.
    uBit.init();
    uBit.radio.enable();
    

    uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData);
    

    while(1)
    {
        if (uBit.buttonA.isPressed())
        {
            tempC = thermometer.getTemperature();
            sprintf(tempToShare,"%0.1f",tempC);
            uBit.display.scroll(tempToShare);
            uBit.sleep(200);
            uBit.display.print('C');
            uBit.radio.datagram.send(tempToShare);
            uBit.sleep(1000);
            uBit.display.clear();
            //uBit.radio.datagram.send('C');
        }
        else if (uBit.buttonB.isPressed())
        {
            tempC = thermometer.getTemperature();
            tempF = (int)((tempC*1.8)+32);
            sprintf(tempToShare,"%0.1f",tempF);
            uBit.display.scroll(tempToShare);
            uBit.sleep(200);
            uBit.display.print('F');
            uBit.radio.datagram.send(tempToShare);
            uBit.sleep(1000);
            uBit.display.clear();
            //uBit.radio.datagram.send('F');
        }
            
        uBit.sleep(100);
    }
}


Please log in to post comments.