Shop OBEX P1 Docs P2 Docs Learn Events
Linear or logarithmic or exponential — Parallax Forums

Linear or logarithmic or exponential

metron9metron9 Posts: 1,100
edited 2005-09-21 18:15 in BASIC Stamp
Using PWM to fade an LED from off to full on duty cycle.
Led on for 99 counts off for 1 count at x frequency = full on
Led off for 99 counts on for 1 count at x frequency = full off

The question is in between, scaling up from on time of 1 off time 99 to on 99 off 1
What kind of math would I use to make the led fade evenly from off to on and on to off?

From on 50 off 60 is 10 steps but represents a 17% difference
from on 1 off 99 to on 10 off 89 represents a 1000% gain

I think I need to make a table of some sort to change evenly 99 levels of light intensity
but I may need more than a 100 count.

Anyone have a clue what I am talking about?

Comments

  • bobledouxbobledoux Posts: 187
    edited 2005-09-21 14:18
    I think you are trying to replicate the curve found in an "audio taper" potentiometer. This is found in the volume controls on radios. Turning the knob creates a proportional increase in volume. Try to Google that topic.
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-09-21 18:15
    To get a logarithm, there is the NCD function.

    log2x=NCD x - 1 ' log2x goes from 0 to 15 in powers of 2 as x goes from 1 to 65535

    I'm not sure that is the function you want. A square law might be more appropriate.

    Subjective luminosity of a source depends greatly on the background light level. It is one thing to view the LED in a dark room and another outdoors. For that reason, your setup might have an ambient light sensor.

    As one counter goes from full off at 0 to full on at 255, say, the PWM values could take on a value that changes by constant proportions, not linearly, something like this...

        rateFactor CON 64      ' larger value for larger step.  64 is x-> x^0.75 or x^1.25
        fadeFactor CON 256 - rateFactor
        growFactor CON 256 + rateFactor
        DO
            DO     ' increase level in steps
               PWM 0,x,100
               x = x */ growFactor
            LOOP UNTIL x>255
           DO       ' decrease level in steps
              PWM 0, x  ,100
              x = x */ fadeFactor
            LOOP UNTIL x=0
         LOOP
    
    



    The low end would only be perceptable in a dark room, though, hence necessity for ambient light compensation, or simply truncation at the low end.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.