9 years, 11 months ago.

Help with staggering off time LEDs with pwm on KL46Z

Hello,

I'm not too good when it comes to programming. What I'm trying to use the 2 on-board LEDs to turn off completely at staggered times. Right now both LEDS come on fade out at the same time and completely turn off at the same time. No matter what I try I cannot get them to stagger, I've tried separate for loops, embedded for loops and delays. Either they never turn off or the just blink for a lack of a better word.

Here is the code that I have now that works:

#include "mbed.h"
#define ON 0
#define OFF 1
DigitalOut redled(LED_RED);
DigitalOut greenled(LED_GREEN);
DigitalIn sw3(PTC12);
//DigitalIn sw1(PTC3);
PwmOut r(LED_RED);
PwmOut g(LED_GREEN); 
int main()
{
    
r = 1;
g = 1;
    
   greenled = OFF; //green and red led are off
    redled = OFF;
  
  
   while(1) {


  //   if (sw3 == 1) {
    //        greenled = OFF; //green led is off
      //      redled = OFF; //  red led is on
        //}  
    
     
       if (sw3== 0) {

   r.period(0.001);
    g.period(0.001);
            greenled = ON;
            redled = ON;
               for (float i = 0.0; i < 3.0 ; i += 0.001) { //i is duty cycle changes length of fade out
            float p = 3 * i;
            
          //r = 1.0 - ((p < 1.0) ? 1.0 - p : (p > 2.0) ? p - 2.0 : 0.0);
           r = 1.0 - ((p < 1.0) ? p : (p > 2.0) ? 0.0 : 2.0 - p); //this dims led off after button press but not completely off.
  wait (0.0025); 
           g = 1.0 - ((p < 1.0) ? p : (p > 2.0) ? 0.0 : 2.0 - p);
           
          wait (0.0025); 
  
  
    
          
            }//end of for loop 
                }


              else{ r = 1; g = 1;}
                                              
            
        }
 
     
}

I appreciate any help or clues to help me along.

Thanks,

Donato

can you edit your question and use "<<code>>" and "<</code>>" for code snippets..

posted by Martin Kojtal 07 May 2014

I think I have it right

posted by Donato Evangelista 07 May 2014

1 Answer

9 years, 11 months ago.

Simple Donato,

To my limited knowledge, the K64F LEDs are not pwm_able, as they are not connected to a PWM pin. On-off mode only

...kevin

K64 is not KL46 ;). I don't know about the K64, but on the KL46 they should be pwm-able iirc.

posted by Erik - 07 May 2014

Erik I do have the KL46Z. I have the on board LEDs dimming with pwm, I just cant figure out how to stagger them to turn completely at different times.

posted by Donato Evangelista 09 May 2014