why "and" not work?

hi,
tryig to detect if two pins high but dont seem to work..

start1:
pause 200

if pinC.1 =1 then
serout B.7,N2400,(254,128) ' move to start of first line
serout B.7,N2400,("one in ") ' output text
pause 10
elseif pinC.2 =1 then
serout B.7,N2400,(254,128) ' move to start of first line
serout B.7,N2400,("two in ") ' output text
pause 10
else
serout B.7,N2400,(254,128) ' move to start of first line
serout B.7,N2400,("put plug in ") ' output text
pause 10

endif

goto start1

start2:


if pinC.3 =1 then
serout B.7,N2400,(254,192) ' move to start of first line
serout B.7,N2400,(" pin 3 ") ' output text
pause 10

elseif pinC.5 =1 then
serout B.7,N2400,(254,192) ' move to start of first line
serout B.7,N2400,(" pin 5 ") ' output text
pause 10

elseif pinC.3 =1 and pinC.5 = 1 then
serout B.7,N2400,(254,192) ' move to start of first line
serout B.7,N2400,(" pin 3 pin 5 ") ' output text
pause 10
else
serout B.7,N2400,(254,192) ' move to start of first line
serout B.7,N2400,(" ") ' output text
pause 10
endif

goto start2
 

lbenson

Senior Member
Welcome to the forum.

If you put "if pinC.3 =1 and pinC.5 = 1 then" first, it should work. As it is, if either your first test or your second one in start2 is true, then no subsequent "ELSEIF" will be evaluated.

(If you place posted code within the tags, "[ code]" and "[ /code]" (but without the space after "["), then any indentation you have in your code will be preserved.)

(Note that with no longer pauses, you may not be able to press the two buttons close enough to simultaneously for one not to be evaluated TRUE before the other becomes TRUE.)
 

hippy

Technical Support
Staff member
The main problem is the logic; "pinC.1 = 1" will be true when C.1 by itself is 1 or C.1 and anything else is 1. What you need is something as lbenson describes...

Code:
If pinC.1 = 1 And pinC.2 = 1 Then
  ; Here when C.1 = 1 and C.2 = 1 : Both
Else If pinC.1 = 1 Or pinC.2 = 1 Then
  ; Here when C.1 = 1 or C.2 = 1 : One
Else
  ; Here when C.1 = 0 and C.2 = 0 : None
End If
You seem to be using an M2 and taking advantage of multi-tasking. The problem there is both tasks will run in parallel and will be competing for access to the display.

This will be like two people talking over each other, each saying a sentence then the other. This means that what the LCD hears is not what the two tasks intended, for example ...

You desire for example ...

Start1 => serout B.7,N2400,(254,128)
Start1 => serout B.7,N2400,("one in ")
Start2 => serout B.7,N2400,(254,192)
Start2 => serout B.7,N2400,(" pin 3 ")

But the LCD may end up seeing ...

Start1 => serout B.7,N2400,(254,128)
Start2 => serout B.7,N2400,(254,192)
Start1 => serout B.7,N2400,("one in ")
Start2 => serout B.7,N2400,(" pin 3 ")

And the LCD may end up with "one in pin 3" on the second line.

You would be better off not using multi-tasking, dropping the Start1 and Start2 labels, and having ...

Code:
main:
  ; Code for handling C.1 and C.2
  ; Code for handling C.3 and C.5
  Goto main
 
thanks for info, I will need a few days to digest and play again.
Im only learning, and seems to be many ways to do same task, I spend hours thinking and writing ...I got it then it don't work...life lol
maybe if I explain may help.
my project is a electrical box that checks sets of switches but these are on different cables/plugs. hence displaying the plug/type number by using an input c1,c2 etc
displaying plug number on screen,

then while displaying this actually checking two switches if open or closed and displaying status of both, looping to check any changes



I think what I need
Check for link a or link b pin C1 and C2
Check switch A and B and C – pin C3, C5, C7
display link a,b,c or d and switch A,b,c OPEN or CLOSED


ie link A link B
OPEN – CLOSED OPEN - CLOSED

Or Link A
CLOSED – OPEN

Or link A
CLOSED – CLOSED

Or link A
OPEN - OPEN
 

techElder

Well-known member
kieranallan, the trick to programming is not programming.

The trick is organization.

Organize (in as many non-programming words as you can) on a blank sheet of "paper" what you want to have as a result of your program. Putting that in other words, the end is the beginning of your program.

Then decide what you have available to accomplish that ending, e.g. switches, meters, dials, LCDs, serial connections, etc.

Then decide what limits those two competing items provide, e.g. PICAXE processor pin/leg limits, voltage/battery size, space, environment, etc.

Then you will be ready to start programming.
 
Top