exiting loop unexpectedly
leglessman
Posts: 3
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:
any and all help is appreciated!!
Pete
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
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?
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!
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.