PROBLEM WITH "IF" STATEMENT

sailgene

Member
Hi Group. I have a program set up via relays to change (2) 12-volt batteries wired in parallel to be wired in series. It involves several relays which connect or disconnect the parallel connections and then tie one battery's positive terminal to the other's negative terminal. This temporary connection is needed to run a 24 volt bow thruster. The program works perfectly with an approximate 20 minute time span for automatically turning it off and reconnecting the batteries in parallel.

My goal was/is to have the program watch for a 5 volt input to drop to 0 volts which should then automatically turn off the thruster. This is line 48 in the code. When I turn on a switch, 5 volts is sent to this one Pin.

The 20 minute program works perfectly. But for some reason, if I apply 5 volts to Pin C.5 (18M2 chip), the program does not work. It works in simulation but not in actual practice. I have a 10K pulldown resistor to hold the pin to ground so if the 5 volts are cut off, the pin will be considered "low". I've attached the program and a copy of the PCB. There are several extracurriculars involved to control LED's, a board-mounted Relay and options to connect other inputs.

Any help would be appreciated on why the PINC.5 is a no-go. Thanks!

'PICAXE 18M2+ FOR THRUSTER CONTROL - 13 TO 26 VOLTS VIA 3 RELAYS
'INCLUDES LED CONTROL FOR A DISPLAY MODULE CONNECTED VIA CAT5 WIRE
'SET FOR 20 MINUTES ON AND AUTO OFF AFTER TIME EXPIRES OR C.5 GOES LOW

INIT:
PAUSE 1000
DISCONNECT
SYMBOL BACKUPPWR = C.2
SYMBOL RELAYDISCONNECT = C.0 'RELAY 1 - LOW = ON, HIGH = OFF
SYMBOL RELAYCONNECT = C.1 'RELAY 1 - LOW = ON, HIGH = OFF
SYMBOL RELAYTHRUSTER = C.3 'RELAY 2 - HIGH FOR 24 VOLT CONNECTION FROM MAIN BOARD
SYMBOL BATTSOKAY = C.6 'LED INDICATOR
SYMBOL TWORELAYS = B.7 'LED INDICATOR
SYMBOL TWENTYFOURVOLTS = B.6 'LED INDICATOR
SYMBOL THRUSTERON = B.5 'LED INCIATOR
SYMBOL ERRORLIGHT = B.4 'LED INDICATOR
LET DIRSB = %11110000 'SET B0 - B3 AS INPUTS AND B4 - B7 AS OUTPUTS ("1" = OUTPUT, "0" = INPUT)
LET DIRSC = %01001111 'SET C0 - C3, C6 AS OUTPUTS, C4 & C5, C7 AS INPUTS
HIGH RELAYDISCONNECT
HIGH RELAYCONNECT
HIGH BATTSOKAY, TWORELAYS, TWENTYFOURVOLTS, THRUSTERON, ERRORLIGHT 'TEST DISPLAY LIGHTING - TURN ON
PAUSE 3000
LOW BATTSOKAY, TWORELAYS, TWENTYFOURVOLTS, THRUSTERON, ERRORLIGHT 'TURN OFF ALL DISPLAY LIGHTING
PAUSE 50
HIGH BACKUPPWR 'TURN ON NPN TRANSISTOR TO RELAY 3 FOR TEMP PWR TO PICAXE IF SWITCH OFF
PAUSE 2000
HIGH BATTSOKAY '"BATTS OKAY" - MEANING > 12.5
PAUSE 1000

MAIN:
PAUSE 1000
LOW RELAYDISCONNECT 'DISCONNECTS BOTH RELAYS WHICH HOLD BATTERIES IN PARALLEL
PAUSE 1500 'MAY REQUIRE LONGER PAUSE TO FIND VOLTAGE DIFFERENCE
HIGH RELAYDISCONNECT 'RELAY SHOULD NOW BE UNLATCHED IN CORRECT DISCONNECT "OPEN" POSTION
LET DIRSC = %01001110 'TEMPORARILY SET C.0 AS INPUT TO MAKE SURE IT DOESN'T START UP
PAUSE 2000
HIGH TWORELAYS 'RELAYS 1 - 2 OKAY
HIGH RELAYTHRUSTER 'CONNECT BATERIES IN SERIES FOR 24 VOLTS
PAUSE 2000 'MAY REQUIRE LONGER PAUSE TO FIND VOLTAGE DIFFERENCE

