And here the delayed code. In mycrocontroller world things are bot a dynamic as in a client (browser) or server (node.s) of the Web world. Dedicatedness trumps flexibility and dynamics. Things get setup and there they are... Due to the memory constraints, the 'spaghetti' is limited... ;-) I still though apply good practices (best ... is just an excuse and justification of not having stepped between the boxes...
SPI1.setup({ sck:B3, mosi:B5 });
var VPIN = A5;
// divider resistor 1 in ohms
var divr1 = 10000;
var divr2 = 1000;
var numReads = 5;
var g;
var updateVolts = function updateVolts() {
var aread = 0;
for (var i = 0; i < numReads; i++) {
aread += analogRead(VPIN);
}
aread /= numReads;
var pinvolts = 3.3 * aread;
var powervolts = (pinvolts / divr2) * (divr1 + divr2);
g.clear();
g.setContrast(0.45);
g.setFontVector(12);
g.drawString("ADC: " + aread,0,0);
g.drawString("Volts: "+ powervolts,0,16);
g.flip();
}
// backlight stays 2 more seconds on after releasing button
var backlight = B1;
var backlightTimeout;
pinMode(backlight,"output");
var backlightOn = function(e) {
if (backlightTimeout) { clearTimeout(backlightTimeout); }
backlight.set();
backlightTimeout = setTimeout(function(){
backlightTimeout = undefined;
if (digitalRead(BTN)) {
backlightOn();
} else {
backlight.reset();
}
},2000);
};
setWatch(backlightOn, BTN, { repeat: true, debouce:50 });
function onInit() {
g = require("PCD8544").connect(SPI1,B6,B7,B10, function() {
setInterval(updateVolts, 500);
});
}
A more object-oriented aproach could have bin teaken... with two inline defined singletons - voltmeter and backlight, and make some start (and stop) methods... but that's overshooting again... ;(
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
And here the delayed code. In mycrocontroller world things are bot a dynamic as in a client (browser) or server (node.s) of the Web world. Dedicatedness trumps flexibility and dynamics. Things get setup and there they are... Due to the memory constraints, the 'spaghetti' is limited... ;-) I still though apply good practices (best ... is just an excuse and justification of not having stepped between the boxes...
A more object-oriented aproach could have bin teaken... with two inline defined singletons - voltmeter and backlight, and make some start (and stop) methods... but that's overshooting again... ;(