Class project: coded in C/C++ Using onboard sensor, display current temperature in Celsius when button A is pressed and in Fahrenheit when button B is pressed.

Dependencies:   microbit

Committer:
tsfarber
Date:
Tue Nov 26 03:30:35 2019 +0000
Revision:
0:874bf65b167e
Tested and verified

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tsfarber 0:874bf65b167e 1 /*
tsfarber 0:874bf65b167e 2 The MIT License (MIT)
tsfarber 0:874bf65b167e 3
tsfarber 0:874bf65b167e 4 Copyright (c) 2016 British Broadcasting Corporation.
tsfarber 0:874bf65b167e 5 This software is provided by Lancaster University by arrangement with the BBC.
tsfarber 0:874bf65b167e 6
tsfarber 0:874bf65b167e 7 Permission is hereby granted, free of charge, to any person obtaining a
tsfarber 0:874bf65b167e 8 copy of this software and associated documentation files (the "Software"),
tsfarber 0:874bf65b167e 9 to deal in the Software without restriction, including without limitation
tsfarber 0:874bf65b167e 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
tsfarber 0:874bf65b167e 11 and/or sell copies of the Software, and to permit persons to whom the
tsfarber 0:874bf65b167e 12 Software is furnished to do so, subject to the following conditions:
tsfarber 0:874bf65b167e 13
tsfarber 0:874bf65b167e 14 The above copyright notice and this permission notice shall be included in
tsfarber 0:874bf65b167e 15 all copies or substantial portions of the Software.
tsfarber 0:874bf65b167e 16
tsfarber 0:874bf65b167e 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tsfarber 0:874bf65b167e 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tsfarber 0:874bf65b167e 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
tsfarber 0:874bf65b167e 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tsfarber 0:874bf65b167e 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
tsfarber 0:874bf65b167e 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
tsfarber 0:874bf65b167e 23 DEALINGS IN THE SOFTWARE.
tsfarber 0:874bf65b167e 24 */
tsfarber 0:874bf65b167e 25
tsfarber 0:874bf65b167e 26 #include "MicroBit.h"
tsfarber 0:874bf65b167e 27
tsfarber 0:874bf65b167e 28 MicroBitStorage storage;
tsfarber 0:874bf65b167e 29 MicroBitThermometer thermometer(storage);
tsfarber 0:874bf65b167e 30 MicroBit uBit;
tsfarber 0:874bf65b167e 31
tsfarber 0:874bf65b167e 32 int temp;
tsfarber 0:874bf65b167e 33 int F;
tsfarber 0:874bf65b167e 34
tsfarber 0:874bf65b167e 35 void onButtonA(MicroBitEvent)
tsfarber 0:874bf65b167e 36 {
tsfarber 0:874bf65b167e 37 temp = thermometer.getTemperature();
tsfarber 0:874bf65b167e 38 uBit.display.scroll(temp);
tsfarber 0:874bf65b167e 39 uBit.sleep(200);
tsfarber 0:874bf65b167e 40 uBit.display.print('C');
tsfarber 0:874bf65b167e 41 uBit.sleep(1000);
tsfarber 0:874bf65b167e 42 uBit.display.clear();
tsfarber 0:874bf65b167e 43
tsfarber 0:874bf65b167e 44 }
tsfarber 0:874bf65b167e 45
tsfarber 0:874bf65b167e 46 void onButtonB(MicroBitEvent)
tsfarber 0:874bf65b167e 47 {
tsfarber 0:874bf65b167e 48 temp = thermometer.getTemperature();
tsfarber 0:874bf65b167e 49 F = (int)((temp*1.8)+32);
tsfarber 0:874bf65b167e 50 uBit.display.scroll(F);
tsfarber 0:874bf65b167e 51 uBit.sleep(200);
tsfarber 0:874bf65b167e 52 uBit.display.print('F');
tsfarber 0:874bf65b167e 53 uBit.sleep(1000);
tsfarber 0:874bf65b167e 54 uBit.display.clear();
tsfarber 0:874bf65b167e 55
tsfarber 0:874bf65b167e 56 }
tsfarber 0:874bf65b167e 57
tsfarber 0:874bf65b167e 58
tsfarber 0:874bf65b167e 59 int main()
tsfarber 0:874bf65b167e 60 {
tsfarber 0:874bf65b167e 61 uBit.init();
tsfarber 0:874bf65b167e 62 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
tsfarber 0:874bf65b167e 63 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
tsfarber 0:874bf65b167e 64
tsfarber 0:874bf65b167e 65 // We don't want to drop out of main!
tsfarber 0:874bf65b167e 66 while(1)
tsfarber 0:874bf65b167e 67 uBit.sleep(100);
tsfarber 0:874bf65b167e 68 }
tsfarber 0:874bf65b167e 69