Program for BS2
Kivisto
Posts: 17
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!
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
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
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!
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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
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.
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.
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
·