Shop OBEX P1 Docs P2 Docs Learn Events
help with some bs2 code — Parallax Forums

help with some bs2 code

gennarobasso81gennarobasso81 Posts: 84
edited 2012-09-20 10:22 in BASIC Stamp
hello all,

I am having some trouble getting my code to do exactly what i want. Maybe someone can give me some pointers.

What i am looking for is for the LCD, when powered on only, to say "HELLO GENO", Then to go back to clear and never come up again while the code is running. Here is what i have

' {$STAMP BS2}
' {$PBASIC 2.5}
'---------------[Variables]-----------------------------
LR              VAR     Word
UD             VAR     Word
btn1wrk      VAR     Byte
btn2wrk      VAR     Byte

'---------------[Constants]-----------------------------
BUTTON btn1, 1, 0, 0, btn1Wrk,0, No_Press
BUTTON btn2, 1, 0, 0, btn2Wrk,0, No_Press
Btn1            PIN       1
Btn2            PIN       2
stickUD       CON     24
stickLD        CON     27

'----------------[Initialize]---------------------------
SEROUT 15, 84,200, [22,12, "Hello", 13, "Geno"]
 
 
'-------------------------[Main]---------------------------
 
Main:

HIGH 4
PAUSE 2
RCTIME 4, 1, UD
HIGH 11
PAUSE 2
RCTIME 11, 1, LR

DEBUG HOME, "UD =", DEC UD, CLREOL, CR,
"LR = ", DEC LR, CLREOL

IF UD > stickUD THEN GOSUB Forward
IF UD < stickUD THEN GOSUB Backwards
IF UD = stickUD THEN GOSUB Clear

IF LR > stickLD THEN GOSUB Right
IF LR < stickLD THEN GOSUB Left
IF LR = stickLD THEN GOSUB Clear

IF btn1 =1 THEN
DEBUG CR, "Button 1 pushed"
GOSUB Left_Turn
DEBUG CLS
ENDIF

IF btn2 = 1 THEN
DEBUG CR, "button 2 pushed"
GOSUB Right_Turn
DEBUG CLS
ENDIF

No_Press:
 GOTO main

GOTO main
 
 
 

'-------------------------[Sub Routine]-----------------------
Forward:
SEROUT 15,84, 20, [17, 128, "Forward"]
PAUSE 200
RETURN
Backwards:
SEROUT 15, 84, 20, [17, 128, "Backwards"]
PAUSE 200
RETURN
Right:
SEROUT 15, 84, 20, [17, 148, "Right"]
PAUSE 200
RETURN
Left:
SEROUT 15, 84, 20, [17,148, "left"]
PAUSE 200
RETURN
Clear:
SEROUT 15, 84, [12,17]
PAUSE 200
RETURN

Left_Turn:
IF  Btn1 = 1 THEN     SEROUT 15, 84, ["Left", 13, "Turn"]
                      PAUSE 1200
IF  Btn1 = 0 THEN     SEROUT 15, 84, [12]
                      PAUSE 5
                      RETURN
 

Right_Turn:
IF btn2 = 1 THEN      SEROUT 15, 84, ["Right", 13, "Turn"]
                      PAUSE 1200
IF btn2 = 0 THEN      SEROUT 15, 84, [12]
                      PAUSE 5
                      RETURN

