Shop OBEX P1 Docs P2 Docs Learn Events
exiting loop unexpectedly — Parallax Forums

exiting loop unexpectedly

leglessmanleglessman Posts: 3
edited 2014-03-03 09:21 in BASIC Stamp
Hello all,

I have never posted to any forum before so I hope I'm in the right place.....

I am trying to make a simple device that detects the presents of darkness (then turn a servo, if so) and for some reason, i keep getting removed from my LOOP statement and I don't know why. When darkness is detected by the light sensor (by a value greater than 300 more than the base room light reading) it exits my LOOP statement and I didn't expect it to. Here's what I have and an explanation:
' {$STAMP BS2}
' {$PBASIC 2.5}

currRead VAR Word 'setting variables
baseRead VAR Word

PAUSE 3000 'wait 3 seconds for the user to set us down after power on
HIGH 2 'open io port 2 which is light sensor
RCTIME 2, 1, baseRead 'set the detected light reading to variable
DEBUG HOME, "base light level is ", DEC5 baseRead, CR 'show us the base light reading in the current room

DO 'start loop
  HIGH 2 'send signal to port 2 which is light sensor
  RCTIME 2, 1, currRead 'monitor current light reading
  DEBUG HOME, CR, "current light level is ", DEC5 currRead 'show current light reading

  IF currRead > baseRead +300 THEN 'compare the current reading from the sensor against the base reading we got on power up and check to see if darkness is detected
   PULSOUT 14, 1150 'darkness was detected, now move motor
  ELSE 'darkness was NOT detected. make sure our steering is set to center
   PULSOUT 14, 300 'servo is centered
  ENDIF 'stop looping
LOOP 'go back to DO and do checks again between initial light reading and current reading

any and all help is appreciated!!

Pete

Comments

  • GenetixGenetix Posts: 1,754
    edited 2014-02-28 00:19
    Your program would be lot easier to read if you indented the DO...LOOP and IF...THEN...ELSE statements.

    Adding the parenthesis should solve your problem
    IF currRead > (baseRead + 300) THEN

    Your program jumps from taking a base reading right to your do loop. Shouldn't there be long pause?
    Also your DO...LOOP repeats immediately so should there be a short pause between loops?
  • Mike GreenMike Green Posts: 23,101
    edited 2014-02-28 06:47
    A servo motor requires a control pulse about every 20ms. If these come too often, it may not work properly. If they don't come often enough, the servo will stutter. Both the RCTIME and the DEBUG statements introduce significant delays. The RCTIME's delay is given as the variable value and can be compensated for (delay in ms = 2 * currRead / 1000). The DEBUG statement takes about 1ms per character. Typically you use a PAUSE statement (20 - delay in ms). You'd have to trim down the DEBUG text to keep its contribution to a few milliseconds.
  • Hal AlbachHal Albach Posts: 747
    edited 2014-02-28 06:57
    What is powering the circuit, a 9V battery? If so, the servo is probably pulling the voltage down to where the Stamp resets.
  • leglessmanleglessman Posts: 3
    edited 2014-02-28 17:53
    Thank you! I just tried all of those suggestions and here's what I found:

    I modified it a bit by adding an LED and removing the servo and replacing the code to leave off PULSOUT and added the necessary HIGH and LOW commands and it worked perfectly. It stayed within my LOOP and never exited to get another base reading (which is what I want). I am using a 9 volt battery and even changed out my servo to use a micro servo (which I thought might do the trick regarding voltage) but got the same/original result. I messed with WHILE AND DO loops, but all tests resulted in what appears to be a resetting of the device.(?) I basically want this to be aware of the light and react quickly to changes (like a cockroach). I would like it to be fast. I even messed with the idea of having it move slowly at first, then as your hand approaches it (from any side), it crawl away faster and faster making it uncatchable. It would have a light sensor on each side, and what I'm trying to do so far is just get the steering down. I haven't yet begun entering the code for the motor that will actually propel it.

    knowing that, you can see why I thought that omitting some PAUSE statements and having it loop as quick as possible was a good idea. getting a base reading every time it's triggered is undesired due to the fact that it would think that darkness is normal and would essentially be catchable. If it is gettting reset due to power issues, how can I resolve those?

    I'm just starting out. Thanks in advance to any other ideas that come to mind! Maybe I'm not aware of another programming method that would work better? If you need any more info, just let me know!
  • Hal AlbachHal Albach Posts: 747
    edited 2014-02-28 20:35
    When adding any kind of motor to a Stamp circuit that uses a 9V battery as its power source, the system will quickly fail because of the heavy drain on the battery by the motor. When I was young and dumb I took a 9V battery apart only to find six very small button cells soldered in series. Not much current capacity in those little guys. Bottom line is, as Mike Green and others have said repeatedly in these forums, a separate power source is needed for motors that are activated by the Stamp, and this definitely includes servos.
    You could use a 4-cell battery pack made up of AA or C cells to provide servo power. Run the battery wires to the servo through a toggle switch and be sure to also connect the black (neg) wires with the Stamp common or ground circuit. With a few decoupling capacitors on the servo supply and the unwanted resets should disappear.
  • leglessmanleglessman Posts: 3
    edited 2014-03-03 09:21
    Thanks for the advice. I'll have to wire up a separate power source and play with that.
Sign In or Register to comment.