HIGH TWENTYFOURVOLTS 'TURN ON 4TH GREEN LED - "24 VOLTS"
PAUSE 1000
HIGH THRUSTERON 'TURN ON 4TH LED FOR "THRUSTER ON"

THRUSTRUN: '30 SECOND RUN AND THEN TURN STUFF OFF
PAUSE 500
FOR B1 = 1 TO 40
IF PINC.5 = 0 THEN
GOTO THRUSTOFF
ENDIF

PAUSE 30000
NEXT B1

THRUSTOFF:
PAUSE 5000
LOW RELAYTHRUSTER 'TURN OFF RELAY #2 (DISCONNECT 24 VOLT CONNECTION)
PAUSE 2000
LOW THRUSTERON 'B.5
PAUSE 2000
LOW TWENTYFOURVOLTS 'B.6
LOW RELAYCONNECT 'C.0 SWITCH RELAY #2 TO RECONNECT BATT 1 TO BATT 2 TERMINALS
PAUSE 1500
HIGH RELAYCONNECT
PAUSE 500
LOW TWORELAYS 'B.7
PAUSE 2000
LOW BATTSOKAY 'C.6
LET DIRSC = %01001100 'TEMPORARILY SET C.1 AS INPUT TO MAKE SURE IT DOESN'T START UP
PAUSE 2000
LOW BACKUPPWR 'TURN OFF NPN TRANSISTOR TO RELAY WHICH POWERS PICAXE AFTER SWITCH OFF
LET DIRSC = %01001000 'TEMPORARILY SET C.2 AS INPUT TO MAKE SURE IT DOESN'T START UP
PAUSE 2000
STOP
 

Attachments

Last edited:
Are you applying 5V and then leaving it there? Based on that code, the pin will only be checked once during each 30-second loop ... So, just pressing and releasing a button won't do ... you'll need that 5V present then the loop cycles through and the code actually checks that pin.

To get around this, you could use an interrupt ... or, you could make that loop much shorter ...maybe 400 loops at 3 seconds per loop? Or 800 loops at 1.5 seconds ... or 1600 loops at 750ms per loop ... that might give you the performance you're looking for.
 
Yes, I'm applying a constant 5-volt supply so it should see the 5 volts during each loop. The loops are designed as a timer to take approximately 20 minutes before moving to the next routine. Thanks for asking.
 
So when the circuit is activated, a continuous 5 volts is sent to C.5. When the circuit is deactivated, the voltage to C.5 is 0 volts. I cannot simply turn off the program with a single switch because the Picaxe needs to turn off each relay in a particular order - to avoid shorting out the battery bank. To avoid this, a small PCB relay keeps power to the Picaxe until the routines are completed. And so currently, the 20-minute routine works fine by simply moving to the "Thrustoff" routine after the 20-minute loop has completed.

But I want to be able to interrupt this loop earlier by switching off the 5 volts to C.5 so that the program then jumps out of the loop and moves to the "Thrustoff" routine.

Currently, when C.5 is hooked up to the 5-volt input, the program does not execute at all.
 
Been doing a little research and maybe, just maybe, the 5v to C.5 needs to be slightly softened or delayed via a capacitor. Right now, the 5 volts is instantaneous with the "on" switch for the circuit. So I'm going to add a 33uF capacitor combined with a 47k resistor for the 5v power to C.5. I'll check this out in a few days and repost the results. I'm also changing the program to eliminate the 20-minute loop and adding a simple Do loop (if Pinc.5 = 0 then turn off) command. More later.
 
If you don't have a pull-down resistor on the PICAXE pin and the input switch does not actually connect the PICAXE pin to 0v, it probably won't register.
 
Can you post a schematic as it will be easier for us to understand how the code should work?

I get the impression that the picaxe gets fully powered down (although I could be wrong) which means the code needs to start from the beginning every time it gets run.

However I notice your THRUSTOFF routine ends with a STOP, so it's possible the picaxe may be receiving a small amount of phantom power from one of the other inputs via its internal diodes. If this is happening then the code could be permanently stopped rather than re-starting from the beginning.

