Shop OBEX P1 Docs P2 Docs Learn Events
Using a potentiometer to control LED blink rate — Parallax Forums

Using a potentiometer to control LED blink rate

wirecutterwirecutter Posts: 22
edited 2011-02-23 15:21 in Propeller 1
Hello,
I need help with Using a potentiometer to control LED blink rate.
I am using RCTime from the OBEX to get a RCValue variable. here is the code I am using to blink the LED. I have the RCValue set at 300 this is the target when the pot is at center. I am using a 10K pot with a .1uf cap, and on P5.

The question is how do I convert the RCValue given in RCTime (in RCTime foreground demo, that states at ''R = 10K you get 4_000", ''and at R = 4.7K you get 1_800") to work in the "wait" method?
_clkmode = xtal1 + pll2x
_xinfreq = 5_000_000

pin = 27
RCValue = 300
high = 1
low = 0

Pub Go

dira[pin]~~
repeat
turnOn_LED
wait
turnOff_LED
wait

Pri turnOn_LED
outa[pin] := high

Pri turnOff_LED
outa[pin] := low

Pri wait
waitCnt((CLKfreq/1000)*RCValue +cnt)

thanks

RCTIME_foreground_DEMO - Archive [Date 2011.02.11 Time 13.02].zip

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2011-02-11 11:54
    Hi wirecutter,

    from a quick look at what you have coded the principle should work.

    But you should use a RC-time backround-code.
    The RC-time foreground needs some time to execute the RCTIME-command itself.
    I have not done a calculation of the charge and discharge-times but this might be the reason why it does not work.
    If measuring the rc-time runs in its own cog you can use the code that you have posted above.

    My sugestion is: do a little bit more "homework" to learn about how rctime works and make a first try on your own.

    If you just want to blink an LED and do nothing else a propeller-chip is really oversised. Except you are just tinkering around to get more familiar with the propeller and his possabilities.
    For just blinking an LED take an NE555-chip and some external components.

    best regards

    Stefan
  • wirecutterwirecutter Posts: 22
    edited 2011-02-11 12:15
    Stefan,
    This is a sample code that I am using to learn about the waitCnt((CLKfreq/1000)*RCValue +cnt) method.
    This operation is part of a on going learning and project.

    when I run the code and the primary value of RCValue is 300, and when the RCTime is run to update RCValue, the LED just stays on, I think its because it was running to fast.

    But I will need to figure out the math. The only part of the code that was attached in the previous post that will be used in the project is RCTime.spin to get the RCValue variable and will be run in a new cog.

    thanks
  • mparkmpark Posts: 1,305
    edited 2011-02-11 14:29
    Are you saying that the code you posted works (LED blinks on for 300ms, off for 300ms, repeatedly) but if you replace RCValue with RCTime.RCTime, the program no longer works (the LED just says on)?

    Perhaps the call to RCTime.RCTime is not returning because your RC circuit is wired incorrectly or you are calling RCTime with the wrong state parameter. Perhaps try printing out the values that RCTime is yielding.
  • wirecutterwirecutter Posts: 22
    edited 2011-02-11 16:33
    I am so confused, I changed something. now nothing works. can someone rework the RCTime to a basic one, that will output to the terminal with the RCValue. I think with the tv out stuff is what is messing me up. attached is the code for my project if that helps. I am trying to control the LED sequence rate externally with a 10K pot with a .1uf cap on P5, and using a new cog.

    Cylon project - Archive [Date 2011.02.11 Time 19.26].zip
  • JasonDorieJasonDorie Posts: 1,930
    edited 2011-02-11 16:39
    If you're using waitcnt( 300 + cnt ) in Spin, it'll "lock up" until the clock wraps around. Executing the waitcnt operation and the add takes a number of cycles, so you have to offset your wait value by that amount or you'll end up waiting for a clock value that's already passed by the time you get there. I don't remember what the exact value is, but I think it's about 400 cycles.

    So, try this: waitcnt( 400 + RCValue + cnt )

    ...and you always put cnt last, so it's the last thing that gets added in, and therefore is as "current" as possible.

    Jason
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-02-11 17:00
    StefanL38 wrote: »
    But you should use a RC-time backround-code.
    Like the the RCTime_forever method here: http://obex.parallax.com/objects/507/
  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2011-02-11 23:15
    I was just messin with RC time this week, the tutorial on page 122 of the Propeller Education Kit Labs-Fundamentals v1.1.pdf is great. Much simpler than some of the current objects available.

    Good Luck
  • wirecutterwirecutter Posts: 22
    edited 2011-02-12 10:00
    Hello,

    It looks like I got the "waitCnt((CLKfreq/1000)*RCValue +cnt)" working in my project code. Now I just need to incorporate the RCTime_forever method into it to update the RCValue.

    I have been studying RCTIME3 and am I correct that P0 is the pin to connect the pot and called "apin" in the DAT section?

    Cylon project_wirecutter - Archive.zip
  • wirecutterwirecutter Posts: 22
    edited 2011-02-23 10:40
    Hello,

    I have the project working, I think, but I am have timing issues using RCTime3. I am watching the values in PST and found the rctime value is over double what I need and is causing the project to go nuts. could someone tell me where I need to look to correct this?
    I need rctime to equal 1800 when the pot is set at 5K, currently I see its over 3800.

    thanks,

    Cylon project.zip
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-02-23 11:20
    so what about simply dividind the value by 2 or subtracting 2000 to get a value of 1800 out of a vlaue of 3800?
    best regards

    Stefan
  • JasonDorieJasonDorie Posts: 1,930
    edited 2011-02-23 11:44
    RCTime is just measuring how long it takes a capacitor to discharge to a certain point (look at http://www.tpub.com/neets/book2/3d.htm).

    If you want to reduce the value, you can do it a couple ways:

    1) In hardware, use a smaller capacitor. It'll hold less charge and won't take as long to discharge.

    2) In software, reduce the length of time you charge the cap for. If you don't fully charge it, it'll take less time to discharge.

    3) In software, just reduce the number using division. 3800 / 2 = 1900, which is close to what you want, and simple. If you want -exact-, you need to use fixed or floating point math. Fixed point math using 8 bits of precision would look like this:

    Result := (rcTimeValue * 121) / 256 ' 121 = 256 / 2.1111
  • wirecutterwirecutter Posts: 22
    edited 2011-02-23 15:21
    Hello,

    Thanks, JasonDorie , the division worked after I found where to put it in the project.

    A big thank you to everyone for the help.
    wirecutter
Sign In or Register to comment.