Using PWD to Fading on and off an led/motor
(note, you could technically use this same method to accelerate/decelerate
a DC motor)
In the same way you can use a transistor to drive a higher power device from
the BX, you can also use a transistor to fade a device (such as a light, or
a motor) on and off.
To do this, you should first
start by wiring your transistor and device to the bx as seen in the following
schematic.
Test your transistor as is. For example, if you have your BX wired to a transistor
that connects a 12Volt Light circuit, make sure that when you call putpin(wiretotransistor,
1) on the bx, your 12V light turns on!.
(If you are having trouble
with this step- go back through our transistor/relay lab.)
Up to now, when we use a transistor, basically the BX is sending the transistor
5Volts. These 5Volts close the transistor 'switch' and allow current to flow
through our higher power circuit. A transistor however, is more then just an
'on/off' switch. A transistor can be used in a way (similar to a water faucet),
to switch only partially on. We control how much the transistor is 'switching'
by how much voltage we send the transistor from our BX.
To regulate voltage flow
on the bx, we can use a command called pulseout.
Below, is an example that fades a 6 volt light on and off. I have connected
PIN 5 on my bx to the transistor.
In this code I have created a sub routine called fader.
fader
(pin, start, finish, speed)
Fader takes 4 arguments.
1) pin # that is
connected to your Transistor.
2) The start pulse for your device. A value of 1 is the minimum. 1, for
example, would give you the most dim point of your light.
3) The finish pulse for your light (i.e.. The brightest point.. you can
play around with this number- increase the number if your light does not get
bright enough)
4) Speed (is the delay between pulses.. which translates to how fast
the light will fade on... this MUST BE A DECIMAL number)
|
Public pulse as
integer
Sub Main()
call flashme(3)
'before running this
code I should check that my transistor works
'normally, but simply running a test such as :
'call putpin(pintotransistor, 1)
'if this works, and my transistor is switched (And my device..ie. lights/etc.)
'are on,then proceed.
DO
'fade
light on
call fader(5,
1, 1000, 0.001) '(pin#, startpulse, endpulse, delay)
delay(2.0)
'fade
light off- I just switch the values for startpulse and endpulse
call fader(5,
1000, 1, 0.001)
delay(2.0)
LOOP
end sub
sub fader(byVal
pin as byte, byVal start as integer, byVal finish as integer, byVal speed
as Single)
'if our start is less
then our finish, this is our loop to fade ON
if start < finish then
for
pulse = start to finish
Call
PulseOut(pin, pulse, 1)
delay(speed)
next
call
putpin(pin, 1) 'turn the pin on when done
end
if
'if our start is greater
then our finish, this is our loop to fade OFF
if start > finish then '
for
pulse = start to finish step -1
Call
PulseOut(pin, pulse, 1)
delay(speed)
next
call
putpin(pin, 0) 'make sure the pin is off when done
end
if
end sub
|
| |
|