Shop OBEX P1 Docs P2 Docs Learn Events
programming question — Parallax Forums

programming question

brian6592brian6592 Posts: 18
edited 2011-11-25 22:04 in BASIC Stamp
I need some help sorting out what seems to be a coding issue. My goal is to detect whether a color is black or white, and perform an action for the two scenarios.
I have made a separate program that tests the sensors I have (ir led and receiver (the type with three leads, not the ones that look like leds)), and it works perfect. If i put a white sheet of paper under the sensor, a fan turns on, if i put a black sheet of paper under it, the fan turns off. Here is the simple code for that program:

' {$STAMP BS2}
' {$PBASIC 2.5}


colordetect VAR Bit


DO
FREQOUT 7, 1, 38500
colordetect = IN1


IF colordetect = 0 THEN
HIGH 14
ELSE
LOW 14
ENDIF
LOOP

My problem occurs when i try to incorporate this into my main code. The main process calls upon the Find_Home subroutine when my other tasks for the robot have been accomplished. The robot has to get back to where it started from, which is indicated by a white sheet of paper rather that black. The robot just keeps moving (it does avoid things btw), but it never stops and ends the program. I don't want to go back to the main code once i go to this subroutine. I want it to either end the program or keep looking for home, so do i need the return command? Here is the subroutine from my main program:


Find_Home:
PULSOUT 12, 650
PULSOUT 13, 850


GOSUB irdetection


IF (irdetectleft = 0) OR (irdetectright = 0) THEN
GOSUB iravoid
ENDIF


PULSOUT 12, 650
PULSOUT 13, 850


FREQOUT 7, 1, 38500
colordetect = IN1


IF colordetect = 0 THEN
GOSUB Find_Home
ELSE
END
ENDIF

Any help is greatly appreciated,
Brian

Comments

  • ercoerco Posts: 20,256
    edited 2011-11-25 17:52
    Obviously the Stamp can only do one thing at a time, and it will do exactly as you code it. Typically, the problem lies in the difference between what you have told it to do and what you want it to do.

    For us to help you, you must first help us. We need a clearer description (a written flowchart of your intentions) and your well-commented code. How about it? :)
  • brian6592brian6592 Posts: 18
    edited 2011-11-25 18:01
    ok give me a few minutes
  • brian6592brian6592 Posts: 18
    edited 2011-11-25 18:08
    ok I've changed the subroutine a little, but still no results. Here it is with comments, let me know if something is unclear.
    Thanks,
    Brian



    Find_Home:
    PULSOUT 12, 650 'the robot does one pulse in the forward direction
    PULSOUT 13, 850


    GOSUB irdetection 'gets a reading from the ir sensors


    IF (irdetectleft = 0) OR (irdetectright = 0) THEN 'if an infrared sensor detects something, it does an avoidance maneuver (this is derived from the "robotics with the boebot" book
    GOSUB iravoid
    ENDIF


    FREQOUT 7, 1, 38500 'sends a pulse to an ir led
    colordetect = IN1 'gets a reading from the ir detector and stores it as "colordetect"


    IF colordetect = 0 THEN 'if the color reading is white, then the program stops
    end
    ENDIF
    IF colordetect = 1 THEN 'if the color reading is black, then it repeats the subroutine Find_Home
    GOSUB Find_Home
    ENDIF
    RETURN
  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-25 22:04
    We still need a clearer description of what you want to accomplish with your program. Perhaps something other than PBasic (like a prose or flowchart description) would be better.

    One note ... You seem to be using GOSUB like a GOTO and this won't work. When you do a GOSUB, the Stamp saves the return location in a first in last out stack of fixed (limited) size, then does a GOTO to the specified label. In your case, this label is at the beginning of your code which does some stuff, then does a GOSUB again. If it ever does a RETURN, the stack is all messed up and the Stamp will probably reset itself.

    We really need the whole program, just as it is when you've compiled it. Often people post fragments of a program and the problem really is in the portion that they didn't show. Use the Go Advanced button to attach your source file to your next reply rather than just pasting a portion of your program.
Sign In or Register to comment.