conditional statements not responding

AlbertZ

Senior Member
When trip1 goes low it initiates the flashers and sets output B.3 high as it should.

When trip2 goes low the flashers continue as they should.

However when trip1 goes high again (simulates a train has passed) the program jumps back to main, even though the conditional statement says that ALL three conditions must be satisfied in order to exit the loop.

Any thoughts on why this is happening and suggested work arounds?

Code:
'===== Inputs =====
symbol trip1 = pinC.0	'initiates crossing gate sequence
symbol trip2 = pinC.1	'sensor before the crossing
symbol trip3 = pinC.2	'sensor after the crossing
'trip1 = 0 when a train is present
'trip1 = 1 when sensor is uncovered.
'same for trip2 and trip3
'Distance between trip2 and trip3 must be
'less than one train length.
'===== Outputs =====
symbol red1 = B.1			'one crossing gate LED
symbol red2 = B.2			'the other crossing LED
symbol gate = B.3			'output to the slave processor
'===Directives===
#com 3                		'specify serial port
#picaxe 14M2            	'specify processor

dirsB = %11111111
dirsC = %00000000

main:
do
	if trip1 = 0 and trip2 = 1 and trip3 = 1 then      'train detected
      exit
	end if
loop  		'loops while waiting for train	
high gate		'activates slave processor

flash:
do while trip2 = 1 or trip3 = 1
	high red1		'flash red lights
	low red2
	pause 100
	low red1
	high red2		
	pause 100
	low red2
	if trip3 = 1 and trip2 = 1 and trip1 = 1 then	'no train
		exit
		end if
loop 
low gate
goto main
 

westaust55

Moderator
Running your program in the Programming Editor V5 simulator (in step mode) and trying all permutations of the three inputs and it appears to be working as you describe/seek.

This suggests that your problem may be hardware related.
 

AlbertZ

Senior Member
I'm running version 6.0.7.5 (BETA). I have not breadboarded this yet so hardware is not the issue. It just won't work on this simulator. I'll check if there are more recent versions of the programming editor.
 

westaust55

Moderator
PE 6.0.8.8 is the latest at this time.

I will look to retry your code in V6 of the PE tomorrow.
PC shut down for the night here.
 

westaust55

Moderator
Looking at the PE6 revision history,
The next version after 6.0.7.5 was 6.0.8.0 which included a fix for case when the IF...THEN had more than 2 AND conditions.
Maybe that was 2 or more AND's

Good to know that your problem is resolved.
 

Skiwi

New Member
Code:
if trip3 = 1 and trip2 = 1 and trip1 = 1 then
I fell into this trap on a project. I had 4 ANDs in a line. Syntax checked OK. After a while I realised it didnt work in practice.
Solution was to nest them. It took more code lines, but at least it works.
 

westaust55

Moderator
I fell into this trap on a project. I had 4 ANDs in a line.
Syntax checked OK. After a while I realised it didnt work in practice.
Solution was to nest them. It took more code lines, but at least it works.

I think you may need to clarify what is not working.

I took AlbertZ's code and modified the lines to:
Code:
symbol trip4 = pinC.3
    	if trip1 = 0 and trip2 = 1 and trip3 = 1 and trip4 = 0 and trip5 = 1then      'train detected
:
:
        if trip3 = 1 and trip2 = 1 and trip1 = 1 and trip4 = 0 and trip5 = 1 then	'no train
downloaded into a 14M2 and testing various combinations:
the code definitely only exited the loop when C.0 AND C.3 were both low AND C.1 and C.2 and C.4 were all high

likewise the flashing phase only ceased when C.3 was low and all others were high.

Therefore to assist Rev Ed/PICAXE in determining any firmware error I would suggest that you:
1. confirm what revision of the PE you are using
2. indicate which PICAXE chip you are/were using
3. post your code
4. explain under what circumstance it is not performing correctly.
 
Last edited:

Technical

Technical Support
Staff member
You should be able to use as many ANDs in a line as you wish. The confusion normally comes when people try to mix AND and OR within the same line, which we don't recommend.
 

hippy

Technical Support
Staff member
It may also be worthwhile refactoring the code to use more explanatory symbol names and even names for the 0 and 1 values to be compared with, and convert the EXIT conditions to be DO-LOOP WHILE or UNTIL conditions. Something like ...

Code:
symbol INITIATE        = pinC.0	'initiates crossing gate sequence
symbol BEFORE_CROSSING = pinC.1	'sensor before the crossing
symbol AFTER_CROSSING  = pinC.2	'sensor after the crossing

symbol PRESENT     = 0 'trip1 = 0 when a train is present
symbol NOT_PRESENT = 1 'trip1 = 1 when sensor is uncovered.

main:
do
  ' nothing
loop until INITIATE = PRESENT and BEFORE_CROSSING = NOT_PRESENT and AFTER_CROSSING = NOT_PRESENT
high gate		'activates slave processor
etc
 

Skiwi

New Member
Not really wanting to turn the original thread into a discussion on my problem. But in answer to westaust55's question, I used 4 AND commands, in a watchdog routine using an 8M2, in a routine monitoring the regular serial output of a 40X2 datalogger. If the COUNT (i.e. changes of state from the serial data) was zero, over 4 cycles (the 4 ANDs), then it reset the main 40X2 datalogger.(pin 1)
As have been suffering occassional lockups of the 40X2, I want to use the 8M2 watchdog to reset it. However, for some reason it didnt always work. i.e. the 40X2 locked up and the Watchdog didnt trigger. On its own, on a breadboard, the watchdog works perfectly.
Back on the bench iI simulated the watchdog action by shorting the serial data line (through buffering resistance). When I changed the 4 ANDs to 4 discrete, nested IF statements, it all worked as expected.
To cut a long story short, maybe I have something else that caused the lack of watchdog action, but I found a fix for me as mentioned. Maybe I need to delve more into the original AND line
And as above, to leave the thread as a discussion on AlbertZ's question, I wont go into my code.

PS
Contradicting my statement on the original thread...The cause of my 40X2 lockups is still a mystery. It sits on a 24V power system that has several switchmode chargers and a sine wave inverter, so there are could be spikes on the dc. My next attempt, is including the DISCONNECT command to see if it may be wandering off and waiting for serial download. I have all sorts of dc blocking on the picaxe board. Not a question in here, just waffling!
 
Top