Learning gosub

212

Senior Member
Maybe I could write this another way, but I thought this should work???

I have a day/night security camera, and if you have ever seen the images from one, you might remember the colors are not right. The only way I know to correct that is to use a filter exchanger...a little deal that moves an IR cut filter over the camera sensor in the daytime to block the IR light. Well I have one of those on this camera, and I built a little circuit to control it. All it is really, is a Picaxe 08M that turns on one of two relays for a second to switch the filters when it should. Mechanically this works great, and during the test time it works too. It even works in the main....but some time during the night, or daytime, it stops working??? I can remove power from the chip for a second and it will work just fine again.

I don't usually use gosub, but I thought it was time to learn how. If you could please take a quick look at the code, and maybe point out where I messed up, I would be in your debt...thanks in advance.


Code:
low 0   'night relay
low 1   'day relay
high 2  'CDS power
input 4 'CDS
disablebod

'b1       is light level
'b2       tells Picaxe which filter position
 b3 = 10  'test timer in seconds

pause 3000

high 0
pause 1000
low 0
b2 = 1
pause 1000
high 1
pause 1000
low 1
b2 = 0
pause 1000

switch_test:

do while b3 > 0
readadc 4,b1
if b1 =< 200 and b2 = 0 then gosub night_time 	
if b1 > 200 and b2 = 1 then gosub day_time
b3 = b3 - 1
pause 1000
loop
low 2
POKE $1F,0
goto main 


main:

sleep 130
high 2
pause 100
readadc 4,b1
low 2
POKE $1F,0
if b1 =< 200 and b2 = 0 then gosub night_time 	
if b1 > 200 and b2 = 1 then gosub day_time
goto main 



night_time:

high 0
pause 1000
low 0
b2 = 1
b3 = 10
return


day_time:

high 1
pause 1000
low 1
b2 = 0
b3 = 10
return
 

boriz

Senior Member
The first GOTO MAIN serves no function, remove it. Don’t know what POKE $1F does. The GOSUBs look fine to me. It could be that the Picaxe is crashing due to power fluctuations. How to eliminate that possibility?
 

212

Senior Member
Dippy helped me with the POKE $1F a good while back, it turns off the adc to save power..thanks again! The Picaxe is using 4 AAs going through two diodes to reduce the voltage. The same batteries power the exchanger, but two at a time, so I don't think the Picaxe is starved for power at all. "Know or Believe. Choose" OK the Picaxe is not being starved.

If the gosub is OK, then I have no clue. I'll go get it and look for a cold solder joint or something???

And I'll remove the first "GOTO MAIN" too, thanks for looking.
 
Last edited:

lbenson

Senior Member
I can't see an evident problem with your program. If b2 ever got to have a value greater than 1, that would be a condition in which nothing would ever change, but there's no evident way for that to happen.

To help debug, you might want to put in an led and toggle it on each pass through the main loop. Also I would add comments, for example, "sleep 130 ' wait 5 minutes".

To make certain that b2 never has a value other than 1 or 0 you could make it a bit value as follows, and set other symbols to make the program more understandable:
Code:
symbol lightLevel = b1  ' ADC value
symbol day_or_night = bit0 ' 0=day; 1=night (note--bit0 is in b0)
symbol day = 0
symbol night = 1
symbol nightLevel = 205  ' to provide some gap to prevent dithering
symbol dayLevel = 195
symbol CDS_pin = 4
then later:
Code:
readadc CDS_pin,lightLevel  ' get the light level
if lightLevel < dayLevel and day_or_night = day then gosub night_time
if lightLevel > nightLevel and day_or_night = night then gosub day_time
Sorry that this doesn't really address your problem. You might also set it up so that it uses sertxd to log a count and the light level reading to a PC--perhaps seeing how long it runs will give you a clue about what makes it stop.

I assume your relays are latching (or the part they are switching just takes a pulse)? What part number? Can you post a schematic and a photograph (we know you have cameras ;-}).

Finally, in coding, it helps readability if you use indentation to indicate the "block level" of the code. For instance, I use two spaces for indentation (others prefer different spacing), so that labels will be at the far left, the first level of code will be indented two; code within an "if" or "do" statement will be indented 2 more, and so on. This may seem unnecessary, but as your code becomes more complex, this will help make it easier to understand by showing its structure. (And good job to start using gosubs.)
 
Last edited:

212

Senior Member
schematic

My apologies for not commenting the code before posting, I have been doing that after things are proven to work...but this doesn't yet. It takes me longer to make it look right and read right than it does to write it in the first place, but you are right, it still should be done.

The schematic for the board I am using is a slight variation of the one I drew for another project. I only moved the parts around a little, but I changed the code to match too. Here is the one I made originally. The relays are just some 5 volt reeds from Rat Shack (Radio Shack not ebay :)) The exchanger has a coil and the filter is on a magnet, a short pulse is all that is needed to move the filter, then it just stays there till you reverse polarity and pulse it again. Here is a picture of one, but the one inside my camera now has all the control parts stripped off...long story :)

I'm in the middle of about a dozen things right now, so I'll have to straighten up some to get actual pictures...my wife might see them on here and I'd be in big trouble :D
 

Attachments

jglenn

Senior Member
I would say that reed relays might be bad for even small motor loads, they
only like small currents. Is it bidirectional? You have no clamping (diode) or snubber (R+C) across the motor. The transients are probably crashing the chip.

