Shop OBEX P1 Docs P2 Docs Learn Events
PWM with constant Period — Parallax Forums

PWM with constant Period

danny270danny270 Posts: 13
edited 2008-08-26 23:03 in General Discussion
Need help to repeat PWM with constant period for 2nd LED and 3rd LED. Currently, APP007 from programming the SX Microcontroller by G. Daubach changes brightness from dark to bright on one LED. I need to duplicate for a 2nd and 3rd LED of different colors. After fade out of 1st LED, I would like the 2nd LED to fade in, then fade out, then the 3rd LED does the same, then the Loop to repeat cyle for the 3 LED's. Thank you.

Comments

  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-08-25 20:48
    I've attached an SX/B framework that will handle three outputs (and can be expanded if you like); it uses a slight variation of Guenther's PWM code. All you have to do is fill in the section at Main to ramp you LEDs the way you want.

    [noparse][[/noparse]Edit] Added controls for the LEDs

    Post Edited (JonnyMac) : 8/26/2008 11:06:20 PM GMT
  • danny270danny270 Posts: 13
    edited 2008-08-25 21:11
    JonnyMac, thanks for the speedy reply. Can't wait to go home and try this tonite.
  • danny270danny270 Posts: 13
    edited 2008-08-26 16:59
    JonnyMac,
    Can't make the PWMLEDS to work. When I hit Run, both LED1 and LED2 lit up and stay lit. Wont fade out/dim. I am using only two LEDs for now. Also, for your comment "fill in the section Main o ramp LED's the way you want it", please guide me how to do it. Just starting with the SX so I still have a lot to chew on. Understood your PM message. Wont happen again.
    Thanks.
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-08-26 23:03
    There is a tricky detail with SX/B: You can't use an array element (like level1, level2, or level3) in a FOR-NEXT loop. The work-around is easy: add a simple variable to your program (I called it brightness) and use it to control your loop -- I've had this running on my PDB all day:

    Main:
      FOR brightness = 0 TO 255
        level1 = brightness
        DELAY_MS 8
      NEXT
      FOR brightness = 255 TO 0 STEP -1
        level1 = brightness
        DELAY_MS 8
      NEXT
    
      FOR brightness = 0 TO 255
        level2 = brightness
        DELAY_MS 8
      NEXT
      FOR brightness = 255 TO 0 STEP -1
        level2 = brightness
        DELAY_MS 8
      NEXT
    
      FOR brightness = 0 TO 255
        level3 = brightness
        DELAY_MS 8
      NEXT
      FOR brightness = 255 TO 0 STEP -1
        level3 = brightness
        DELAY_MS 8
      NEXT
    
      GOTO Main
    


    I've updated the listing in my original post -- download it and give it a try.
Sign In or Register to comment.