Need help with a BS2 program
MYM
Posts: 9
I need to make this code work so that when you enter a destination, the input from pin3 will make the 'elevator' go UP or DOWN. While output will be to PIN9 and PIN10.
But since this code has a lot of 'bugs' on it I would really appreciate if anyone could help me.
Thank You.
But since this code has a lot of 'bugs' on it I would really appreciate if anyone could help me.
Thank You.
' {$STAMP BS2} ' {$PBASIC 2.5} cf VAR Byte 'current floor = cf df VAR Byte 'destination floor -=df cf = 0 df = 0 Start: DEBUG CLS DEBUG DEC cf 'maybe not neccesary DEBUG "Current Floor = ",cf ,CR DEBUG "Enter Destination",CR DEBUGIN NUM df IF df > cf AND df <=9 THEN GOTO Up IF df < cf AND df >=0 THEN GOTO Down IF df = cf THEN GOTO Start Up: DEBUG "Elevator ascending", CR DO DEBUG DEC (0+cf) ,CR PAUSE(500) DO DEBUG DEC (0+cf) ,CR IF NOT IN3 = 1 THEN EXIT LOOP cf=cf+1 DEBUG "current floor = ",cf DEBUG DEC (0+cf) ,CR LOOP UNTIL cf = df GOTO Start Down: DEBUG "Elevator Descending" DO DEBUG DEC (0+df) PAUSE (500) DO DEBUG DEC (0+df) IF NOT IN3 = 1 THEN EXIT LOOP df = df + 1 cf = cf - 1 DEBUG "Destination floor = ",df DEBUG (0+df) LOOP UNTIL df = df GOTO Start END
Comments
First - It's not at all clear what IN3 is, why it's in the program or how it's supposed to affect the program.
Second - There should NEVER be a reason to change the destination floor (dl). That should be viewed as fixed data, and user input, if I understand the program correctly.
Third - The DO .... LOOP may or may not be helpful or it just may be adding more confusion and complexity to the logic. That really depends on what IN3 is, and what it is supposed to do.
Fourth - You say in the limited explanation of the program "output will be to PIN9 and PIN10", yet neither PIN9 nor PIN10 are referenced anywhere. It's also not clear HOW they are supposed to represent the output?
Fifth - I can't understand the logic of the following line of code in the DOWN routine:
LOOP UNTIL df = df
df will ALWAYS equal df !
Let's clean that much up. and go from there.
Regards,
Bruce Bates
I too read through your code and was stumped at the loop and checking in3, as the loop sends on a BS2 for example 4000 bytes per second to the display running at 2000 lines of code per second, of course the debug command slows the output to what it can handle but you should keep in mind and have logic built in for debug output that reduces the effect it has on your program.
Also it is good practice to put a blank line before the DO and after thre LOOP to make it easier for humans read the code
Rewrite the code just the way you have it, indent the code between DO and LOOP and If then Endifs and add comments
I think you will find the bugs yourself when you do this, if you don't find them just post the modified code here and I or others can help.
After further review of your program, it seems your DOWN routine changes both the destination and the current floor
so if df=5 on entry and cf=6 your code would
df=df+1 ' would now be 6
cf=cf-1 ' would now be 5
and the code would never exit
Both subroutines should be exactly the same for up and down it would seem to me, that being the case there is no need for seperate routines at all
making a new variable that gives the direction of the move would be better and a subroutine of just 6 lines of code will work.
in your main loop you would call the subroutine like this
if cf<>df then gosub movesoubroutine ' if current floor is not equal to destination floor then move elevator
And on new input the direction flag would be set.
This would allow a que to be set up on input, as floor number destinations come in they are put into a que (array variable) a timing routine could hold the elevator at the current floor for a specified time and then go to the next floor in the que. Also in that case additional logic would be to check the last direction moved and look for (if it was down) any additional floors that are lower than the current floor and go their first. sorting the list of floors in the buffer after new input would allow a better flow up and down. You dont want to go to floor 9 then floor 1 and then floor 5 if you are passing 5 on the way to 1, even though input for floor 1 may come before floor 5.
So try to refine your code to the least amount of instructions or you end up with what we call spaghetty code that you can't read a few weeks after you write it.
Post Edited (metron9) : 11/4/2005 3:42:48 PM GMT
I recently agonized over a similar program,then worked with the information in the book.It will lead you through the steps in simple terms.