The issue with the code not displaying is that you need a newline before and after each section of code that you paste in...
I think the main issue you're hitting here is that when you send code to Espruino from the right-hand pane, it's executed immediately. That means if you say:
var foo = myFunction();
Then foo will forever be set to the result of myFunction() - it won't be executed each time you start. Espruino has always worked like this - it's nothing new. When you type dump(), Espruino tries to reconstruct the code that you wrote based on its current state (by adding setWatch, setInterval, digitalWrite, etc) - and sometimes it doesn't get it exactly correct.
It's most noticeable for setInterval/setWatch which return integers - so Espruino has no way of knowing that the number 2 assigned to sp came from setInterval, and not just you writing 1+1.
To get around this, it's best to put code that you want to start into a function called onInit(), which will automatically get called when Espruino starts up:
var list = [
{"b":0,"g":0,"r":0,"s":0},
{"b":0,"g":0,"r":1,"s":45},
{"b":0,"g":1,"r":0,"s":90},
{"b":0,"g":1,"r":1,"s":135},
{"b":1,"g":0,"r":0,"s":180},
{"b":1,"g":0,"r":1,"s":120},
{"b":1,"g":1,"r":0,"s":90},
{"b":1,"g":1,"r":1,"s":60}
];
var MIN_SERVO = 0.63;
var MAX_SERVO = 2.34;
var ONE_DEG = (MAX_SERVO - MIN_SERVO) / 180;
var ZERO_DEG = MIN_SERVO;
function posServo(deg) {
return E.clip(MIN_SERVO + (ONE_DEG * deg), MIN_SERVO, MAX_SERVO);
}
function stopTimer() {
if (timer) {
clearInterval(timer);
timer = undefined;
}
}
function nextLights() {
digitalWrite(LED1, list[step].r);
digitalWrite(LED2, list[step].g);
digitalWrite(LED3, list[step].b);
servoPos = posServo(list[step].s);
step = (step +1) % list.length;
}
var step, servoPos, timer, sp;
function onInit() {
// just in case other intervals were already running when we typed 'save()'
clearInterval();clearWatch();
// now init
step = 0;
servoPos = posServo(0);
timer = setInterval(nextLights, 500);
setWatch(function() {
stopTimer();
nextLights();
}, BTN1, {"repeat":true,"edge":"rising","debounce":10});
sp = setInterval("digitalPulse(C5,1,servoPos)", 20);
}
// Explicitly call onInit, so it starts working (this time) even before you save:
onInit();
Having said that, there seem to be other issues:
There's a bug in Espruino where it doesn't seem to like dumping a value if it is undefined. Hence why you get var timervar sp = 2; instead of what you should see, which is var timer = undefined; var sp = 2;
Given timer is undefined, it looks like the button has already been pressed (and so stopTimer() was called) by the time you save() - which would explain why nothing happened in your case. I just tried your exact code here and it works fine.
The missing debounce is a bug too - i'll try and get that fixed for 1v68.
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.
The issue with the code not displaying is that you need a newline before and after each section of code that you paste in...
I think the main issue you're hitting here is that when you send code to Espruino from the right-hand pane, it's executed immediately. That means if you say:
Then
foo
will forever be set to the result ofmyFunction()
- it won't be executed each time you start. Espruino has always worked like this - it's nothing new. When you typedump()
, Espruino tries to reconstruct the code that you wrote based on its current state (by addingsetWatch
,setInterval
,digitalWrite
, etc) - and sometimes it doesn't get it exactly correct.It's most noticeable for
setInterval/setWatch
which return integers - so Espruino has no way of knowing that the number2
assigned tosp
came fromsetInterval
, and not just you writing1+1
.To get around this, it's best to put code that you want to start into a function called
onInit()
, which will automatically get called when Espruino starts up:Having said that, there seem to be other issues:
var timervar sp = 2;
instead of what you should see, which isvar timer = undefined; var sp = 2;
timer
is undefined, it looks like the button has already been pressed (and sostopTimer()
was called) by the time yousave()
- which would explain why nothing happened in your case. I just tried your exact code here and it works fine.debounce
is a bug too - i'll try and get that fixed for 1v68.