PWM an LED

I am following the example exactly as such mentioned here:
http://devwiki.electricimp.com/doku.php?id=example:pwm

I am not having any luck with my code PWM an LED. code below:

Code wasn’t below… LED the right way round?

whoops. sorry, and yes.

// April with pushbutton switch
// define our callback function. Note that we’re doing this before we register it with hardware.pin7.configure

server.log(“Hardware Configured”);

class ledBrightness extends InputPort
{
name = "LED Brightness"
type = “number”
// brightVal;

function set(brightVal) {
    for(brightVal = 0; brightVal <=1.0; brightVal+=5)
    {
    // write a floating point number between 0.0 and 1.0, where 1.0 = 100% duty cycle

hardware.pin9.write(brightVal);

}
for(brightVal = 1.0; brightVal >=0; brightVal-=5)
{
// write a floating point number between 0.0 and 1.0, where 1.0 = 100% duty cycle

hardware.pin9.write(brightVal);
}
}
}

hardware.pin9.configure(PWM_OUT, 1.0/500.0, 1.0);

// imp.configure registers us with the Imp Service
// The first parameter Sets the text shown on the top line of the node in the planner - i.e., what this node is
// The second parameter is a list of input ports in square brackets (we have none here)
// The third parameter is a list of output ports in square brackets (we have none here)
imp.configure(“button and brightness controller”, [ledBrightness], []);

A couple of notes:

  • you’re throwing away the passed in parameter brightVal with your loop, as you set it to zero on the first line of the set() function

  • your loops on brightval don’t make sense; you’re going from 0-1.0 in steps of…5. ie, it’ll never write anything but the first value.

Just doing this:

`class ledBrightness extends InputPort
{
name=“LED brightness”;
type=“number”;

function set(brightVal) {
server.log("setting "+brightVal);
hardware.pin9.write(brightVal);
}
}`

…to test that the value is arriving, and simply setting it, should allow you to check the LED is working. After that you can think about ramping brightness, which it appears is what you’re trying to do.

I had tested this, and it works fine with the PWM tutorial after I added the tick tock node, but am I atleast on the right path to get a ramp up ? I am a bit confused with the syntax.

works fine, i.e. makes LED high and low.

I am at a loss now. This is my current code:

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

function set(value) {
    
    for( value = 0 ; value <= 255; value +=1) {
    // write a floating point number between 0.0 and 1.0, where 1.0 = 100% duty cycle
    server.log(value);
    hardware.pin9.write(value);
    ;
}
for( value = 255 ; value >= 0; value -=1) { 
  hardware.pin9.write(value);
  
  
}
}

}
hardware.pin9.configure(PWM_OUT, 1.0/500.0, 1.0);
imp.configure(“April Brightness Controller”, [ledBrightness], []);

`

for( value = 0 ; value <= 255; value +=1) { // write a floating point number between 0.0 and 1.0, where 1.0 = 100% duty cycle server.log(value); hardware.pin9.write(value); }

Firstly, that code doesn’t write values between 0.0 and 1.0 – it writes values 0,1,2,…,255. Secondly, there’s no sleep or delay in the loop, so it will execute in a tiny fraction of a second – too fast to see the ramp. I think you mean something like this:

for (local value=0; value<256; value++) { hardware.pin9.write(value/256.0); imp.sleep(0.04); }
This will ramp from 0.0 to 1.0 over the course of about a second.

Peter

i’ve tried to implement your suggestion, but the variable ramps up, shows in the monitor, but the LED stays High.

`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], []);

`

it seems like it goes through the for loop, fades down, then the value hangs in the monitor. Is their an issue with Imps server?

No issue with the server, no.

I guess you’ve solved this from your multiple other posts (can you keep stuff in one thread if it’s related to one set of questions?).

You keep randomly switching from using PWM_OUT to PWM_OUT_STEPS, which both require very different values to be written. Peter’s example would work fine with the code you listed just before his post, but then when you implemented his suggestion in your last post, you switched PWM method, stopping it working again…