Created
February 10, 2017 00:58
-
-
Save cdinu/05084bee7c9a3a908ca25b093edc1431 to your computer and use it in GitHub Desktop.
Measure and display temperature with a BBC Micobit computer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The number of leds represent how many | |
// degrees Celsius are above 15C | |
// run and compile at https://pxt.microbit.org | |
const getLedArray = (temp: number) => { | |
const n = Math.max(0, temp - 15); | |
return[ | |
Math.min(5, n), | |
Math.max(0, Math.min(5, n - 5)), | |
Math.max(0, Math.min(5, n - 10)), | |
Math.max(0, Math.min(5, n - 15)), | |
]; | |
}; | |
const drawArray = (vals: Array < number >) => { | |
vals.map((value: number, y: number) => { | |
for (let x = 0; x < value; x++) { | |
led.plot(x, y); | |
} | |
}); | |
} | |
let canDisplay = true; | |
basic.forever(() => { | |
if (canDisplay) { | |
basic.clearScreen() | |
drawArray(getLedArray(input.temperature())); | |
basic.pause(1000); | |
led.toggle(4, 4); | |
} | |
basic.pause(1000); | |
}) | |
input.onButtonPressed(Button.A, () => { | |
canDisplay = false; | |
basic.showString(`${ input.temperature() } C`); | |
canDisplay = true; | |
}) | |
bluetooth.startTemperatureService(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment