Shop OBEX P1 Docs P2 Docs Learn Events
Sample code needed for LED strobe pattern — Parallax Forums

Sample code needed for LED strobe pattern

BallNmIKEBallNmIKE Posts: 7
edited 2010-01-23 17:17 in Learn with BlocklyProp
I would like some help in designing some code for control of 4 LED's Red Green Blue and White. I would like multiple functions with the push of a button. and flash/strobe speed control with a pot. I would like to choose a single color for strobe/w speed control. A few different defined patterns. Like chase, alternate, fade.·All with only 1 button and 1 pot.·Thanks!!·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-21 00:32
    Like any complex project like this, you divide it up into pieces, get each piece to work by itself, then integrate the various pieces and add functionality a step at a time. In your case you need:

    1) 4 LEDs separately controlled (each by one I/O pin). Since the LEDs are of different types, they will need different value resistors for the same level of intensity. How would you figure that part out? Hint: You need to know the voltage drop (forward voltage) of each type of LED and you need to use Ohm's Law.

    2) You need a pushbutton switch on another I/O pin and a pullup or pulldown resistor. There are examples of both #1 and #2 in the "What's a Microcontroller?" tutorial and the "BASIC Stamp Syntax and Reference Manual", both downloadable from Parallax and included as part of the help files with the Stamp Editor.

    3) You need a pot and code to read it. There are examples in the documentation on the RCTIME statement

    4) Next steps are to try to light the LEDs in whatever patterns you want. The details depend on the patterns and how you've hooked everything up. You should end up with subroutines for each pattern that have speed (time delay) information in a variable so the pot can be used for a speed control.

    5) Last step is to figure out how you want the push button to behave for selecting the pattern to show. There are many ways to do this. You propose something.
  • BallNmIKEBallNmIKE Posts: 7
    edited 2010-01-21 00:50
    Yeah I remember reading that in the book, I can get the sub routines down, but how do I control all of it?
  • FranklinFranklin Posts: 4,747
    edited 2010-01-21 02:12
    Get the subroutines down and show us what you have. we will be glad to offer suggestions (as Mike has) to help you along.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • BallNmIKEBallNmIKE Posts: 7
    edited 2010-01-21 03:00
    How would I do this in the code? read pushbutton. if pushbutton = 1 goto police, if pushbutton = 2 goto strobe, if pushbutton = 3 goto chase, if .... with each push of the pushbutton I want the value to increase 1 so that it will go to the next pattern in the sequence.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-21 05:32
    The operative statement is "get each piece to work by itself". Rather than post long bits of code for our "approval", try the pieces on a real piece of hardware, one at a time.
  • FranklinFranklin Posts: 4,747
    edited 2010-01-23 04:15
    if pushbutton = 1 then
        pushbutton = pushbutton + 1
        goto police
    

    This is not real code but the idea is valid.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-01-23 17:17
    ·Play with this until you understand how it work
    (By The Way)····· (·BTW )·the two Example can be found in the Basic Stamp Editor in the BS2 folder
    You might also read ( What Is A Micro Controller ·)<<<<< ( Click Here )
    If I where you I would read and do all of the Example in this book before I would start this project· here is why
    First
    It will help you understand how to write basic code
    Second
    It will help you Understand how to hook different thing to the Basic Stamp Pins

    These are the most Inportant thing to underdtand in how to use a Basic Stamp

    I hope this helps

    ' BUTTON.BS2
     
    Ths can be found at on[url=http://www.parallax.com/Portals/0/Downloads/docs/books/edu/Wamv2_2.pdf] [/url]( [url= http://www.parallax.com/Portals/0/Downloads/docs/books/edu/Wamv2_2.pdf]PAGE 76 [/url])<<<<<(click here )
     
                              ._____220 ohm Resister >>>>>>>>> PIN 0
                              !
    Vdd >>>>>  ^^.^^ Switch ^^.^^ >>>> 10k ohm Resister >>>>>> Vss
    
    ' Connect an active-low circuit to pin P0 of the BS2. When you press the
    ' button, the DEBUG screen will display an asterisk (*). The program, as
    ' shown below, will print an asterisk at the first button press, then
    ' delay approximately one second (200 x 5 ms PAUSE) before auto-repeating
    ' at a rate of approximately 100 ms (5 x 20 ms).  Feel free to modify the
    ' program to see the effects of your changes on the way BUTTON responds.
     
     
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
     
    Btn             PIN     0
    btnWrk          VAR     Byte
     
     
    
    Main:
     
     PAUSE 5
      BUTTON Btn, 0, 200, 20, btnWrk, 0, No_Press
      DEBUG "*" No_Press:
      GOTO Main
     
    
    
      ' Try changing the Delay value (255) in BUTTON to see the effect of
      ' its modes: 0 = no delay; 1-254 = varying delays before auto-repeat;
      ' 255 = no auto-repeat (only one action per button press)
      '
      ' The BUTTON instruction will cause the program to branch to
      ' No_Press unless P0 = 0
       
    

    ' HIGH_LOW.BS2
     
    This can be found on ( [url= http://www.parallax.com/Portals/0/Downloads/docs/books/edu/Wamv2_2.pdf]PAGE 48[/url] )<<<<<<<<<(click here)
     
    [color=#1f1a17][color=#1f1a17][color=#1f1a17]PIN 1 >>>>  [color=#131516]470 ohm Resister [/color] >>>>>>  LED Light  >>>>>>>>>>>  Vss [/color][/color][/color]  
    
    
    ' This simple program sets I/O pin 1 high for 1/2 second and low for
    ' 1/2 second in an endless loop.  Connect an LED to P1 for a simple
    ' blinker.
    ' {$STAMP BS2}
    
    Main:
      HIGH 1
      PAUSE 500
      LOW 1
      PAUSE 500
      GOTO Main
      END  
    

    Try this
    

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
     
    Btn             PIN     0
    LED_Red         PIN     1
     
     
    btnWrk          VAR     Byte
      
    
    Main:
     
    PAUSE 5
    
      BUTTON Btn, 0, 200, 20, btnWrk, 0, No_Press
      DEBUG "Switch Pushed "
     
    HIGH LED_Red 
    PAUSE 500
    LOW LED_Red
    PAUSE 500
     
    No_Press:
      GOTO Main 
     
        
    



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 1/23/2010 6:36:47 PM GMT
Sign In or Register to comment.