Dimming Neon Transformer using PWM

Hi everyone,

I’m here asking for a tiny fix that has to do with PWM and Json Packages. Basically I’m controlling a neon lamp transformer to change the tube’s brightness. My code runs almost perfectly and it would be perfect if I were able to do a soft dim. So far the dimmer works but it is too fast to perceive the steps it takes to reach the specified brightness. Its a very simple code, I don’t understand why my Imp’s stepping is imperceptible or how to dial in the speed for my FOR loop

Here’s my device code:

neonWhite <- hardware.pin1;
neonWhite.configure(PWM_OUT, 0.0167, 0.0);

neonBlue <- hardware.pin2;
neonBlue.configure(PWM_OUT, 0.0167, 0.0);

// Record the state and delta of the LED’s cycle
neonState <- 0.6;
neonChange <- 0.006;
newState <- 0.0;

function dim1(white) {
newState = neonState - (white.tofloat() * 0.006);
server.log("White lamp state is " + white + “%”);

function dim2(blue) {
newState = neonState - ((blue.tofloat() * 0.006));
server.log("Blue lamp state is " + blue + “%”);

function action() {
for(local x = neonState; x >= newState; x -= neonChange){

 for(local y = neonState; y <= newState; y +=neonChange){


agent.on(“white”, dim1);
agent.on(“blue”, dim2);

Any advice will be greatly appreciated

You just want to do this slower; the simplest way (if you’re trying to do this in, say, a second or less) is just to add an imp.sleep(x) where x is the time in seconds you want to wait for.

Once that works, then you can make it a bit more squirrel-y by doing this with imp.wakeup, ie you don’t do a loop with delays, your function just does one step and then schedules when it’s going to be called again.

eg, action might become:

`function action() {
  if (currentState >= newState) {
    currentState -= neonChange;
    imp.wakeup(0.05, action); // call us back in 50ms for the next step