SPI1.setup({ sck:B3, mosi:B5 });
var VPIN = A5;
// divider resistor 1 in ohms
var divr1 = 10000;
var divr2 = 1000;
function onInit() {
var g = require("PCD8544").connect(SPI1,B6,B7,B10, function() {
function updateVolts() {
var aread = 0;
var numReads = 5;
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();
}
updateVolts();
setInterval(updateVolts, 500);
});
// handle backlight
(function() {
var backlight = B1;
var backlightSleep = false;
setWatch(function(e) {
// clear any old timeouts so that if we have a misspress the screen will
// stay on during the subsequent presses
if (backlightSleep) {
clearTimeout(backlightSleep);
backlightSleep = false;
}
// if button down
if (e.state) {
// turn backlight on
digitalWrite(backlight, true);
} else {
// turn backlight back off after a few seconds
backlightSleep = setTimeout(function() {
digitalWrite(backlight, false);
backlightSleep = false;
}, 2000);
}
}, BTN, { repeat: true });
})();
}
//onInit();
save();
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.
Added your code... into context.