Shop OBEX P1 Docs P2 Docs Learn Events
Some help for a beginner — Parallax Forums

Some help for a beginner

JudithJudith Posts: 9
edited 2008-01-06 23:49 in BASIC Stamp
Hey, I'm pretty new to microcontrollers, so I was wondering whether anyone could help me with what I'm trying to make. The overall goal is to make a vehicle that travels a certain distance and then stops when it reaches that distance. I want to hook up a switch to one of the inputs on the BASIC stamp--when the switch closes the circuit, the BASIC stamp saves it to a variable. So say I press the switch 5 times, the variable will say 5. Then, an output switch will send out a signal to a servo, and the number of times the signal is sent out will be the number of times the switch was pressed, basically (I'll have to multiply the number of times its looped by a certain number so that it travels the distance I want it to). Once its repeated this many times, the motor will engage in reverse, braking the vehicle. This is the code I've written so far:

DIRS=%0000000000000011
SwitchIn VAR BYTE
TotalSwitch VAR WORD
PULSIN 4, SwitchIn
DO
IF SwitchIn=1 THEN TotalSwitch = TotalSwich+1
LOOP
i VAR WORD
i = TotalSwitch
DO
PULSOUT 15, 300
PAUSE 10
LOOP
i=20
DO
PULSOUT 15, -300
PAUSE 10
LOOP
END



I have a couple of questions--first, I know that the BASIC stamp will read the PULSIN command for every 2 microseconds that power is run through it--so say I press the switch for 8 microseconds, will that create 4 separate instances of PULSIN that will be stored in the variable? Also, I'm pretty sure the code is wrong in lots of places...this is my first time coding a microcontroller :P...any suggestions on how to fix the places I've screwed up in?

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-12-26 01:03
    For the servo values of a BS2 they should fall in the range 500-1000 (-300 is not valid ) with 750 being a stopped or neutral position. PULSOUT 15,600 will drive the motor one way and PULSOUT 15,900 will drive it the other way.

    Look at the BUTTON instruction for the switch input.

    Look at the FOR...NEXT instruction to cycle the number of times the button was pressed

    When the code exits the FOR ... NEXT loop then stop the motor with PULSOUT 15,750

    Jeff T
  • JudithJudith Posts: 9
    edited 2007-12-26 03:01
    ah ok, thanks a lot! So do you think this will work? I have 2 buttons--one to actually say how long I want it to loop for, then another one to tell it when I've stopped enterint input, because I'm not sure how else I would make it jump to the next bit of code.


    
    DIRS=%0000010000000011 'all input except p15 and p16
    
    gWorkspace1 VAR BYTE
    gButtonCounter VAR NIB
    gWorkspace2 VAR BYTE
    repeat VAR WORD
    i VAR WORD
    DO
    BUTTON1:
    LOOP:
        'BUTTON Pin, DownState, Delay, Rate, Workspace, TargetState, Address
        BUTTON 4, 1, 255, 0, gWorkspace1, 1, press1
    
    noPress1:
        DEBUG "no, not pressed. ", DEC ? gButtonCounter
        GOTO BUTTON1
    
    press1:
    
        gButtonCounter = gButtonCounter + 1
        DEBUG "yes, pressed", DEC ? gButtonCounter
    
        
        GOTO BUTTON1
    
    DO
    LOOP:
        BUTTON 5, 1, 255, 0, gWorkspace2, 1, press2
    
    press2:
        DEBUG "stop counting", DEC ? gButtonCounter
        GOTO main
    
    main:
    DO
    repeata:
    LOOP
    gButtonCounter=i
        PULSOUT 15, 600
        PAUSE 10
        i=i-1
        IF i>0 THEN GOTO repeata
        IF i=0 THEN GOTO repeatb
    DO
    repeatb:
    LOOP
        PULSOUT 15, 900
        PAUSE 10
        PULSOUT 15, 900
        PAUSE 10
        PULSOUT 15, 900
        PAUSE 10
        PULSOUT 15, 750
        PAUSE 10
    END
    
    
    
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-12-26 03:44
    Heres an adaptation of the button instruction in the IDE help file

    Time_out CON 500
    Btn············ PIN···· 0
    btnWrk········· VAR···· Byte
    Idx VAR Word
    Pressed VAR Byte
    Main:

    DO WHILE Idx<Time_out
    · PAUSE 5
    · BUTTON Btn, 0, 255, 255, btnWrk, 0, No_Press
    · DEBUG "*"
    · Pressed=Pressed+1
    · Idx=0
    · No_Press:
    · Idx=Idx+1
    LOOP
    DEBUG CR,"Timed out" ," ",? Pressed
    END

    Once you cease pressing the button the variable Time_out controls how long it is before the program continues. Run this as stand alone code and play with the settings.

    Jeff T.
  • JudithJudith Posts: 9
    edited 2008-01-06 16:05
    Thanks a lot for the help. I have managed to test the button and servo circuits separately and everything works fine, but when I star the program, it loops endlessly for some reason, even though I only want it to loop once.
    Here's the code:

    Btn VAR Byte
    Time VAR Word
    Btn = 0
    Counter VAR Word
    DO
    
    DEBUG ? IN4
    
    IF (IN4 = 1) THEN
    Btn = Btn+1
    PAUSE 250
    DEBUG ? Btn
    ELSE
    PAUSE 250
    Time=Time+1
    ENDIF
    
    LOOP UNTIL Time = 200
    DEBUG "Starting Servo"
    
    FOR Counter = 1 TO (BTN * 10)
    PULSOUT 14, 1000
    PAUSE 20
    NEXT
    
    DEBUG "Braking", CR
    FOR Counter = 1 TO 25
    PULSOUT 14, 500
    PAUSE 20
    NEXT
    
    DEBUG "DONE", CR
    
    END
    
    
    


    when I run it, it just debugs IN4 and Btn, and doesn't start the servo nor timeout...
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-01-06 17:35
    Hi Judith, from what you say IN4 is always 1. This is probably because of the way the button is connected to the Stamp. I am assuming the button has normally open contacts, it should be wired this way ,

    VDD to one side of the button

    on the other side of the button connect a 220 ohm resistor

    connect the other end of the 220 ohm resistor to P4

    now take a 10K ohm resistor and connect one end to P4

    connect the other end of the 10K resistor to VSS

    try your program again and see how it responds

    Jeff T.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-01-06 17:45
    Judith -

    Indeed if IN4 always equals 1, the program will loop forever since the variable TIME will never be incremented, and thus will never reach the LOOP UNTIL value of 200. You never mention what the DEBUG values are for IN4 and Btn. It might be interesting to put in a DEBUG statement for TIME as well.

    You don't mention what is attached to pin port 4, nor how it is hooked up (circuitry).

    I suppose it's fair to presume that "Starting Servo" is never displayed?

    You have also not indicated to us, nor to the PBASIC compiler which PBASIC Stamp you are using?

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • JudithJudith Posts: 9
    edited 2008-01-06 20:42
    Bruce and Jeff,

    Sorry, in the original code I have
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    


    at the beginning, I just forgot to paste it into my post.

    I have a switch connected to pin 4 through a 200 ohm resistor, with a 10k pulldown resistor. The debug values for IN4 indicate that the button circuit is working--it's 0 when it's not pressed, and 1 when it is. The Btn values also seem to be working, as they store the number of times I press the button. When I put in a DEBUG statement for TIME, it went up to 200, then displayed "Starting Servo" and restarted. Also, the Btn variable resets itself. However, the servo itself (which is correctly connected to pin 14, I checked with a test program and it worked fine) never turns on. That's what makes me think I wrote the loop code wrong...

    Judith

    Post Edited (Judith) : 1/6/2008 9:04:55 PM GMT
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-01-06 22:14
    Judith -

    If you are powering the servo directly from the Stamp's power supply, it may be causing a brown-out condition, and the Stamp itself is resetting. You can check that by adding the following DEBUG statement right at the beginning of your program, before any looping takes place: DEBUG "STARTING". "STARTING" should appear once and ONLY once during any iteration of the program. If it appears more than once, the Stamp is being RESET.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • JudithJudith Posts: 9
    edited 2008-01-06 23:36
    Bruce,
    Thanks! That seems to have been the problem, I gave the servo its own 9V power supply and it's working fine [noparse]:)[/noparse]

    Thanks again,
    Judith
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-01-06 23:49
    Judith -

    Using 9 volts DC for a servo is a bit high. Generally one doesn't supply them with more than 6-7.5 volts DC.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sign In or Register to comment.