Simple PWM an LED

I am beating my head against why I can’t simply implement a fade with an LED. I had tried suggestions on the forum, and nothing. The values ramp up, freeze/stop at random intervals. code below:

`
class ledBrightness extends InputPort
{
name = "LED Brightness"
type = “number”

function set(value) {
    
    for(local value = 0 ; value < 256; value ++) {
    
    server.log(value);
    hardware.pin9.write(value/256.0);
    imp.sleep(0.04);

// }
//for(local value = 1.0 ; value > 0; value --) {
//hardware.pin9.write(value/256.0);
//imp.sleep(0.02);

}
}
}

hardware.pin9.configure(PWM_OUT_STEPS, 1.0/500, 0.0, 37);
imp.configure(“April Brightness Controller”, [ledBrightness], []);`

ive also attached the “tick tock” node to my planner/object.

Try changing the following line:
hardware.pin9.configure(PWM_OUT_STEPS, 1.0/500, 0.0, 37);
to
hardware.pin9.configure(PWM_OUT_STEPS, 1.0/500, 1.0, 256);

eesh, the same issues. it fades the led (sometimes) and mostly just hangs on random values in the 0255 range.shown in the log monitor.

revised code:

`class ledBrightness extends InputPort
{
name = "LED Brightness"
type = “number”

function set(value) {
    
    for(local value = 0 ; value < 256; value ++) {
    
    server.log(value);
    hardware.pin9.write(value/256.0);
    imp.sleep(0.04);
    
}
for(local value = 256.0 ; value > 0; value --) { 
  hardware.pin9.write(value/256.0);
  imp.sleep(0.04);

}
}
}

hardware.pin9.configure(PWM_OUT_STEPS, 1.0/500, 1.0, 256);
imp.configure(“April Brightness Controller”, [ledBrightness], []);`

If you’re using 256 steps, then the write() value for pin 9 should be a number 0-255, not 0-1.

are you talking about the “Value” variable? I am under the impression to even do the most basic of PWM with this service, you need to first connect a tick tock node, then actually create a class to listen in on it? am I way off to just get a simple LED to fade in and out? I don’t see how this can be so difficult.

is their a worked example someone can lead me to? everything on the wiki, though claiming to be a PWM sketch just blinks an LED on and off.

its also rather confusing since the documentation illustrates sending low (0) and HIGH (1.0) are the integers, but suggestions point to using an int of a val 256.

well I followed this criteria mentioned, and it does fade (as it did with previous methods), but i once again still have the issue of the monitor “hanging” on random values within for loop. code below:

`// Control a LED intensity throught PWM Control

// Show program name in logs window
server.log(“Start PWM LED Intensity”);

// Set pin to PWM, with a 20 ms period and duty cycle to 1 (default)
hardware.pin7.configure(PWM_OUT, 0.02, 1.0);

// Create inputPort and correspondent function light the LED
class LEDintensity extends InputPort
{
// Set name and type of input port
name = “InputAnalog”;
type = “number”;

// Create function to light the LED
function set(nvalue)
{
   
   // local new_val = scale(value, 0, 1.0, 0, 256.0);
   for(local nvalue=0.0; nvalue<=1.0; nvalue+=0.02){
    // Write PWM value accordingly to the input value. Divide by 65535 to get a value between 0 and 1
    hardware.pin7.write(nvalue/1.0);
    server.log(format("%.2f", nvalue));
   imp.sleep(0.04);

}
for(local nvalue=1.0; nvalue>=0.0; nvalue-=0.02){
    // Write PWM value accordingly to the input value. Divide by 65535 to get a value between 0 and 1
    hardware.pin7.write(nvalue/1.0);
    server.log(format("%.2f", nvalue));
  imp.sleep(0.04);

}

}
}

// Configure imp in the server
imp.configure(“PWM LED Intensity”, [LEDintensity()], []);`

So generally, the reason you’re having problems here is that you have 4 second delays (2*(1/0.02)*0.04) in there - this is blocking in the foreground - and you’re logging to the server.

Bear in mind that a server.log is not like a printf - this is an encrypted TCP transaction. It’s not very low overhead.

If you remove the server.logs then I suspect it’ll look fine.

The PWM value needs to be 0.0-1.0 for PWM_OUT (the system will work out how many steps are needed to represent that value), but for PWM_OUT_STEPS you specify the number of steps you want at configure, and when you write a new value you have to use a value between 0 and the number of steps used at configure.

No, it’s the same for both PWM_OUT and for PWM_OUT_STEPS: you specify a value 0.0-1.0.

Peter

I stand corrected, and a bit confused. Never used the steps option myself :slight_smile: