View Full Version : Stamp2 and pwm control

05-26-2005, 06:57 PM

·I would like to know. What would be the best way using the stamp 2, to created a 35Hz square wave signal
and keeping that frequency as a constant, and vary the mock to space ratio.

·Example: Time = 35Hz fixed
············· Change duty cycle (using a pot) between 17.5% and 95%.

·Any help/schematics· would be much appreciated. Thankyou

Clock Loop
06-25-2005, 04:37 PM
How constant do you need it? If you don't need a REALLY stable 35hz signal, you can do it. To get ultimate stability, you would need an external clock (oscillator)

Heres some code that lets a 10k POT adjust the flash rate of a LED, but the code is really BAD..

HIGH 13 'turns pin 13 High when the ForNext is done.
FOR mycounterOff = timeOff1 TO 1 ' updates timing so when you change your POT, the change is immediate
HIGH 9 'charges capacitor
PAUSE 10 'lets the capacitor charge, adjust for your cap
RCTIME 9, 1, timeOff 'calculates the amount of time it takes your cap to discharge thru your 10k POT
LOW 13 ' makes sure pin 13 is low for length of time off.
timeOff1 = timeOff / 50 'makes the POT range not as wide.
IF timeOff1 = 0 THEN timeOff1 = 1 ' makes sure 0 isn't used as a time.

If you would like to adjust the on and off times, copy above code, rename variables, and pins for the on time POTl, and capacitor.

Post Edited (BPM) : 6/25/2005 5:55:45 PM GMT

Clock Loop
06-25-2005, 04:45 PM
you would need to tweak the code with PAUSE times to get your 35hz, you then can figure out what min and max values you will allow for the variable timeOff. (for your duty cycle of 17 to 95). You can measure frequency with a decent digital multimeter.

But when using pause times, obviously this uses your BS2 to waste processing time. Thats why external clock sources are better.
They don't require you to stop the BS2 using pause to make the proper 35hz signal. Once you have a stable reference input for the BS2 you can use math to make a 35hz do loop to one of your output pins.

BE AWARE THAT I MAY BE TALKING OUT OF MY REAR. Sometimes I think I know what im saying, but after trying it, 3 other problems pop up. :)

Philip Gamblin
06-26-2005, 04:19 PM
The BS2 PWM command won't do what you want. But I believe the PWMPAL will, http://www.parallax.com/detail.asp?product_id=28020. I would guess use RCTIME command to read the pot then based on the value rec'd dial in the desired duty cycle on the PWMPAL.