pulsing LED – fireflies on an Espruino #196
Replies: 20 comments
-
Posted at 2014-01-23 by @gfwilliams Hi Kim, I'm afraid it isn't possible on the Espruino board... I'll update the documentation. For the latest revision I chose to use pins for the LEDs that were basically useless for anything else (so there was the maximum amount of IO). It means that there aren't any timers for those pins, so no PWM. The pins it's available on are detailed here: www.espruino.com/ReferenceESPRUINOBOARD I've just put an issue in for implementing PWM (analogWrite) in software though - as it would be nice to be able to do it on the LEDs (espruino/Espruino#193) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-23 by Kim Sounds like fun. Might try a JavaScript implementation of PWM tonight. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-23 by @gfwilliams Heh, it won't be that fast - but doing 50/50 using setInterval would probably work ok. There's some internal stuff for doing digitalPulse that would actually handle it really well. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-23 by Kim I have myself some pulsing LEDs ... but the journey there wasn't all that smooth though. First, the code:
I would be happy to write a tutorial for this code, but first I want to know if any improvements can be suggested. The problem with this code is that it halts all other things that are going on. Which isn't all that efficient. So any suggestions on how to improve this would be appreciated. I realise that this can be easily implemented on a lower level (and it should be implemented on a lower level), but the example is great in finding out how well this compact version of JavaScript works. And I have a number of suggestions:
quick update:
which fails with ERROR: Function or String not supplied!. another update:
But some messy stuff is going on with the pulse at the end ;). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-23 by @gfwilliams If you only want slow-ish PWM, you can do something like this:
Note that because digitalPulse uses a timer, you're only having to run JavaScript once per pulse, and not once. If you want to do that glowing effect then you could do something like this:
Which shouldn't halt Espruino while it's working. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-23 by Kim Your code needs comments :). I like the approach of prototyping. But there is also a bit of dirty playing here, like starting from Brings me to something else though, why not
The second piece of code is nice though. I'll try to elaborate on that version. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-23 by Kim Here is an extended version of your last code to glow an LED. There has been a bit of a comment explosion, it allows you to specify the duration and the Hz, and it has an improved glow by taking the power of the sine function, so that only the peak is very bright. I would like to go to
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-23 by Kim Just a bit of magic and you have a nice collection of fireflies on an Espruino:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by @gfwilliams :) Just so you know - intervals is just a variable. The idea is that if you call LED1.pwm more than once, it knows that it was already doing PWM on that pin and knows to get rid of the old setInterval...
Can you give an example of which I'm unsure if the speed/complexity would help much... I do already have a page on performance as Espruino has some particularly weird behaviour ;) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by Kim Thanks for clarifying the code. It was already late yesterday and details such as that tend to get lost in those cases. The code is definitely JavaScript, but the JS code you write on an Espruino definitely has a different flavour. No idea which value causes the pow error, but it occurred when writing Thanks for the performance page, first time I see it. Overall, it would be good to have this software PWM on a lower, more barebone level for performance reasons. This code is nice to get your feet wet, but it does show how the Espruino can struggle with relatively simple things due to the embedded nature. I would also still suggest to introduce a delay() function. Where a timeout allows you to asynchronously (I think?) execute a function, a delay would allow similar behaviour during the execution of a function. I think this will simplify a lot of the code. And now that I think of it: are there any pages on power management on the Espruino? I know that there were a lot of things you could do such as enter a lower power state or even turn off USB, but information on those things is hard to find. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by @gfwilliams The delay function is tricky - it'd be nice to add it but in a way it encourages coding in a style that works very badly with Espruino. As you'd see with your previous code, having loops that don't return will effectively stall the interpreter and will stop other things from being executed - it's one of the reasons why web browser JavaScript doesn't have anything similar (afaik) either. It also exposes the slow-ish speed of execution, for instance:
Won't keep the LED on for 1ms, it'll probably end up being maybe 0.1ms more (don't know - haven't tested it) because of the relatively slow execution speed. I can pretty much guarantee that as soon as I add the delay function I'll get complaints about exactly that kind of problem :) There aren't pages on power management at the moment, but I've put it on my list of things to add. In short, something like this:
or
Will instantly be very power efficient, dropping the device into a state where it draws around 100uA. However at the moment I haven't got |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by Kim Thanks for the quick response :). About the delay: yes and no. You can make it very clear in the function description that this is only a rough delay, and that it may or may not be accurate (as I recall, you already do that at some points). While it does block the execution, it could be used with e.g. a setInterval to call it every 2000 milliseconds. Having a dedicated delay function would then hopefully free up that time, eg On the power issue: I actually though you couldn't do that. It says Espruino will only enter Deep Sleep when there are no timers in the reference and I assumed that both are timers. Well, especially the one with setInterval(). It then worsens things further by saying that * The System Timer will also pause*. Ouch :). Good to know though that this already works. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by @gfwilliams Unfortunately doing stuff during the By when there are no timers it means PWM. I'll update the docs on that along with a Power consumption page. The issue is that when it enters deep sleep, all the clocks stop (except the real time clock) so anything that depends on that clock (SPI,I2C,timers,usart,etc) stop working. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by Kim No problem, I think this is just the curse of having a very high level language at your disposal. Thanks for the explanation on the timers. Do remember that a lot of people buying these Espruino's will be coming from the software side and have virtually no experience with electronics. For most of them a clock is a clock and they never consider that there might be other clocks at work as well. I definitely didn't think of it :). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by @gfwilliams This might be handy: http://www.espruino.com/Power+Consumption |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by Kim Excellent :) Thanks! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by Kim Sleep mode is available on the STM32 chip (very low power, but ALL DATA is lost from RAM). It is not currently used in Espruino (see Other sources of Power Draw below for why not). I assume you mean Stop mode is available ... ?
There are no current results for deep sleep. Adding a glow light with variable duration and brightness might be a very good idea, consumption-wise. Something like Math.pow(Math.sin( ..), 3) will reduce power consumption by about 60% while giving a nice glow. Brightness could push this down even further. Any ability for a reduce speed mode where the clock speed is lowered, e.g. like the micro Python board? It would definitely be interesting to see how low the board can go with all these things combined, e.g. have bluetooth on there where you can still communicate with the board, have a light flashing and have input (e.g. BTN1) working. I think this is also a fairly reasonable use-case. If you have the Espruino disconnected from USB you either are going to use it for the wireless capabilities, or to have it lay dormant and react to e.g. someone passing by to turn on the lights. The last use-case is pretty self-explaining, but the first use-case would definitely benefit from squeezing out as much of the savings as possible. Just my thought though :). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-24 by @gfwilliams Thanks, I've updated the docs. Maybe someone else can give more info on the Bluetooth module - I'm not too sure on low power but there may be something available via AT commands. As far as clocking down, Espruino can do it, but then the frequency for PWM (and also USART/SPI baud rates) will change. Some kind of compensation needs to be built in for that - otherwise it's very possible to do. The obvious change would be to clock everything down to a lower speed when entering Sleep mode. That would probably halve power consumption. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-02-03 by Edwin Yes, the HC-0x bluetooth modules support SNIFF to go into sleep mode. AT+SNIFF=Param1,Param2,Param3,Param4 Param1: maximum time I don't know what the best values are. There's also AT+ENSNIFF=Param and AT+EXSNIFF=Param (Param: Bluetooth address of device). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-02-03 by @gfwilliams Thanks for the info! There's some info on sending the AT commands at www.espruino.com/Bluetooth so it should be pretty easy to enable this... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-01-23 by Kim
In the reference on the function analogWrite an example is given where you use it on LED1, namely
analogWrite(LED1,0.5,{ freq : 10 });
. However, the code example on Pulse Width Modulation (PWM) specifies that PWM is only available on certain pins, and LED1 is not one of those according to the reference.Is it possible to have a pulsing light or not?
Beta Was this translation helpful? Give feedback.
All reactions