Skip to content

Instantly share code, notes, and snippets.

@coderbyheart
Created August 29, 2020 10:31
Show Gist options
  • Save coderbyheart/b878301fcec53695a655a99f14196247 to your computer and use it in GitHub Desktop.
Save coderbyheart/b878301fcec53695a655a99f14196247 to your computer and use it in GitHub Desktop.
espruino temperature controlled fan on Thingy:52
const threshold = 26.5;
const interval = 10;
let currentTemp;
let isOff;
function setLEDS(l1,l2,l3) {
digitalWrite(LED1, l1);
digitalWrite(LED2, l2);
digitalWrite(LED3, l3);
}
function setIsOff(b) {
if (b && isOff === undefined || b && !isOff) {
console.log("Turning off.");
// Thingy.beep(500, 100);
} else if (!b && isOff === undefined || !b && isOff) {
console.log("Turning on.");
/*
Thingy.beep(3000, 100);
setTimeout("Thingy.beep(3000, 100)", 200);
*/
}
isOff = b;
digitalWrite(D2, isOff);
}
function alert() {
console.log("Alert: " + currentTemp);
setIsOff(false);
setLEDS(true,false,false);
}
function ok() {
console.log("OK: " + currentTemp);
setIsOff(true);
setLEDS(false,true,false);
}
function warn() {
setIsOff(false);
console.log("Warning: " + currentTemp);
setLEDS(true,true,false);
}
function onHumidity(res) {
console.log(res.temperature);
currentTemp = res.temperature;
if (currentTemp > threshold) {
alert();
} else if (currentTemp > threshold * 0.9) {
warn();
} else {
ok();
}
}
function detect() {
Thingy.getHumidity(onHumidity);
}
setInterval(detect, 1000 * interval);
detect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment