Shop OBEX P1 Docs P2 Docs Learn Events
pbasic code for 3 led and 2 pushbutton — Parallax Forums

pbasic code for 3 led and 2 pushbutton

stevennstevenn Posts: 3
edited 2013-02-23 21:24 in BASIC Stamp
i try to program 3 led and 2 pushbutton program so red led is power blink all time 2 push-button when press 1 button green led is flash when press 2 button yellow led is flash but it don't work here is my code
DO
HIGH 15
PAUSE 500
LOW 15
PAUSE 500
IF ( IN 3 = 1 ) THEN
HIGH 14
PAUSE 50
ELSEIF ( IN4 = 1 ) THEN
HIGH 13
PAUSE 50
ELSE
PAUSE 50
ENDIF
LOW 14
LOW 13
PAUSE 50
LOOP
Can someone check what wrong with the code

Comments

  • bsnutbsnut Posts: 521
    edited 2013-02-22 18:42
    You problem is part code and the controller. The Basic Stamp can only do one thing at a time. This means you will need to place the code for the buttons inside the flashing code.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-22 20:58
    Typically in a problem like this, you have a single loop that repeats roughly at the blink rate using one PAUSE at the beginning of the loop and another identical PAUSE at the end of the loop. In between the two PAUSEs, you check for each button and turn on the corresponding LED if the button is down. After the 2nd PAUSE, you turn off all the LEDs even if they weren't on.
  • stevennstevenn Posts: 3
    edited 2013-02-23 11:51
    so what code i need to change and where
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-23 11:59
    I'm not going to write the program for you. It sure sounds like an exercise for a class. There should be enough of a suggestion in Post #3 for you to write your program. Perhaps you should start with a program that just blinks the red LED as I described, then add the other LEDs and pushbuttons later. Is there something specific you don't understand in Post #3?
  • stevennstevenn Posts: 3
    edited 2013-02-23 15:20
    i dont really get what you want to mean can you be more specific in post 3
    i can make red led blink the whole time but whe i add 2 push-button it don't work anymore
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-23 21:24
    What you have for your blink cycle is something like this:

    DO ---- stuff#1 ---- PAUSE ---- stuff#2 ---- PAUSE LOOP

    stuff#1 sets things up for the first PAUSE and stuff#2 sets things up for the second pause, then the whole thing repeats. To blink an LED, stuff#1 does whatever is needed to turn the LED on and stuff#2 does whatever is needed to turn the LED off. If you want to blink another LED, you add statements to stuff#1 that turns the 2nd LED on and to stuff#2 that turns the 2nd LED off and so on. If you want to turn the 2nd LED on only when a pushbutton is pressed, you test for that with an IF/THEN statement and either turn the LED on or not. It doesn't hurt to always turn the 2nd LED off in stuff#2 even if it's already off.

    If you have questions about how to add the pushbutton testing, I suggest you review the sections in "What's a Microcontroller?" (find here) that deal with pushbuttons and LEDs.
Sign In or Register to comment.