How to fade-in and fade-out an LED? #7462
Replies: 1 comment
-
Posted at 2018-01-13 by Chaapu I have got it to work(sort of), but I am not happy with it. It feels like there is/should be a better way to do it than below. While googling I found this jhonny-five example, which is quite nice. https://github.com/rwaldron/johnny-five/blob/master/docs/led-fade.md A LED class might make it easier for a beginner like me to start with Espruino. In all, loving Espruino so far. Need to learn Javascript to fully appreciate it though.
Posted at 2018-01-14 by DrAzzy I wouldn't be happy with that code either... TBH - what I'd do for something like this is have a single function getting called every 10 milliseconds with setinterval, set up like kind of like a state-machine, which determines if it needs to brighten, dim, or do nothing. Which feels slightly un-Espruino-ey... but I think it'll probably work better. I don't like a solution that's relying on two intervals staying in sync - that's not guaranteed. Posted at 2018-01-17 by Chaapu You are right, I was unnecessarily complicating things. Thanks a lot for your suggestion @drazzy This is what I have got now
Posted at 2023-01-20 by user152410 I ended up wanting to do something like this too. I actually ended flashing with a sin wave instead to simplify things and it gives it a nice glow IMO. You can change how long the flash cycle is by adjusting the constant to multiply getTime() by. I liked a 2 second cycle, hence the code below. To ensure it always starts by fading in you could use a new Date object when you start the fade instead.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2018-01-13 by Chaapu
Hello,
I am new to Espruino and Javascript and am struggling with the asynchronous nature of it. For my first shot at Espruino, I tried to fade in and out an LED. I have done this before in Arduino, but struggling here. What's the best way to do it?
Here is what I did, but I realize this is wrong as both the
fadeIn
andfadeOut
methods a running in parallel. Thanks in advance.Beta Was this translation helpful? Give feedback.
All reactions