Comments

  • davejamesdavejames Posts: 4,047
    edited 2012-09-14 20:06
    Hello Geno,

    Make the changes I've marked in red and see if it's what you want:
    '----------------[Initialize]---------------------------
    SEROUT 15, 84,200, [22,12, "Hello", 13, "Geno"]
    
    [COLOR=#ff0000]PAUSE 5000 'wait 5 seconds to viwe the sign-on
    **another SEROUT with the LCD "clear" code (12, I believe)
    [/COLOR]'-------------------------[Main]---------------------------
    
    
  • walkin79walkin79 Posts: 29
    edited 2012-09-15 03:24
    try moving your hello statement just below main, then creat a new subroutein "anything" for the rest of the program chane your goto statments to goto "anything"
  • vaclav_salvaclav_sal Posts: 451
    edited 2012-09-16 09:27
    I can't really run your code on my setup. But here are some general comments:
    Where is your LCD initialization code? LCD takes time ( in ms) to initialize and it should be part of Main as any other code.
    Second, do you really want to check UD and LR in sequence and depending on returns from subroutines? Basically UD gets checked first for value and if the first IF passes than the second one gets checked ( return from subroutine) even if it cannot be true anymore anyway and so on. I think you wanted to use SELECT or other command.
    Than the BUTTON gets executed only once, again outside Main. To continually monitor the buttons state the code needs to be in loop.

    As definitive plus – you are on right track using DEBUG !
  • gennarobasso81gennarobasso81 Posts: 84
    edited 2012-09-17 18:39
    here is what i'm doing. I'm making a wireless remote control for a boe-bot. I dont have the wireless yet. The problem with putting the lcd into the main is it will do the "Hello Geno" everytime. I just want the Hello Geno upon the power on. I added a little switch to the remote so the whole thing is inside a box and i can turn it on and off. As for the next part with the UD and LR checking....that is what will determine if my boe bot will move forward, backward or left and right. The LCD will display the direction of movement. I dont know any other way to write it.

    I was checking over my code. I think i might have messed up in the Initialization part. I have
    [Initialize]
    SEROUT 15, 84,200, [22,12, "Hello", 13, "Geno"]

    I have 12 in the code...12 is to clear. I think that might be my problem. Not sure as my son (He's only 3) broke my set up and i haven't had the time to sit down and set up my tools to resolder it. Maybe tonight i'll have time and post a few pics. I have some pics posted in another thread but they are the begining of my project.
  • vaclav_salvaclav_sal Posts: 451
    edited 2012-09-17 20:11
    Friend, you need to back-up a little. At this point it really does not matter what you are trying to accomplish with your hardware unless you understand the programming process better.
    Irregardless if you use serial or parallel LCD - your program need to initialize the LCD first, SEROUT will not work until you do that. At best you may see something wild on the LCD because it does run default initialization on power up.

    Start with checking out the editor example code for LCDCMD command.
  • davejamesdavejames Posts: 4,047
    edited 2012-09-17 22:01
    Geno - did you try what I suggested?
  • gennarobasso81gennarobasso81 Posts: 84
    edited 2012-09-18 12:25
    Dave,

    Yes i did try what i think u were suggesting. This is my first code ever....so i'm not intirely sure i know what i'm doing. I played around with ur suggestions....and changed a few things each time. Here is what i came up with:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '---------------[Variables]-----------------------------
    LR            VAR     Word
    UD            VAR     Word
    btn1wrk       VAR     Byte
    btn2wrk       VAR     Byte
    
    '---------------[Constants]-----------------------------
    BUTTON btn1, 1, 0, 0, btn1Wrk,0, No_Press
    BUTTON btn2, 1, 0, 0, btn2Wrk,0, No_Press
    Btn1          PIN     1
    Btn2          PIN     2
    stickUD       CON     24
    stickLD       CON     26
    
    '----------------[Initialize]---------------------------
    PAUSE 5
    SEROUT 15, 84, [22,12]
     
     
    '-------------------------[Main]---------------------------
     
    Main:
    SEROUT 15, 84,5000, [22,17, "Hello", 13, "Geno"]
    PAUSE 1000
    Sub_Main:
    HIGH 4
    PAUSE 2
    RCTIME 4, 1, UD
    HIGH 11
    PAUSE 2
    RCTIME 11, 1, LR
    DEBUG HOME, "UD =", DEC UD, CLREOL, CR,
    "LR = ", DEC LR, CLREOL
    IF UD > stickUD THEN GOSUB Forward
    IF UD < stickUD THEN GOSUB Backwards
    IF UD = stickUD THEN GOSUB Clear
    IF LR > stickLD THEN GOSUB Right
    IF LR < stickLD THEN GOSUB Left
    IF LR = stickLD THEN GOSUB Clear
    IF btn1 =1 THEN
    DEBUG CR, "Button 1 pushed"
    GOSUB Left_Turn
    DEBUG CLS
    ENDIF
    IF btn2 = 1 THEN
    DEBUG CR, "Button 2 pushed"
    GOSUB Right_Turn
    DEBUG CLS
    ENDIF
    
    No_Press:
     GOTO sub_main
    
    GOTO sub_main
     
     
     
    
    '-------------------------[Sub Routine]-----------------------
    Forward:
    SEROUT 15,84, 20, [17, 128, "Forward"]
    PAUSE 200
    RETURN
    Backwards:
    SEROUT 15, 84, 20, [17, 128, "Backwards"]
    PAUSE 200
    RETURN
    Right:
    SEROUT 15, 84, 20, [17, 148, "Right"]
    PAUSE 200
    RETURN
    Left:
    SEROUT 15, 84, 20, [17,148, "left"]
    PAUSE 200
    RETURN
    Clear:
    SEROUT 15, 84, [12,17]
    PAUSE 200
    RETURN
    
    Left_Turn:
    IF  Btn1 = 1 THEN     SEROUT 15, 84, ["Left", 13, "Turn"]
                          PAUSE 1200
    IF  Btn1 = 0 THEN     SEROUT 15, 84, [12]
                          PAUSE 5
                          RETURN
     
    
    Right_Turn:
    IF btn2 = 1 THEN      SEROUT 15, 84, ["Right", 13, "Turn"]
                          PAUSE 1200
    IF btn2 = 0 THEN      SEROUT 15, 84, [12]
                          PAUSE 5
                          RETURN
    


    and still not getting it to do what i'd like.
  • gennarobasso81gennarobasso81 Posts: 84
    edited 2012-09-18 12:39
    i got it! :)
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    
    '----------------[Initialize]---------------------------
    PAUSE 5
    SEROUT 15, 84, [22,12]
    
    SEROUT 15, 84,50, [22,17, "Hello", 13, "Geno"]
    PAUSE 1000
    SEROUT 15, 84, [22,12]
    PAUSE 5
    '---------------[Variables]-----------------------------
    LR            VAR     Word
    UD            VAR     Word
    btn1wrk       VAR     Byte
    btn2wrk       VAR     Byte
    
    '---------------[Constants]-----------------------------
    BUTTON btn1, 1, 0, 0, btn1Wrk,0, No_Press
    BUTTON btn2, 1, 0, 0, btn2Wrk,0, No_Press
    Btn1          PIN     1
    Btn2          PIN     2
    stickUD       CON     23
    stickLD       CON     25
    
    '-------------------------[Main]---------------------------
     
    Main:
    
    HIGH 4
    PAUSE 2
    RCTIME 4, 1, UD
    HIGH 11
    PAUSE 2
    RCTIME 11, 1, LR
    DEBUG HOME, "UD =", DEC UD, CLREOL, CR,
    "LR = ", DEC LR, CLREOL
    IF UD > stickUD THEN GOSUB Forward
    IF UD < stickUD THEN GOSUB Backwards
    IF UD = stickUD THEN GOSUB Clear
    IF LR > stickLD THEN GOSUB Right
    IF LR < stickLD THEN GOSUB Left
    IF LR = stickLD THEN GOSUB Clear
    IF btn1 =1 THEN
    DEBUG CR, "Button 1 pushed"
    GOSUB Left_Turn
    DEBUG CLS
    ENDIF
    IF btn2 = 1 THEN
    DEBUG CR, "Button 2 pushed"
    GOSUB Right_Turn
    DEBUG CLS
    ENDIF
    
    No_Press:
     GOTO main
    
    GOTO main
     
     
     
    
    '-------------------------[Sub Routine]-----------------------
    Forward:
    SEROUT 15,84, 20, [17, 128, "Forward"]
    PAUSE 200
    RETURN
    Backwards:
    SEROUT 15, 84, 20, [17, 128, "Backwards"]
    PAUSE 200
    RETURN
    Right:
    SEROUT 15, 84, 20, [17, 148, "Right"]
    PAUSE 200
    RETURN
    Left:
    SEROUT 15, 84, 20, [17,148, "left"]
    PAUSE 200
    RETURN
    Clear:
    SEROUT 15, 84, [12,17]
    PAUSE 200
    RETURN
    
    Left_Turn:
    IF  Btn1 = 1 THEN     SEROUT 15, 84, ["Left", 13, "Turn"]
                          PAUSE 1200
    IF  Btn1 = 0 THEN     SEROUT 15, 84, [12]
                          PAUSE 5
                          RETURN
     
    
    Right_Turn:
    IF btn2 = 1 THEN      SEROUT 15, 84, ["Right", 13, "Turn"]
                          PAUSE 1200
    IF btn2 = 0 THEN      SEROUT 15, 84, [12]
                          PAUSE 5
                          RETURN
    
  • davejamesdavejames Posts: 4,047
    edited 2012-09-18 13:12
    ...all right! There ya go! :thumb:

    Isn't it GREAT when things work as wanted?

    Thanks for the follow up.
    .
  • gennarobasso81gennarobasso81 Posts: 84
    edited 2012-09-19 06:49
    vaclav_sal wrote: »
    I can't really run your code on my setup. But here are some general comments:
    Where is your LCD initialization code? LCD takes time ( in ms) to initialize and it should be part of Main as any other code.
    Second, do you really want to check UD and LR in sequence and depending on returns from subroutines? Basically UD gets checked first for value and if the first IF passes than the second one gets checked ( return from subroutine) even if it cannot be true anymore anyway and so on. I think you wanted to use SELECT or other command.
    Than the BUTTON gets executed only once, again outside Main. To continually monitor the buttons state the code needs to be in loop.

    As definitive plus – you are on right track using DEBUG !

    Vaclav,

    thank you for ur suggestions. I think i am going to try to rewrite the code with different commands. I dont know enough about SELECT to use it yet...so i'm am going to read the basic syntex manuel. If you are around in the future maybe you'll check it out for me once i post it

    Thanks again

    P.S. My main thread is educational group project for all of us that are new to electronics if you would like to check out some pictures of the project i am working on or see updated code
  • vaclav_salvaclav_sal Posts: 451
    edited 2012-09-20 10:22
    Now it is time I slow down a little. Sorry.
    I was referring to the logic flow of your IF statements.

    IF UD > stickUD THEN GOSUB Forward

    If the above is true subroutine Forward is executed and program flow retruns to this

    IF UD < stickUD THEN GOSUB Backwards

    but you already determined that UD > stickUD so why do you check for UD < stickUD?
    Same wiht next statement
    IF UD = stickUD THEN GOSUB Clear

    Now you do same for LR

    IF LR > stickLD THEN GOSUB Right
    IF LR < stickLD THEN GOSUB Left
    IF LR = stickLD THEN GOSUB Clear


    The above code will execute correctly but it is unnecessary complex. Not that it matters in this simple case, but it may matter when things get more complicated.

    Similar with this snippet

    IF Btn1 = 1 THEN SEROUT 15, 84, ["Left", 13, "Turn"]
    PAUSE 1200
    IF Btn1 = 0 THEN SEROUT 15, 84, [12]
    PAUSE 5
    RETURN

    It would follow the logic better , button can be 1 or 0 only , if you write it this way
    IF Btn1 = 1 THEN
    SEROUT 15, 84, ["Left", 13, "Turn"]
    PAUSE 1200
    ELSE
    SEROUT 15, 84, [12]
    PAUSE 5
    ENDIF

    RETURN

    Again, you code will work , but it is not necessary to evaluate button twice.

    I would still recomend you do some study on the LCD operation.
    Apparently you are using serial LCD in default setting. Either by accident or by desire, I cannot tell from the code.
    You are aware that you can make the characters flash or undeline them, right? But that is not power on default setting. And most LCD have an additional RAM you can use for temporary data storage also.
Sign In or Register to comment.