Skip to content

PWM: Almost done

February 17, 2011

Alright, so here’s the link for the PWM program: Just plug a standard Light Sensor into Port 2 and you’re good to go.

Unfortunately this program is very large…. I hope to find a way to shrink it down. If you have any ideas on how to do this (‘for’ loops maybe?), please say so in the comments. Till then, Enjoy!

Advertisements

From → Uncategorized

10 Comments
  1. Hi stryker. I’ve made a more compact version of your PWM code
    I used a while loop
    For now, you can’t change the brightness in the program, but I’m working on it

    #define PWMPERCENT 50
    #define PWMFREQUENCY 10

    #define PWMONTIME PWMPERCENT/PWMFEQUENCY
    #define PWMOFFTIME 100-PWMONTIME
    task main()
    {
    while(1)
    {
    SetSensorType(S1,IN_TYPE_LIGHT_ACTIVE);
    Wait(PWMONTIME);
    SetSensorType(S1,IN_TYPE_LIGHT_INACTIVE);
    Wait(PWMOFFTIME);

    }
    }

    I don’t have a light sensor, so I couldn’t test it but i thinkit will work.

    -Tim
    ps. visit my new blog

    • Coolio. I’m trying to come up with a similar version that changes the brightness based on an accelerometer’s “tilt” value, but I’ve been busy with other stuff…. Thanks for the tip!

      – Stryker

      P.S. Nice blog!

  2. New version (can change brightness in program)
    Use the pwm fuction like this
    pwm(, ,, );

    #define PWMFREQUENCY 10
    #define PORT S2
    void pwm(int time, byte startVal, byte endVal, byte frequency);
    task main()
    {
    pwm(100,0,100,10);

    }
    void pwm(int time, byte startVal, byte endVal, byte frequency)
    {
    byte increment = (startVal - endVal)/time;
    byte onTime = startval/frequency;
    byte offTime = onTime-frequenct;
    for(int i = 0; i < time; i++ )
    {
    SetSensorType(S1,IN_TYPE_LIGHT_ACTIVE);
    Wait(onTime);
    SetSensorType(S1,IN_TYPE_LIGHT_INACTIVE);
    Wait(offTime);
    onTime += increment;
    offTime = frequency/onTime;
    }
    }

    -Tim

  3. sorry it should be “Use the pwm fuction like this
    pwm(time, startVal, endVal, frequency);”

  4. byte onTime = startval/frequency; should be
    byte onTime = startVal/frequency;
    otherwise it won’t compile

  5. and
    byte offTime = onTime-frequenct;
    should be
    byte offTime = onTime-frequency;

  6. I found a typo in the first version as well, but it’s no biggie (I usually have >3 typos per program lol)

    As far as your second program, I’m really not good with code, so here’s a stupid question: what does it mean? You don’t have any comments, and my helpfiles don’t work, so I can only guess as to what it really means. Would you mind helping me out a bit? Thanks!

    • ok here it goes

      #define PWMFREQUENCY 10
      #define PORT S2

      These are defines. They are processed before the code is compiled
      the compiler will eg. see if there are any mentions of “PORT”
      in the program and repalce them with S2
      This just makes it easier to change the port in the program, becausen in a bigger program you would have to replace
      every instance of S2 with S3 of whatever
      void pwm(int time, byte startVal, byte endVal, byte frequency);

      task main()
      {
      pwm(100,0,100,10);

      }

      This is the main task It call the pwm() function once
      TO BE CONTINUED

  7. This is the pwm function It fades the light sensor brightness
    from startVal(what you want the brightness to start at)
    to endVal (what you want it to finish at)
    these can be values form 0 – 100

    void pwm(int time, byte startVal, byte endVal, byte frequency)
    /*
    it has 4 arguments
    time: This is how long the brightness should change (big number = slow
    short number = fast) This is not in seconds or anything
    startVal(what you want the brightness to start at)
    endVal (what you want it to finish at)
    frequency: the PWM frquency (how fast the sensor should turn on and off)
    (smaller is faster)
    */
    {
    byte increment = (startVal + endVal)/time; /* how much more (or less) time the LED in the sensor is on for
    (the difference between startval and endval divided by the number of loops the function will do (time))
    */
    byte onTime = startVal/frequency; /* Calculates how long the light has to be on for each time */
    byte offTime = onTime-frequency; /* the time the led is on for - the total time for each loop */
    for(int i = 0; i < time; i++ ) //repeats for the value of time
    {
    SetSensorType(S1,IN_TYPE_LIGHT_ACTIVE);
    Wait(onTime);
    SetSensorType(S1,IN_TYPE_LIGHT_INACTIVE);
    Wait(offTime);
    onTime += increment; // makes the on time longer by increment
    offTime = frequency/onTime; // recalculates the off time based on that
    }
    }

    Hope this explains it better

  8. if you want to chat about it join #mindboards on freenode IRC
    just google freenode irc
    when your on it type /join #mindboards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: