Shop OBEX P1 Docs P2 Docs Learn Events
Can a Fade LED routine run nested while other LEDs flash? — Parallax Forums

Can a Fade LED routine run nested while other LEDs flash?

grafixusagrafixusa Posts: 2
edited 2013-05-18 12:33 in BASIC Stamp
O.k. I am VERY new at Basic. I purchased this to help me with an art project. I am building a scale airplane that my dad used to fly. I need to replicate strobes, beacons and constant on lights (with LEDs). After trying to do this with electronic circuitry for MONTHS!, I opted to buy a Basic Stamp Activity kit and have managed to almost complete the project- (not by my genius... but because of the flexibility of the Basic system). All of the coding below is basically from the manual, but I pulled the bottom code (fading an LED from another post). It works fine, however, as you experienced programmers can see... it fades IN SEQUENCE with the other processes in the code. In other words: LEDs 9, 12, 14, 25 flash while green 6 and red 3 are on constantly, AND THEN 11 fades in and then out... then the process repeats.
IS THERE A WAY TO HAVE THE FADES RUNNING CONTINUALLY while the other LEDs are flashing? Can the fade code be nested some how so that it runs in the background constantly? Thank you for your help!


'Test.bs2
'Turn two LEDs on and off. Alternate flashing twice 25/1000 of a second, pause
'for one second then repeat indefinitely.
'Hold a red LED on indefinitely.
'Hold a green LED on indefinitely.
'Fade a red LED up and down.


' Future needs: Add 3 UltraWHite LEDs that remain lit to simulate taxi lights.
' Try to determine if there is a way to fade a red LED continually up and down, while strobes
' 9, 12, 14, 25 contniually flash as well- instead of operating in sequence.?(This LED
' simulates a red beacon, one on the belly of fuselage and one on the top). F.Y.I. This red
' beacon, on an airplane indicates to the ground crew, that the engine(s) is/are running.)
' (Ultimately, I would want two red LEDs to fade in and out continually.)




' {$STAMP BS2}
' {$PBASIC 2.5}


DEBUG "Aircraft navigation, taxi and takeoff lights on."


DO
HIGH 6 'Right wing leading edge GREEN solid
HIGH 3 'Left wing leading edge RED solid


HIGH 14 'Strobe vertical stabalizer UltraWHite
PAUSE 25 '25/1000ths of a second
HIGH 8 'Strobe aircraft tailcone UWH
PAUSE 25
LOW 14
PAUSE 25
LOW 8
PAUSE 25
HIGH 14
PAUSE 25
HIGH 8
PAUSE 25
LOW 14
PAUSE 25
LOW 8
PAUSE 1000 'one second


'These two strobes alternate flashing in sink with 8 and 14 above.
HIGH 12 'Strobe aircraft wingtip RIGHT UWH
PAUSE 25
HIGH 9 'Strobe aircraft wingtip LEFT UWH
PAUSE 25
LOW 12
PAUSE 25
LOW 9
PAUSE 25
HIGH 12
PAUSE 25
HIGH 9
PAUSE 25
LOW 12
PAUSE 25
LOW 9
PAUSE 1000


'Variables


dty VAR Byte


' Syntax is:
' PWM PIN, duty(cycle), duration
'
' In this program "duty" controls the brightness range (0 to 255),
' duration controls the speed of the pulsing (1 to 20 works best-higher is slower).
' Connect the LED between pin 11 and ground (Vss)and use a current limit resistor.
'


'First, ramp the brightness up. The low value of 20 keeps LED from going
'totally dark. Experiment with STEP values in the 1-20 range.
'Note that ramp up and ramp down do not have to be the same.


FOR dty = 50 TO 255 STEP 1


PWM 11, dty, 5


NEXT


'Now ramp the brightness down. You can add a Step value here too.


FOR dty = 255 TO 50


PWM 11, dty, 5


NEXT
LOOP


' Is there any way to get PWM 11 to fade in and out continually instead of operating
' in sequence with all the other LEDs? Can you nest a set of programming commands?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-17 14:54
    It's hard to do. The general scheme is that you have a single loop with a time period that's the shortest event. You have a variable for each LED and increment each of them each time through the loop. They are your timekeeping counters. For each LED, you check its counter for the proper range of values, turn the corresponding LED on or off and reset the counter to zero when the cycle for that LED is done. For a single LED that's 1/2 second on and 1/2 second off, you might have something like this
    DO
       SELECT counter1
          CASE 0..19:
             LOW led1
          CASE 20..39:
             HIGH led1
       ENDSELECT
       IF counter1 = 39 THEN counter1 =  0 ELSE counter1 = counter1 + 1
       PAUSE 25   ' depending on how long the other statements take to do,  you might need to adjust this
    LOOP
    
    For other LEDs, you'd have a SELECT statement for each and an IF/THEN statement for each
  • grafixusagrafixusa Posts: 2
    edited 2013-05-17 17:01
    Ouch. That gave me a headache trying to figure out what you're trying to do!

    I couldn't get it to work in the code, even after adding:

    counter VAR Byte

    I went back to the book and it says that when you have a counter to add: counter VAR Byte, but it still didn't like counter1.

    The computer just kept sending back, "REALLY! ARE YOU SERIOUS?" (just kidding)
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-17 17:23
    You'll need "counter1 VAR BYTE" for that code fragment and you'll need to define led1 as well (probably with a PIN declaration). That was really an example of what can be done, not by any means a finished example.

    The whole idea is that the variable is the clock for the LED in basic time "ticks" and repeats in a fixed cycle. Certain things happen at certain times in that cycle. By doing it this way, you can have multiple LEDs, each running in its own cycle any which way. Some can be blinking. Some can be flashing faster than a blink. If the loop is fast enough, some can be dimming or brightening.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2013-05-18 12:33
    Another possibility would be an external circuit. A second BASIC Stamp? :smile:

    Or an analog circuit. Your p11 could be set high or low occasionally, and the fade in and fade out would be done by a capacitor that gradually charges or discharges. Here is a possible circuit. This circuit uses a bipolar transistor, and the bias set a current of around 5mA through the LED. When the input goes high, the current rises toward 15mA, and when the pin goes low it falls toward zero. Time constant around 6 seconds. It would be better done with an nMOSFET or an op-amp.

    slowled.png
    231 x 237 - 7K
Sign In or Register to comment.