Shop OBEX P1 Docs P2 Docs Learn Events
sensor — Parallax Forums

sensor

tinatina Posts: 3
edited 2009-02-04 21:30 in Learn with BlocklyProp
hi...I have a code for my quad crawler to walk and sense objects ...for some reasons it started walking just in backward· direction after it sense an object.Please help me out with this one..
Set_Direction:
· SELECT dir
··· CASE Ahead
····· ptrEEPROM = Forward
····· GOSUB Equal
··· CASE ForwardRight
····· ptrEEPROM = Forward
····· GOSUB SlowRight
··· CASE RightSpin
····· ptrEEPROM = RSpin
····· GOSUB Equal
··· CASE BackRight
····· ptrEEPROM = Back
····· GOSUB SlowRight
···· CASE Backward
····· ptrEEPROM = Back
····· GOSUB Equal
··· CASE BackLeft
····· ptrEEPROM = Back
····· GOSUB SlowLeft
··· CASE LeftSpin
····· ptrEEPROM = LSpin
····· GOSUB Equal
···· CASE ForwardLeft
····· ptrEEPROM = Forward
····· GOSUB SlowLeft
· ENDSELECT


SetDelay:
· IF vel < 9 THEN
··· delay = 30
· ELSE
··· delay = 10
· ENDIF


·SELECT dist
··· CASE < 10
····· ptrEEPROM = Back
··· CASE < 15
····· ptrEEPROM = LSpin
··· CASE < 30
····· ptrEEPROM = Forward
····· GOSUB SlowLeft
··· CASE ELSE
····· ptrEEPROM = Forward
····· GOSUB Equal
· ENDSELECT
RETURN

Write_PSC:
· SEROUT PSC,Baud,[noparse][[/noparse]"!SC",ServoAddr,
········ Ramp,servoPosition.LOWBYTE,
········ servoPosition.HIGHBYTE, CR]
· IF rowCounter = 1 THEN
··· PAUSE delay
· ENDIF
RETURN



Get_Sonar:
· Ping = IsLow
· PULSOUT Ping, Trigger
· PULSIN· Ping, IsHigh, dist
· dist = dist */ Scale
· dist = dist / 2··························
RETURN



C_Centimeter:
· dist = dist ** RawToCm··················· ' convert to centimeters
RETURN

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-02-04 03:01
    Generally, it's a good idea to put lots of comments in your code. It helps you remember what you wanted to do 12 months from now, and it helps others (like forum members) quickly get a sense of what your code is supposed to do.

    Also, is this your full code? I don't see any sort of main program loop that tells what is called where, and also a variable declaration might be of issue here.

    Finally, I notice that some of your functions have returns, others don't. While it might work, you may want to go all returns or all GOTOs. Mix and match can make for more difficult code to read.
  • SRLMSRLM Posts: 5,045
    edited 2009-02-04 21:30
    Why do you never call set_direction, setDelay, or get_ping_reading? My impression after reviewing your code is that you've just started adding things, without a definite plan. Bad idea. Sit down with a piece of paper, and describe in simple blocks what you want each section to do and be sure that you know why you're having it do it. Then, code in small, logical functions that clearly state what the meaning is, and with a limited amount of nesting your code will be easier to read. Doing it this way will make it easier to add new functions and capabilities in the future.

    On a syntax level, code is generally easier to read if you choose either GOSUB or GOTO, and avoid mixing them. Personally, I prefer GOSUB since it fits better an OO paradigm.

    Also, when there is lots of code, use the attachment manager to attach it so that it keeps the native format (.bs2) and can be opened in the editor. If you have short blocks of code and you want to embed it into your post, use the [noparse][[/noparse]code] tags so that it keeps the format. You'll get better answers that way if you limit how much effort it takes to read your post.

    Cheers.
Sign In or Register to comment.