However you also need to address the issue of the button only being checked every 30 seconds as bpowell pointed out in post#2, although it looks like you are already planning to change this, as mentioned in post#6.
 
Both the schematic of the PCB and the code are shown at the top of this posting. To Inglewoodpete, there is a pulldown resistor (10k) and 5 volts are sent to the Picaxe (pin C.5) when the program is activated. To everyone, the 5 volts remains constant until the switch (not a momentary button) is turned off. At that moment, the 5 volts goes to 0 volts at Pin C.5. When the program runs through the loop and finds 0 volts on C.5, in theory, it should go to the shutdown routine.

But the program does not even run when 5 volts are instantaneously sent to C.5 on startup. So again, I am wondering if the inrush of current to that pin is causing the program to misbehave. And in about a week, I'll get back to trying out the addition of a 33uF Capacitor and 47K Resistor to delay the voltage/current inflow to C.5.

Again, if I leave the C.5 code out of the program and leave that pin disconnected from 5 volts, the program works perfectly, running for about 20 minutes and then automatically shutting down. So I don't believe there is any problem with any phantom power.

Thanks for your input.
 
Have you checked for a short between adjacent pins? I'm guessing a high on C.4(Serial_In) will put the PICAXE in program download mode, or a high on the 0V pin would short the PICAXE power supply and the 7805 would current limit.
 
But the program does not even run when 5 volts are instantaneously sent to C.5 on startup. So again, I am wondering if the inrush of current to that pin is causing the program to misbehave. And in about a week, I'll get back to trying out the addition of a 33uF Capacitor and 47K Resistor to delay the voltage/current inflow to C.5.
Before I say anything else, I don't know what is causing this problem. And 'phamtom' powering of the chip is not a possibility on pin C.5.

What I can say is that the silicon in the 18M2 is a PIC16F1847 and pinC.5 can behave slightly differently to all the other pins when they are configured as an input. The PIC16F1847's leg 4 is designated as RA5/!MCLR/Vpp. Vpp is used to flash the firmware into the PIC and, in the case of the PICAXE, the firmware turns the pin into an Input-Only pin at booup. The specification for Vpp says a maximum of 9 volts is applied to cause the flash memory to be programmed. As a result of this functionality, there is no internal ESD protection diode built into the chip for just that pin (all other I/O pins have both positive and negative ESD diodes). I'm not saying that this has occurred but, if a voltage above 9v is applied to RA5/(C.5) without a current limiting resistor in series, the pin's input circuitry could be damaged. Just something to ponder.

It would be worth writing a short piece of code that reads the logical state of the pin and then lights an LED according to the 0 or 1 state of the pin. That would test the pinC.5 input functionality.
 
Thanks for the input. 5 volts is the maximum voltage that's been applied to C.5. Also, the chip works perfectly when the C.5 input part of the program is left out. But I have definitely not tried testing the pin with simple program which just applied a 5-volt input both immediately on startup and then with a slight delay - and then asking for it to acknowledge the input by taking an action like powering an LED. That's a great idea to see if there is something else wrong.

Otherwise, I'm going to limit the inrush of current/voltage to c.5 on startup to see if that might be interfering with the rest of the programming. Again, I'll let everyone know if any of this works but it'll take a few days for me to check it out - the circuit is on my boat and I can't easily test it without bringing it home. Probably middle of next week I'll have an answer or two.

Again, thank you all for your thoughts and ideas. I love this forum, and I totally love the Picaxe system.

Gene Darby
 
Success. After much toil and trouble, I ended up using a spare pin (C.7) for the input control. I also changed the program from an "if" statement (as to the status of C.5) to a simple Do Loop ("DO WHILE PINC.7 = 1"). The program now works perfectly.

Per the advice from Englewoodpete, I did test C.5 at home with an LED and it worked perfectly. But when I connected on my boat, the Picaxe would not even begin any part of the programming. Also, one of the connected Relays which is activated from Pin C.3 would start beeping. Very strange. Never figured that out.

Anyway, thanks to you all for trying to help solve this. My final thoughts: Leave C.5 alone!
 
Back
Top