Shop OBEX P1 Docs P2 Docs Learn Events
Smart Pin RGB LED Demo — Parallax Forums

Smart Pin RGB LED Demo

JonnyMacJonnyMac Posts: 8,924
edited 2020-03-15 18:59 in Propeller 2
More simple, yet useful fun with smart pins -- a no-cog RGB LED controller. This is an alpha version; I'm sure more features will be added later. It works with common-cathode or common-anode LEDs, and you get to set the PWM frequency.

Setup code:
pub start(r, g, b, drive, freq) | x

'' Start RGB LED driver
'' -- r, g, and b are output pins
'' -- drive is the output drive mode
''    * 0 = common anode
''    * 1 = common cathode
'' -- freq is pwm frequency

  longmove(@rpin, @r, 3)                                        ' copy pins
  x := 255 << 16                                                ' set pwm period to 255 units
  x |= 1 #> ((clkfreq / 255) / freq) <# $FFFF                   ' set timing of 1 unit
  drive := (drive) ? 0 : OP_INV                                 ' setup CA inversion if needed
  pinstart(rpin, SP_PWM | drive, x, 0)                          ' red = true mode pwm, off
  pinstart(gpin, SP_PWM | drive, x, 0)                          ' green = true mode pwm, off
  pinstart(bpin, SP_PWM | drive, x, 0)                          ' blue = true mode pwm, off
Example use:
      repeat level from 0 to 255                                ' ramp up
        rgb1.set_red(level, rgb1.USE_GAMMA)                     ' linear brightness
        waitus(1_000_000 >> 8)                                  ' ramp in 1 second
Sign In or Register to comment.