131 Week 8 - micro:bit buttons, temps, logs Flashcards
How to access buttons in CODAL
uBit.buttonA, uBit.buttonB and uBit.buttonAB for button A, button B and button A+B respectively
Detecting button presses
.isPressed() returns 1 or 0 if the specified button has been pressed.
Synchronous programming, detection of buttons is done in main() function.
Asynchronous programming
Asynchronous detection determines when something has happened rather than if it has happened.
MicroBitMessageBus class is to listen to events and delivers MicroBitEvents as they occur.
When an event of interest is detected, MicroBitMessageBus class calls a function linked to that event, known as an event handler.
release_fibre()
Used in asynchronous programming to stop exiting main() once code is executed so that we can use messageBus.listen();
Place at the end of the main() function.
Detecting an event
uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
Has 3 arguments:
ID of component to listen to
Event of interest
Event handler to be called if event is called
Wildcard event
Event ID of: MICROBIT_EVT_ANY
Allows event handler to be called if the component raises any events
Event handler
void onButtonAB(MicroBitEvent e)
Name has to match the argument in messageBus.listen()
Has 1 argument which stores the event that was raised
Can use e.value to return the ID of the event that was raised.
MicroBitThermometer
A class that lets you return the surface temperature of the application MCU - not the ambient temperature
Get uncalibrated temp reading
readTemp = uBit.thermometer.getTemperature();
Calibrating the temperature
Can measure ambient temperature if we know the real temperature by calibrating the thermometer:
uBit.thermometer.setCalibration(readTemp-ambientTemp);
readTemp = uBit.thermometer.getTemperature();
Asynchronous temperature reading
uBit.messageBus.listen(MICROBIT_ID_THERMOMETER, MICROBIT_THERMOMETER_EVT_UPDATE, eventHandlerName);
Sampling period
The time between temperature readings.
Can be set using:
uBit.thermometer.setPeriod(time in ms);
Logging data
need #include “MicroBitLog.h”
Allows storing of data in a table like format containing rows of readings or other types of data.
How to log data
beginRow() – open a file and create a new row
logData(“label of column”, value_to_log) - identify the label of the column where a value will be entered in the new row.
endRow() - complete logging and close the file.
Timestamp
A timestamp can be added before data is started to be collected and logged.
Not necessary but it allows you to create a plot of recorded values.
uBit.log.setTimeStamp(TimeStampFormat::Seconds);