A simple but infuriating symbolic naming problem.

lauriet

Member
I have two inputs, C.1 and C.2

The code:
if pinC.1 = 1 then ...
and
If pinC.2 = 1 then ...
functions as expected.
I can declare symbols for them:
If switch1 = 1 then ...
and
If switch2 = 1 then ...
and everything is fine.
I want to run some common code that uses one or other of the switches, so might say 'Detector= pinC.1' or 'Detector= pinC.2' in the body of the code and then
'If Detector = 1 then ...'
This passes syntax, but does not work. With my current knowledge I cannot see how to resolve this; I'm sure it can.
Please advise gently.
 

Technical

Technical Support
Staff member
as long as you have defined detector as a variable

symbol detector = b0

this process will work fine.
 

Technical

Technical Support
Staff member
I want to run some common code that uses one or other of the switches, so might say 'Detector= pinC.1' or 'Detector= pinC.2' in the body of the code and then
'If Detector = 1 then ...'
Remember if you do these two lines

detector = pinC.1
detector = pinC.2

then detector will ONLY have the value of pinC.2 in it.
 

techElder

Well-known member
... and another thing ...

Why define a SYMBOL for C.1 and C.2 if you aren't going to use the SYMBOL in your programming?
 

hippy

Ex-Staff (retired)
I want to run some common code that uses one or other of the switches, so might say 'Detector= pinC.1' or 'Detector= pinC.2' in the body of the code and then
'If Detector = 1 then ...'
This passes syntax, but does not work.
Perhaps post your code so readers can review what you actually have which does not work.

Could it be that you are not updating your "Detector" variable before testing it, were expecting it to automatically track the input pin value ?
 

lauriet

Member
Hippy, as ever I think you have focussed on the problem.
I have (in extract):

Symbol Detector = b1
...
Detector = pinC.4
...
if Detector = 1 then
high RedLed
endif
...
Detector = pinC.5
...
If Detector = 1 then
high GreenLed
endif
... etc.

I can see that 'Detector' might not read the state of the relevant pin when called (as using its equivalent pin would), I just cannot see how to make it do so.
('Detector' is assigned as part of separate configuration sub-routines that are chosen by the state of another input, so is unpredictable)

If necessary, I will post more of the code, but I think I've captured the nub here.
 

Technical

Technical Support
Staff member
The difference is that the input is only checked when you use 'detector =', which is therefore at a completely different time than if you use 'if pinC.1 =' directly in the if statement.
 

lauriet

Member
Thanks; I can understand that it won't do what I want.
But is there any strategy I can use so it will, i.e. forcing 'detector' to read the state of the input just before the 'if' statement when I don't know what pin it is assigned to, but the code does?

Or a completely different strategy?
 

hippy

Ex-Staff (retired)
There is no automatic way to have "when I use this variable I want it to be the value read from a particular input pin" but it is possible to pass information in a variable indicating which pin is to be read or used ...

Code:
Symbol pinToRead = b10
Symbol result    = b11

Do
  pinToRead = C.0 : Gosub ReadPin : SerTxd( "C.0=", #result, CR, LF )
  pinToRead = C.1 : Gosub ReadPin : SerTxd( "C.1=", #result, CR, LF )
  pinToRead = B.5 : Gosub ReadPin : SerTxd( "B.5=", #result, CR, LF )
Loop

ReadPin:
  Select Case pinToRead
    Case C.0 : result = pinC.0
    Case C.1 : result = pinC.1
    Case B.5 : result = pinB.5
  End Select
  Return
So for your own example you could use ...

Code:
Symbol DetectorPin = b0
Symbol Detector    = b1
...
DetectorPin = C.4
...
Gosub ReadDetectorPin
if Detector = 1 then
  high RedLed
endif
...
DetectorPin = C.5
...
Gosub ReadDetectorPin
If Detector = 1 then
  high GreenLed
endif
...

ReadDetectorPin:
  Select Case DetectorPin
    Case C.4 : Detector = pinC.4
    Case C.5 : Detector = pinC.5
  End Select
  Return
An alternative to using a SELECT-CASE is to use a LOOKDOWN and LOOKUP ...

Code:
ReadPin:
  LookDown pinToRead, (    C.0,    C.1,    B.5 ), result 
  LookUp   result,    ( pinC.0, pinC.1, pinB.5 ), result 
  Return
 

lauriet

Member
Many thanks to all who helped.

I realised it was my silliness in not realising the significance of the 'pin' prefix reading the state of the pin rather than the pin designation.
So my final code was:

Symbol Detector = b1
...
(subroutines set Detector to C.4 or C.5)
...
if Detector = C.4 then
if pinC.4 = 0 then
high RedLed
endif
elseif Detector = C.5 then
if pinC.5 = 0 then
high GreenLed
endif
endif

Like so many things, it's obvious in retrospect.
I'll get the hang of life and coding soon.
Cheers,
Lauriet.
 

lauriet

Member
Doh!
in case you notice, it should be 'if pinC.4 = 1 ...' in my example code.
(I'm using output low in my real code (so it also detects if the switch circuit fails))
 
Top