Shop OBEX P1 Docs P2 Docs Learn Events
Creating a Program with BASIC Stamp (2.5.3) and LEDs — Parallax Forums

Creating a Program with BASIC Stamp (2.5.3) and LEDs

jellybean1865jellybean1865 Posts: 7
edited 2012-10-10 22:46 in BASIC Stamp
I have to design a program that will turn on an LED with a push button and cause it to blink 10 times at a rate of 1/s, with the LED on for 0.5 s and off for 0.5 s during each cycle. If the button is pressed again before the LED blinks 10 times, the LED should automatically turn itself off. I am supposed to use IF THEN statements. I am not sure how to get the LED to blink ten times after it is pushed, or how to make it stop if it is pushed again before the ten blinks have happened. Any help is appreciated!!

My code so far is: (and I know this isn't right)

' What's a Microcontroller - PushbuttonControlblink10.bs2
' Check pushbutton state 10 times per minute, blink LED 10 times when button pushed, and stop blinking if pushed again before 10 blinks


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


DO


DEBUG ? IN3


IF (IN3 = 1)THEN
HIGH 14
PAUSE 50
LOW 14
PAUSE 50


ELSE
PAUSE 100


ENDIF


LOOP

Comments

  • Clive WakehamClive Wakeham Posts: 152
    edited 2012-10-08 03:34
    You would need to have a variable set up to count how many times the LED blinks. And it would need to determine at the tenth cycle to stop, and if it is before the tenth cycle it needs to check to see if the button has been pressed, and if it has then it aborts.
  • softconsoftcon Posts: 217
    edited 2012-10-08 05:58
    As mentioned by Clive, you will need a loop counter to determine how many loops have been done.
    I'm sure this was covered in your class.
    The other thing is, the pause command is milliseconds, so you'd need pause 500 to get half a second delay, not 50 as you have.
    And, you must remember that anything you have inside the loop will execute everytime through the loop, so if you have some sort of setup that needs done, or conditions that must be met before the instructions in the loop take over, then those things should be done outside the loop, otherwise you'll get incorrect results.
    hth.
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-10-08 06:16
    You will need to check the state of the button within the same loop that flashes the LED so that you can exit the loop (stop flashing) as soon as the button is pushed. Also, you might want to look at using the TOGGLE command instead of HIGH and LOW to flash the LED.
  • jellybean1865jellybean1865 Posts: 7
    edited 2012-10-08 10:37
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    counter VAR Byte


    FOR counter = 1 TO 10


    DEBUG ? counter


    HIGH 14
    PAUSE 500
    LOW 14
    PAUSE 500


    NEXT


    DEBUG "All done!"


    END


    ok got this part. still not sure how to get it to stop if button is pressed before 10 blinks. thanks for all the help! :)
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-10-08 12:06
    You will need to check the state of the button within the same loop that flashes the LED so that you can exit the loop (stop flashing) as soon as the button is pushed.

    Hint: You only need three more lines of code and you'll be done. Read about IF...THEN in the Basic Stamp Editor help files.
  • jellybean1865jellybean1865 Posts: 7
    edited 2012-10-08 17:28
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    counter VAR Byte

    IF (IN3 = 1) THEN
    {IF (IN3 = 0) THEN
    FOR counter = 1 TO 10


    DEBUG ? counter

    HIGH 14
    PAUSE 500
    LOW 14
    PAUSE 500

    ELSE (IN3 = 1) THEN
    LOW 14
    PAUSE 500
    }

    ELSE (IN3 = 0)
    PAUSE 100

    ENDIF

    LOOP

    DEBUG "All done!"

    END


    still not sure but i tweaked some
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-10-08 18:16
    Like RDL2004 said, read up on IF....THEN statements. you can shorten this up using IF,,,,, THEN.
  • vaclav_salvaclav_sal Posts: 451
    edited 2012-10-08 19:04
    I would suggest you draw yourself a flow chart of your program depicting graphically what you are trying to accomplish. Any format, free hand, pseudo code etc.

    What you are doing now results in sloppy code which probably does not even compile and have no chance to do what you want to do.

    And even the simplest code should include some comments.
    For example if you added " .5s LED on" it would be obvious from start that PAUSE 50 was wrong.

    And my favorite "complaint" - if the variable has only two values possible - 0 and 1 - your code does not have to check for both - in pseudo code:

    if variable = 1 then
    do something...
    (esle) do something else... variable is zero
    end

    A final hint - to monitor the pushbutton you need to replace PAUSE 500 with code reading the button state.
  • jellybean1865jellybean1865 Posts: 7
    edited 2012-10-08 19:07
    I have read up on IF THEN statements. And understanding them is not my problem. It's implementing them in this specific program. If I use an IF...THEN statement here, what am I saying specifically? I am assuming that at the very beginning, for the program to even begin, I will use IF (IN3 = 1) THEN, but once this ten blink has begun, how do I tell the program that now repressing the button and getting IF (IN3 = 1) THEN means something different than it did before? And do I need two IF...THEN statements (one and another inside of that one) or do I need only one?
  • jellybean1865jellybean1865 Posts: 7
    edited 2012-10-08 19:09
    ok let me try to do these
  • softconsoftcon Posts: 217
    edited 2012-10-08 19:39
    You could try using polling to solve the second problem. Simply check to see if the button is pressed, and if so, leave the light on and you're done.
  • jellybean1865jellybean1865 Posts: 7
    edited 2012-10-08 19:41
    polling? I don't think I've heard of that before. How would I use that?
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-10-09 06:45
    Syntax: IF Condition THEN Address

    Condition = Button is pressed
    Address = END

    Put this in your program from post #5. Put it inside the blinking loop at the end. This will work but it has a bit of a problem - response time.
    The program cannot check the button while it is doing the PAUSE commands, so the button must be down while the program runs the IF...THEN command.
    In other words, if the button is pressed quickly it may not register, to be sure the program ends it may have to be held down for a full second. You could put two of these commands in, one after each PAUSE and reduce this to 1/2 second, but there are better ways to fix this problem.

    Hint: Replace the long PAUSE commands with loops using much shorter PAUSE commands while checking the state of the button.
  • vaclav_salvaclav_sal Posts: 451
    edited 2012-10-09 09:11
    Here is a sample , stolen from another thread, of "flow chart".
    Once you have that it is MUCH easier to write code.


    1. Read byte 4. - yours would be - input button.
    2. Add the new 8-bit value to it.
    3. If the sum is less than the original value of byte 4, go to step 4; otherwise, quit.
    4. Add one to byte 3. If the sum is zero, go to step 5; otherwise, quit.
    5. Add one to byte 2. If the sum is zero, go to step 6; otherwise, quit.


    PS Is this a school project??
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-10-09 10:29
    I'm pretty sure this is a school assignment. It comes from the "What's a Microcontroller" book.

    Actually, you don't have to go through all that just to read a pin. You can just use IN to read the status of any of the 16 i/o pins directly. They are set to be inputs by default on start up. So if IN3 = 1 it means the button is pressed (the exercises in that section of the book have the pin wired to Vdd through a 220 ohm resistor with a 10k pulldown to ground). IF IN3 = 1 THEN END will cause the program to stop. As I stated above, the problem is that the program is spending most of its time PAUSEd.
  • vaclav_salvaclav_sal Posts: 451
    edited 2012-10-10 15:39
    RDL2004 wrote: »
    I'm pretty sure this is a school assignment. It comes from the "What's a Microcontroller" book.

    Actually, you don't have to go through all that just to read a pin. You can just use IN to read the status of any of the 16 i/o pins directly. They are set to be inputs by default on start up. So if IN3 = 1 it means the button is pressed (the exercises in that section of the book have the pin wired to Vdd through a 220 ohm resistor with a 10k pulldown to ground). IF IN3 = 1 THEN END will cause the program to stop. As I stated above, the problem is that the program is spending most of its time PAUSEd.

    Well, looks like we have managed to chase the OP off.

    My point exactly, if (!) this is a school assignment, than (!) I would like to hear from the teacher.

    I like to know if this is current state of the art teaching method - write incomplete "to do " statement, read "What is a microprocessor?" and than start coding. And when in trouble - post on internet.

    The OP had no clue how to count events, read input and implement if... than, comments etc.
    Reminds me of “How about Bob?” - baby steps, baby steps.
  • jellybean1865jellybean1865 Posts: 7
    edited 2012-10-10 15:53
    Since I was having problems with the flow of the program, I created a pseudocode version. I'm currently converting that into coding. I'll be posting that within the next few days. Thanks for all the help :)
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-10-10 16:37
    I looks like this exercise is based on topics covered in chapter 2 and 3 of the book. I think the teacher has taken ideas from some of the examples and added additional requirements, so that the answer can't be simply copied out of the book. Counters, For...Next, If...Then, and Loops have just been discussed and the primary focus is on blinking LEDs and reading button inputs. It's still pretty early in the game and these are probably new concepts for most students. I know I didn't instantly "get it".
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-10-10 22:46
    vaclav_sal wrote: »
    “How about Bob?” - baby steps, baby steps.

    Correction: "What About Bob", One of my favorites " I'm Sailing " !!!!!!
Sign In or Register to comment.