Hi! As you saw, your problem is really that you're not writing async code (plus digitalWrite only takes a digital value, not an analog one). Ideally you'd call analogWrite every so often to update the output
You probably want something like this:
var colFrom = [0,0,1]; // blue
var colTo = [1,0,0]; // red
var colFade = 0;
var colors = [
[0,1,0],
[1,1,0],
[0,1,1],
[0,0,0]
];
function onTimer() {
// fade a bit more...
colFade += 0.05;
// we've completed fading
if (colFade>=1) {
// try and fade to a new color
if (colors.length) {
colFrom = colTo;
colTo = colors.shift();
colFade = 0;
} else {
// no more colors - stop for now
colFade = 1;
}
}
// you may not need soft:true - it depends on your board and what pins you're using
analogWrite(LED1,colFrom[0]*(1-colFade) + colTo[0]*colFade, {soft:true});
analogWrite(LED2,colFrom[1]*(1-colFade) + colTo[1]*colFade, {soft:true});
analogWrite(LED3,colFrom[2]*(1-colFade) + colTo[2]*colFade, {soft:true});
}
function onButton() {
// add a new, random color
colors.push([Math.random(),Math.random(),Math.random()]);
}
setInterval(onTimer, 100);
setWatch(onButton, BTN, {repeat:true, edge:"rising", debounce:50});
This starts off fading through colors, but when you press a button it'll add a new color to the end of colors to fade to
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.
Hi! As you saw, your problem is really that you're not writing async code (plus
digitalWrite
only takes a digital value, not an analog one). Ideally you'd callanalogWrite
every so often to update the outputYou probably want something like this:
This starts off fading through colors, but when you press a button it'll add a new color to the end of colors to fade to