Shop OBEX P1 Docs P2 Docs Learn Events
Program for BS2 — Parallax Forums

Program for BS2

KivistoKivisto Posts: 17
edited 2008-11-17 19:58 in BASIC Stamp
I am working in a class with a BS2 (revision F), but my lack of knowledge of the Basic language is giving me fits. I am trying to put together a program to do the following:

1)count the number of high pulses on pin 0
2)each time a pulse is counted, decrement a number stored in a variable by 1
3)when the variable reaches zero, send pin 1 HIGH

i.e. say 500 is stored into variable X. every time a high pulse is detected on pin 0, X is decremented to 499. Another high pulse, 498, and so on. When X reaches 0, pin 1 goes HIGH.

it seems like a relatively simple program, but some example code would be really beneficial in understanding Basic and getting this accomplished.

Thanks for any help!

Comments

  • SRLMSRLM Posts: 5,045
    edited 2008-11-15 18:03
    
    X VAR Word
    X = 500
    
    
    DO
      IF(IN0 = 1) THEN
        X = X - 1
        IF(X = 0) THEN
          OUT1 = 1
        ENDIF
        DO UNTIL IN0 = 0
        LOOP
      ENDIF
    LOOP
    
    
    



    That should work. Note that you have to test in the code to make sure that the pin goes low. That's what the nested loop is for. I haven't test this on a BS2, so you may want to...

    Post Edited (SRLM) : 11/15/2008 6:18:33 PM GMT
  • KivistoKivisto Posts: 17
    edited 2008-11-15 18:12
    Thank you very much! That's exactly what I needed!

    Also, is there anyway to use the DEBUG function in there? So when I'm running the application, I can see everytime X is decremented via the debugger on my Parallax software?

    Thanks again!
  • SRLMSRLM Posts: 5,045
    edited 2008-11-15 18:17
    You can add the DEBUG anywhere you want. Probably the best place would be right after the decrement.
  • KivistoKivisto Posts: 17
    edited 2008-11-17 00:50
    Okay, I can get this to compile but for some reason the logic still isn't correct when running:

    DIRA = %100000000000000         
     
    X VAR WORD                              
    X = 500
      
    Main:                                       
    IF (IN0 = 1) THEN START 
                      
    Start:
    X = X - 1
    DEBUG X, CR
    IF (X=0) THEN COMPLETE
    
    Complete:
    OUT1 =1
    DEBUG "COMPLETE", x, CR
    GOTO MAIN
    
    



    I noticed you said I needed to make it a nested loop. Not sure if that's being done. Also, the output on the DEBUG is in weird characters, looks like ASCII.
    Suggestions would be greatly, greatly appreciated.
  • FranklinFranklin Posts: 4,747
    edited 2008-11-17 01:47
    Take a look at your code above. you have if statements that, if they are true go to the next line where the label is and if they are false they 'fall through' to the next line that is the same place so you can either rewrite them to do what you really want or take them out. Take a look at the help file for the proper use of IF..THEN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • KivistoKivisto Posts: 17
    edited 2008-11-17 02:18
    I'm sorry, my knowledge of the language just isn't strong enough to understand what needs to go where. That's why I'm here asking for help in the first place. I'm not understanding how to make these IF loops complete, as well as where to go about putting the DO loop that SRLM originally placed in his code. Thanks for your help...
  • SRLMSRLM Posts: 5,045
    edited 2008-11-17 02:29
    Okay, first, notice that in your main you will always proceed immediately to the Start method. As franklin said, this is because it is falling through. You can solve this with a GOTO Main: command right after the if statement. This is effectively the same as a loop structure.

    Next, after the line "IF (X=0) THEN COMPLETE" you'll want to add some things. Like in the case we looked at before, this one falls through too, regardless of the state of X. So, you'll need a return to main (if X > 0 GOTO main, effectively ). But wait! we still need to test to make sure that the pin is low again, otherwise the code will rush through 500 times on a single long, high pulse (because it never considers a pulse to have two components: a high and a low.) So you need to wait for it to go low before calling the GOTO Main.

    As a note, whenever I debug variables, I like to use the ? operator to tell me the variable name.

    DEBUG ? X, CR

    will print

    X = 43
  • KivistoKivisto Posts: 17
    edited 2008-11-17 03:01
    Okay, I tried to add on some stuff based on your suggestions to the best of my knowledge.

    DIRA = %100000000000000         
     
    X VAR WORD                              
    X = 500
      
    Main:                                       
    IF (IN0 = 1) THEN START 
                      
    Start:
    X = X - 1
    DEBUG ? X, CR
    IF (X>0) THEN GOTO MAIN
    IF (X=0) THEN COMPLETE
    
    Complete:
    OUT1 =1
    DEBUG "COMPLETE", CR
    
    



    I'm still not grasping if I have these IF statements correct, or where exactly to wait for pin 0 to go low. You originally suggested

    DO UNTIL IN0 = 0
    LOOP

    I'm not completely sure where to throw this in. I apologize for being so oblivious.
  • SRLMSRLM Posts: 5,045
    edited 2008-11-17 05:14
    Often, if I'm having problems with some code I'll leave it alone for a while and go somewhere (like class...) and bring a notepad with me. I keep it open, and write out some psuedo code to do something else in the project. Often, the solution will appear as if by magic. It just takes some relaxed thought and a careful step through of your algorithm.

    Anyway, the loop that you don't know what to do with? That waits until the pin goes low. Like I mentioned earlier, this is important so that you are measuring pulses, not durations. Ask yourself: where do I want to make sure the pulse is low? After you've made sure it's high, of course. You'll want to add it before the line "IF (X=0)..." if you want to go to COMPLETE after the last pulse goes to ground. Or you can add it after that line if you want to go to COMPLETE the moment that you sense the last pulse. Your choice.

    You still haven't added a GOTO in the right spot. Notice that in your main function, it still always goes to the Start method. Read a couple of times the sections in the PBASIC manual on GOTO, GOSUB, and RETURN. They'll help you get the hang of subroutines.

    Also, notice that the IF(x>0)... is implicit: you don't need to test for it. To make this happen, put a GOTO main at the end of your Start routine.
  • KivistoKivisto Posts: 17
    edited 2008-11-17 18:36
    Alright. Here's the latest I have come up with. Pick it apart burger.gif

    ' {$STAMP BS2}
    DIRA = %100000000000000
    
    X VAR Word
    X = 500
    
    Main:
    IF (IN0=1) THEN START
    GOTO MAIN
    
    Start:
    X = X - 1
    DEBUG ? X, CR
    IF (X=0) THEN COMPLETE
    DO
    UNTIL: IN0=0
    LOOP
    GOTO MAIN
    
    Complete:
    OUT1 =1
    DEBUG "COMPLETE", CR
    
    
  • MikerocontrollerMikerocontroller Posts: 310
    edited 2008-11-17 19:35
    · You need a PBASIC version directive.
    Also :

    DO
    UNTIL:·IN0=0··

    should be changed to:

    DO UNTIL IN0=0

    Another thing I see is that DIRA is declared as sixteen bits.· DIRA is PINS 0-3.· DIRS covers PINS 0-15
    I am not sure if this matters , though
    ·
  • KivistoKivisto Posts: 17
    edited 2008-11-17 19:58
    Thanks. Adding the PBASIC 2.5 directive cleared up a lot of errors.
Sign In or Register to comment.