Try a 0.1uF cap in series with a ~10 ohm R across the motor. Or a 1uF instead.
 

212

Senior Member
The original was for controlling the lens cover motor inside a Sony P32 camera. The motor draws only 200ma if I stop it with my finger, so it worked alright. I drilled out the lens cover and installed an IR cut filter in it, so daytime shots would be in full color, despite having removed the IR cut from inside the camera. I put the filter in front of the lens in the day and open the cover to remove it at night. Otherwise the daytime shots are all red, and I don't like that. Since the motor is basically disconnected from anything when the relay opens, I didn't think any clamping (diode) or snubber (R+C) across the motor was needed???

edit...thought it was fixed, but that's what I get for thinking...nope :(
 
Last edited:

boriz

Senior Member
“I don't think the Picaxe is starved for power at all.”

That’s not exactly what I mean. Power fluctuations can be of any type, including spikes of very short duration and high voltage, such as you can expect from any switched inductive load.
 

jglenn

Senior Member
When the relay opens, the contacts are going to arc, transmitting noise into the ground, which..

the PICAXE is connected to. 5V logic only needs blips of 3V to get confused.
Try a separate batt for the motor, use single point grounding, and it will go away. Or try a snubber on the motor. 200mA is a lot for the reed, what is the rating?
 

212

Senior Member
http://www.radioshack.com/product/index.jsp?productId=2062478

I don't actually know what they are rated for, the ad seems to contradict itself...

I will have to disassemble the thing and add the separate power supply and the snubber. Remember this is not running a real motor in this application, but I suppose the coil and magnet create one don't they. I appreciate the help and the explanation so I understand better.

I better fess up first though. I just looked at the little board I have in there, and it has been sitting in a box of junk for a while now, well...I just grounded the serial in and input3 instead of using the resistors like drawn. I think I better fix those two things and try it again, just so I know that isn't my problem. I have had this circuit working fine before, but not using gosub, and not with the grounded pins.

If I don't return for a few days you will know I found the problem, and kicked myself pretty hard for doing that...
 

jglenn

Senior Member
Only try one change at a time, to see the effect. I know the motor seems like
an insignificant load, but it's inductance can cause voltage spikes of maybe 30V.
Hook up a scope across it and see!
 

212

Senior Member
I found the problem about 10 times so far...well not exactly. I changed the code and it worked, then stopped. I put the resistors on the serial in like I was supposed to, and it worked, then stopped. I pulled one of the wires off the battery holder...ha!...it was loose...then it stopped working. You get the idea by now I guess.

I THINK I have a dirty or worn out socket, and the adc reading was going haywire...I'm working my way to finding the real problem, I guess a separate power will be next if this doesn't fix it.

Oh...the only scope I have is on a rifle...I just might use it too...:)
 
Last edited:

inglewoodpete

Senior Member
As an example of what may be necessary to 'calm' the commutator noise on a PIC controlled motor, have a look at the motor in the link. The problem is not specific to PICAXEs. All microcontrollers can be sent into la-la-land by induced electrical noise.

Picture Motor Head The motor in the picture is rated a bit higher than yours but the principles remain the same. This motor is a 12v (1.5A stall current), geared drive, driven by an H-Bridge so that it can be reversed. The capacitors are 0.1uF, in parallel with a 100uF non-polarised electrolytic. Note the ferrite bead on the motor power lead to minimise any transients getting back to the PICAXE controller board.

The so-called 'data' on the reed relay is a bit ordinary (RadioShack website). I'd say that the current limit of the relay would be closer to 0.5A when switching an inductive load. Due to their flimsy nature, reed relays have a habit of welding their contacts together when used near their limit. (Often a flick of the finger is all that is needed to break the weld.)

What is the DC resistance of the the reed relay coil? Remember, the PICAXE can only supply 25mA max on a pin, so a 5v coil must have a resistance > 200 ohms
 
Last edited:

212

Senior Member
The relay coil is 250 ohms, and it uses about 18ma. I breadboarded it again and I think I might have found the problem ...with the code. When I converted it from the original, I think I made a boo-boo lol... I'll know in the morning :)

Either way, I appreciate all the replies, and I will be using the suggestions offered.
 

212

Senior Member
Morning came and went and I still did not have the filter exchanger working. Yes, the code was goofy, and yes the circuit is less than perfect too...I realize that could cause issues. But...I added three little LEDs to the board so I could keep an eyeball on the situation. Because I don't want to get the site shut down, I'll keep my real thoughts to myself and just tell the facts.

The LEDs indicate the circuit is functioning just fine, but the exchanger gets stuck machanically... Thank you for the help, and I usually save everything, but this is going straight into the trash!



.OK, I can't really do that, but it felt good to say it........
 

212

Senior Member
I'll teach it a lesson...I call this new code tick tock, cause that's what is sounds like... :)

low 0 'night relay
low 1 'day relay
low 2 'CDS power

pause 3000

do
high 0
pause 100
low 0

pause 500

high 1
pause 100
low 1

pause 500

loop
 

212

Senior Member
I sure did. I also let it go for a while with the new code too, and it seemed to free up a little. This poor thing has had a tough life too, I may have to pasture it.
 

boriz

Senior Member
I have to laugh. All these clever people with clever electronic and programming suggestions, and it comes down to a simple mechanical jam.
 
Top