help with some bs2 code
gennarobasso81
Posts: 84
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
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
Make the changes I've marked in red and see if it's what you want:
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 !
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.
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.
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:
and still not getting it to do what i'd like.
Isn't it GREAT when things work as wanted?
Thanks for the follow up.
.
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
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.