Skip to content

Instantly share code, notes, and snippets.

@cdinu
Created February 10, 2017 00:58
Show Gist options
  • Save cdinu/05084bee7c9a3a908ca25b093edc1431 to your computer and use it in GitHub Desktop.
Save cdinu/05084bee7c9a3a908ca25b093edc1431 to your computer and use it in GitHub Desktop.
Measure and display temperature with a BBC Micobit computer
// 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