... It uses a hardware timer to produce accurate pulses, and returns immediately (before the pulse has finished). Use digitalPulse(A0,1,0) to wait until a previous pulse has finished.
So it's doing the pulses, but it's queueing them up to happen at pretty much the same time, in the background. If you did:
for (int i=0; i<6; i++) {
digitalWrite(segs[i], 1); // light up the first segment in segs[]
digitalPulse(D30, 0, 100); // pulse unused pin for 100ms, this is just a simple delay
digitalPulse(D30, 0, 0); <--- wait for the pulse to finish
}
It'd likely do what you're expecting, but it's far from ideal. What you were attempting with the timeout would have been best, but there were just somes issue with your code:
for (var i = 0; i<6; i++) {
setTimeout('digitalWrite(segs[i], 0)', 100);
}
for (var x = 5; x>=0; x--) {
setTimeout('digitalWrite(segs[x], 1)', 100);
}
It's queuing all 12 timeouts up to happen at the same time - 100ms from the time the code runs
When the timeout runs, it executes the string 'digitalWrite(segs[i], 0)' - but 'i' is just set to 6, because the FOR loop has already run.
Instead something like this should work fine:
for (var i = 0; i<6; i++) {
setTimeout('digitalWrite(segs['+i+'], 0)', i*100);
}
for (var x = 5; x>=0; x--) {
setTimeout('digitalWrite(segs['+x+'], 1)', 1200 - x*100);
}
Normally I wouldn't recommend setTimeout with a string (I'd usually pass in a function) but here it works quite well.
Another slightly more efficient solution would be to just have a single function that runs over and over:
var i=0;
var myInterval = setInterval(function() {
if (i<6) digitalWrite(segs[i], 0);
else digitalWrite(segs[11-i], 1);
i++;
if (i>=12) i=0; // or you could clearInterval(myInterval); to stop it
}, 100);
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.
Ahh - pretty sure you're hitting:
http://www.espruino.com/Reference#l__global_digitalPulse
So it's doing the pulses, but it's queueing them up to happen at pretty much the same time, in the background. If you did:
It'd likely do what you're expecting, but it's far from ideal. What you were attempting with the timeout would have been best, but there were just somes issue with your code:
'digitalWrite(segs[i], 0)'
- but 'i' is just set to 6, because the FOR loop has already run.Instead something like this should work fine:
Normally I wouldn't recommend
setTimeout
with a string (I'd usually pass in a function) but here it works quite well.Another slightly more efficient solution would be to just have a single function that runs over and over: