Using C/C++, program reads temperature and displays in Celsius if button A is pressed or Fahrenheit if button B is pressed. Uses radio to transmit temp to a second MicroBit

Dependencies:   microbitI

Committer:
tsfarber
Date:
Tue Nov 26 22:37:48 2019 +0000
Revision:
1:5cf655a789e0
Parent:
0:b43412199f81
Tested and confirmed working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tsfarber 0:b43412199f81 1 /*
tsfarber 0:b43412199f81 2 The MIT License (MIT)
tsfarber 0:b43412199f81 3
tsfarber 0:b43412199f81 4 Copyright (c) 2016 British Broadcasting Corporation.
tsfarber 0:b43412199f81 5 This software is provided by Lancaster University by arrangement with the BBC.
tsfarber 0:b43412199f81 6
tsfarber 0:b43412199f81 7 Permission is hereby granted, free of charge, to any person obtaining a
tsfarber 0:b43412199f81 8 copy of this software and associated documentation files (the "Software"),
tsfarber 0:b43412199f81 9 to deal in the Software without restriction, including without limitation
tsfarber 0:b43412199f81 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
tsfarber 0:b43412199f81 11 and/or sell copies of the Software, and to permit persons to whom the
tsfarber 0:b43412199f81 12 Software is furnished to do so, subject to the following conditions:
tsfarber 0:b43412199f81 13
tsfarber 0:b43412199f81 14 The above copyright notice and this permission notice shall be included in
tsfarber 0:b43412199f81 15 all copies or substantial portions of the Software.
tsfarber 0:b43412199f81 16
tsfarber 0:b43412199f81 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tsfarber 0:b43412199f81 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tsfarber 0:b43412199f81 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
tsfarber 0:b43412199f81 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tsfarber 0:b43412199f81 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
tsfarber 0:b43412199f81 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
tsfarber 0:b43412199f81 23 DEALINGS IN THE SOFTWARE.
tsfarber 0:b43412199f81 24 */
tsfarber 0:b43412199f81 25
tsfarber 0:b43412199f81 26 #include "MicroBit.h"
tsfarber 0:b43412199f81 27
tsfarber 0:b43412199f81 28 MicroBitStorage storage;
tsfarber 0:b43412199f81 29 MicroBitThermometer thermometer(storage);
tsfarber 0:b43412199f81 30 MicroBit uBit;
tsfarber 0:b43412199f81 31
tsfarber 0:b43412199f81 32 float tempC;
tsfarber 0:b43412199f81 33 float tempF;
tsfarber 0:b43412199f81 34 char tempToShare[6];
tsfarber 0:b43412199f81 35
tsfarber 0:b43412199f81 36
tsfarber 0:b43412199f81 37 void onData(MicroBitEvent)
tsfarber 0:b43412199f81 38 {
tsfarber 0:b43412199f81 39 while(onData != 0){
tsfarber 0:b43412199f81 40 ManagedString s = uBit.radio.datagram.recv();
tsfarber 0:b43412199f81 41 uBit.display.scroll(s);
tsfarber 0:b43412199f81 42
tsfarber 0:b43412199f81 43 }
tsfarber 0:b43412199f81 44 }
tsfarber 0:b43412199f81 45
tsfarber 0:b43412199f81 46
tsfarber 0:b43412199f81 47
tsfarber 0:b43412199f81 48 int main()
tsfarber 0:b43412199f81 49 {
tsfarber 0:b43412199f81 50 // Initialise the micro:bit runtime.
tsfarber 0:b43412199f81 51 uBit.init();
tsfarber 0:b43412199f81 52 uBit.radio.enable();
tsfarber 0:b43412199f81 53
tsfarber 0:b43412199f81 54
tsfarber 0:b43412199f81 55 uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData);
tsfarber 0:b43412199f81 56
tsfarber 0:b43412199f81 57
tsfarber 0:b43412199f81 58 while(1)
tsfarber 0:b43412199f81 59 {
tsfarber 0:b43412199f81 60 if (uBit.buttonA.isPressed())
tsfarber 0:b43412199f81 61 {
tsfarber 0:b43412199f81 62 tempC = thermometer.getTemperature();
tsfarber 0:b43412199f81 63 sprintf(tempToShare,"%0.1f",tempC);
tsfarber 0:b43412199f81 64 uBit.display.scroll(tempToShare);
tsfarber 0:b43412199f81 65 uBit.sleep(200);
tsfarber 0:b43412199f81 66 uBit.display.print('C');
tsfarber 0:b43412199f81 67 uBit.radio.datagram.send(tempToShare);
tsfarber 0:b43412199f81 68 uBit.sleep(1000);
tsfarber 0:b43412199f81 69 uBit.display.clear();
tsfarber 0:b43412199f81 70
tsfarber 0:b43412199f81 71 }
tsfarber 0:b43412199f81 72 else if (uBit.buttonB.isPressed())
tsfarber 0:b43412199f81 73 {
tsfarber 0:b43412199f81 74 tempC = thermometer.getTemperature();
tsfarber 0:b43412199f81 75 tempF = (int)((tempC*1.8)+32);
tsfarber 0:b43412199f81 76 sprintf(tempToShare,"%0.1f",tempF);
tsfarber 0:b43412199f81 77 uBit.display.scroll(tempToShare);
tsfarber 0:b43412199f81 78 uBit.sleep(200);
tsfarber 0:b43412199f81 79 uBit.display.print('F');
tsfarber 0:b43412199f81 80 uBit.radio.datagram.send(tempToShare);
tsfarber 0:b43412199f81 81 uBit.sleep(1000);
tsfarber 0:b43412199f81 82 uBit.display.clear();
tsfarber 0:b43412199f81 83
tsfarber 0:b43412199f81 84 }
tsfarber 0:b43412199f81 85 uBit.sleep(100);
tsfarber 0:b43412199f81 86 }
tsfarber 0:b43412199f81 87 }
tsfarber 0:b43412199f81 88