I'm very confused by the JS in Espruino for many reasons. I want to start off addressing this simple concern that I believe will also be useful to many others.
I want to address pins whose names are stored in variables to make the code considerably cleaner.
Consider this:
led1 = function() {
LED1.write(1);
LED2.write(0);
LED3.write(0);
setTimeout(led2, 500);
}
led2 = function() {
LED1.write(0);
LED2.write(1);
LED3.write(0);
setTimeout(led3, 500);
}
led3 = function() {
LED1.write(0);
LED2.write(0);
LED3.write(1);
setTimeout(led1, 500);
}
setTimeout(led1, 500);
That's pretty amateur. It would be some much cleaner/DRYer to do:
clearInterval();
led = function (n) {return (this)['LED'+n];}
setInterval(function() {
ledCount = 3;
for (i=1;i<=ledCount;i++){
l=led(i);
if(l.read()){
n = Math.wrap(i, ledCount) + 1;
}
led(i).write(0);
}
led(n).write(1);
}, 500);
It would be splendid if that would work in the same way this works:
ono = function (n) {return (this)['ONO'+n];}
ONO1 = 'a'
ONO2 = 'b'
ONO3 = 'c'
ono(2)
But it doesn't. It seems to require a limited switch/case.
clearInterval();
led = function(n) {
switch (n){
case 1:
return LED1;
case 2:
return LED2;
case 3:
return LED3;
}
};
setInterval(function() {
ledCount = 3;
for (i=1;i<=ledCount;i++){
l=led(i);
if(l.read()){
n = Math.wrap(i, ledCount) + 1;
}
led(i).write(0);
}
led(n).write(1);
}, 500);
So, what and where are these LED variables an how can I address them programmatically?
I'm very confused by the JS in Espruino for many reasons. I want to start off addressing this simple concern that I believe will also be useful to many others.
I want to address pins whose names are stored in variables to make the code considerably cleaner.
Consider this:
That's pretty amateur. It would be some much cleaner/DRYer to do:
It would be splendid if that would work in the same way this works:
But it doesn't. It seems to require a limited switch/case.
So, what and where are these LED variables an how can I address them programmatically?