Shop OBEX P1 Docs P2 Docs Learn Events
A command I need — Parallax Forums

A command I need

power_speedpower_speed Posts: 5
edited 2006-08-24 14:14 in BASIC Stamp
Hello all,

I am using a basic stamp 2 with pbasic 2.5.

I need to figure out a way to essentlaly say when a certain input of pulses reaches a certain number it will turn a certain pin high. AFter a short Delay that pin will go back low.

The program still needs to count pulses even while bringing pins high and low.

THen it needs to do the whole thing over again.

A basic idea I had was if there was a "when" command I could just say when pulsein = 45 or so bring pin 1 high.
Anybody think that might work.

Has anybody got any better ideas?

Thanks in Advance!

Comments

  • power_speedpower_speed Posts: 5
    edited 2006-08-24 03:13
    This is what I have so far

    PULSIN pulse, 1, time 'counts pulses from the optoisolater and stores it in var time
    IF time = 45 'measures the counted pulses, change in accordance with the opto disk used
    THEN HIGH 1 'brings pin one high
    PAUSE 20 'pause for 20ms
    LOW 1 'bring pin 1 low
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-08-24 03:24
    power_speed -

    I'm not sure exactly what you're looking for, but you might look at DO ... UNTIL, or DO ... WHILE.

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 8/24/2006 3:35:23 AM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-08-24 12:52
    You say you want to 'count' pulses, but your code shows you

    'measuring' a pulse width.· So the attached does the 'measuring'.
  • metron9metron9 Posts: 1,100
    edited 2006-08-24 14:14
    Somebody said...
    A basic idea I had was if there was a "when" command I could just say when pulsein = 45 or so bring pin 1 high.


    It looks like you want to count pulses not measure their width(time on or time off)
    There is a when command in the context you describe only its called IF THEN construct

    Assuming your pulsein is a variable that your code keeps track of. >>>this is not the pulsin command<<<

    IF pulsein = 45 THEN bring pin 1 high

    so a program loop might look like this (in psudo code, psudo code= non executable code for demonstration of program flow)

    pulsein = varaible
    inputpin = pin you are measuring pulse on
    buttonflag = variable

    pulsein=0
    main program loop

    if pulsein=45 ; did we count 45 pulses yet?
    pin1=high ; yes so make pin1 high
    pin1counter=50 ;A counter to measure the time pin1 stays on
    reset pulsein to 0 ;reset the counter
    endif

    ;code continues here

    >>>counters to count pulses and keep track of time pin1 is on<<<

    if buttonflag=0
    if inputpin=1
    pulsein=pulsein+1 ;here is where we count pulses
    buttonflag=1 ;turn on a flag so we know button is pressed down
    endif
    endif

    if inputpin=0
    buttonflag=0 ;reset button flag
    endif


    if pin1counter>0
    pin1counter=pin1counter-1
    if pin1counter=0
    pin1=LOW ;reset pin1 to low after counter runs out
    endif
    endif

    goto mainloop


    so your program runs in a loop checking variuos flags and pin conditions and setting and resetting pins and flags depending on the condition of all the various flags and pins.

    You have to take into account how fast the loop executes, to have timw for other processing you need to define your input source of the pulses, I assumed a button press by a person, without the buttonpress flag the counter would go to 45 as soon as you press the button because the BS2 is running 2000 lines of code per second. Read some GAME programming using BASIC for most any computer to learn about the type of loop structure above. The other way is to use interrupts to count but that is not available in the basic stamp 2 model.

    I wish you luck in your programming future.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
Sign In or Register to comment.