View Full Version : Fade Using PWM command with the BASIC Stamp

10-30-2009, 11:23 PM
Hello. I am a new member trying to write a Program and breadboard the electronics for my Basic Stamp, and I need some help. Any suggestions would be greatly appreciated. This is what I need to do.

I need to control fading of analog voltages on 2 pins at the same time.

This is an example. I have been able to use the PWM command to put a voltage of 2 volts on Pin 12 and at the same time a voltage of 2.5 volts on pin 13. Then using the PWM command I can abruptly change the voltage to 2.5 volts on Pin 12 and 2.0 volts on Pin 13.

What I would like to do is have them fade simultaneouly from the first state to the second state over time period of about 1 second. As an example, fade simultaneously from (2V Pin 12, 2.5V Pin 13) to (2.5V Pin 12, 2.0V Pin 12) over a time of about 1 second.

Any suggestions on how to do that? Or is there a better way to do it other than the PWM command.

If I can figure out that specific example, then I can figure out the more general case.

Thanks ahead of time.


Mike Green
10-31-2009, 01:30 AM
The idea is that you divide the fading process into fixed intervals. The PWM statement has a value given that results in a particular voltage being output when the statement is executed. You have two values for each pin, the starting value and the ending value. You subtract the starting value from the ending value and divide by the number of intervals you want to use. You also divide the total time (1 second) by the number of intervals to get the step duration. You repeat this process for the other pin as well. You then have a loop with two PWM statements, one for each pin. The PWM value is the starting value plus the step size times the interval number and you provide a delay each time through the loop. Here's an example for one pin:

stepSize1 = (endValue1 - startValue1) / numberOfSteps
for i = 0 to numberOfSteps-1
pwm pinNumber1, startValue1 + (stepSize1 * i)

If you have two pins, you'd need "stepSize2 = (endValue2 - startValue2) / numberOfSteps"
and, in the loop, you'd need "pwm pinNumber2, startValue2 + (stepSize2 * i)"

10-31-2009, 03:35 AM

Thanks. That's very helpful. I'll try it over the weekend.