Shop OBEX P1 Docs P2 Docs Learn Events
How to combine two programs? For Christmas surprise, please respond sooner rat — Parallax Forums

How to combine two programs? For Christmas surprise, please respond sooner rat

emmali55emmali55 Posts: 10
edited 2008-12-26 13:32 in BASIC Stamp
I am new to this whole programming thing - basically following the directions in the boe-bot book.· I have two programs that I would like the boe-bot to do simultaneously.· Can anyone help me paste the code together?· It is a scrolling message code and a "follow the light" code.·They are below:

MESSAGE (I don'y know why it is underlined...)
SEROUT 14,84, [noparse][[/noparse]22,12]
DO
PAUSE 250
SEROUT 14, 84, [noparse][[/noparse]12]: PAUSE 5
PAUSE 5

SEROUT 14, 84, [noparse][[/noparse]"MERRY CHRISTMAS"]
SEROUT· 14, 84, [noparse][[/noparse]255,
··············· %00100,
··············· %00100,
··············· %01110,
··············· %01110,
··············· %11111,
··············· %11111,
··············· %00100,
··············· %00100]
SEROUT 14, 84, [noparse][[/noparse]149]
SEROUT 14, 84, [noparse][[/noparse]7]
PAUSE 750
SEROUT 14, 84, [noparse][[/noparse]"· I love YOU!"]
PAUSE 2000
SEROUT 14, 84, [noparse][[/noparse]12]
PAUSE 500

LOOP


FOLLOW LIGHT
DEBUG "Program Running!"·········· 'Roam with Light, Sit when dark.
· leftAmbient··· CON 380
· rightAmbient·· CON 230
· leftBright···· CON 32
· rightBright··· CON 20

· leftThreshold· CON leftBright + leftAmbient / 2· * 5 / 8
· rightThreshold CON rightBright + rightAmbient / 2 * 5 / 8
· darkThreshold· CON 200

· timeLeft······ VAR Word
· timeRight····· VAR Word

FREQOUT 4,2000,3000
DO
· GOSUB test_photoresistors
· GOSUB navigate
LOOP
test_photoresistors:
· HIGH 6
· PAUSE 3
· RCTIME 6, 1, timeLeft
· HIGH 3
· PAUSE 3
· RCTIME 3, 1, timeright
· RETURN

Navigate:
· DO
····· IF timeRight > darkThreshold AND timeLeft > darkThreshold THEN
····· NAP 6····· ' Sleep for about one second, then repeat loop
·· ELSE
····· EXIT········' Continue with rest of program
·· ENDIF
LOOP
· IF (timeLeft < leftthreshold) AND (timeRight < rightthreshold) THEN
··· PULSOUT 13, 850
··· PULSOUT 12, 650
· ELSEIF (timeLeft < leftthreshold) THEN
··· PULSOUT 13, 700
··· PULSOUT 12, 700
· ELSEIF (timeRight < rightthreshold)THEN
··· PULSOUT 13, 800
··· PULSOUT 12, 600
· ELSE
··· PULSOUT 13, 750
··· PULSOUT 12, 750
· ENDIF
· PAUSE 20
·RETURN····························· 'END Light program
·

Comments

  • SRLMSRLM Posts: 5,045
    edited 2008-12-23 03:17
    Probably the easiest thing to do would be to have methods, and you simple call them from the main loop. Something like this:

    DO
    GOSUB message
    GOSUB followLight
    LOOP

    You'll have to get rid of the DO-LOOP pairs in the two programs that you posted, and replace the LOOP part with RETURN. Also, to make them functions you'll want to add a : (colon) after the 'title' (and make follow light one word).
  • SRLMSRLM Posts: 5,045
    edited 2008-12-23 03:22
    Another couple of things:

    You may want to get rid of some of your long pauses (just to make things happen faster)
    The variable and constant declarations should be moved out if the follow light function and placed at the top of the program.
  • emmali55emmali55 Posts: 10
    edited 2008-12-23 03:23
    OK. I was trying that but I think things were in the wrong order - I tried adding the 'Message' into the 'Light' program. You are saying I should create a whole new thing that includes 'light' and 'message' as sub categories. Makes sense. [noparse]:)[/noparse] Thank you I will try.
  • emmali55emmali55 Posts: 10
    edited 2008-12-23 03:50
    I did as you sugested and made:
    DO
    GOSUB follow_light
    GOSUB scroll_message
    LOOP
    etc.

    But it only runs the first one. i.e. if set up like above, will only run follow_light. If I switch their positions, it will only run the scroll_message. It loads the program OK and doesn't find anything wrong with it. Do you know what is going on?
  • emmali55emmali55 Posts: 10
    edited 2008-12-23 03:59
    OK, I took out the loop in 'message' so it doesn't flash -just sits. [noparse]:([/noparse]

    And I think we are in business! I like it flashing better but if this is what it takes to make it work, I will live with it. Thanks for your help.
  • SRLMSRLM Posts: 5,045
    edited 2008-12-23 05:01
    Hmm... Can you post your code as you have it? Upon more inspection, and another thought. You light following/waiting won't work right without it being in a loop. Therefore, the main function in your program should go something like this:

    DO
       GOSUB Message
       GOSUB test_photoresisitors
       IF timeRight > darkThreshold AND timeLeft > darkThreshold THEN
          NAP 6      ' Sleep for about one second, then repeat loop
       ELSE
          GOSUB Navigate
       ENDIF
    LOOP
    
    



    By doing it this way, you can break it down into more logical blocks. Of course, this necessitates changing some code, namely the navigate routine and message routine. The problem occurs when the servos need to be refreshed every 20 ms, while your message routine takes several seconds.
  • dpeterson3dpeterson3 Posts: 4
    edited 2008-12-23 13:12
    If he just coppied and pasted the code, then it is stuck in the do loops. It will only execute one because it loops forever (or in the light's case, until it finds light).
  • Shawn LoweShawn Lowe Posts: 635
    edited 2008-12-23 15:30
    The basic stamp wont do both programs at the same time. It will have to do one follow light and then message. Having said that, post what code you have and maybe someone can help tweek it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    When all else fails.....procrastinate!
  • SLUG-1SLUG-1 Posts: 28
    edited 2008-12-24 00:55
    A servo controller could help with this i do believe, however it is 2 days to christmas!
  • SRLMSRLM Posts: 5,045
    edited 2008-12-25 20:48
    He can (or could have) done it all with the BS2, it just takes a little bit of thought to figure out when to update the LCD and when to do the servo actions.
  • emmali55emmali55 Posts: 10
    edited 2008-12-26 13:31
    It worked well and he was happy with my surprise. Thbanks for all your help! I had so much fun doing this.
  • emmali55emmali55 Posts: 10
    edited 2008-12-26 13:32
    It worked well and he was happy with my surprise. Thbanks for all your help! I had so much fun doing this.
Sign In or Register to comment.