PDA

View Full Version : Help with Maxim Ibutton "IF THEN ELSE" statements please



andybarrett1
- 8th May 2014, 08:43
Hi thanks for reading this...

Am attempting to sort out some Ibutton DS1990 security code.... I have this:-

OWOUT iButton,1,[$33]
PAUSE 100
OWIN iButton,0,[Serial[0],Serial[1],Serial[2],Serial[3],Serial[4],Serial[5],Serial[6],Serial[7]]

IF Serial[1]=$15 AND Serial[2]=$D3 AND Serial[3]=$7D AND Serial[4]=$16 AND Serial[5]=$00 AND Serial[6]=$00THEN correct1
IF Serial[1]=$68 AND Serial[2]=$36 AND Serial[3]=$4F AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00THEN correct2
IF Serial[1]=$1E AND Serial[2]=$1A AND Serial[3]=$2C AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00THEN correct3
'else gosub wrong
'endif
GOTO mainloop

This works fine for the correct1,correct2, and correct3 labels.....

What I am attempting to do is; If the code doesn't see one of the three detailed Ibuttons it runs error label "wrong"


I know it must be easy (should be) but not seeing it !

My Picbasic 2.50C

Help welcome.......Please. !!!:biggrin:

EarlyBird2
- 8th May 2014, 09:11
Hi thanks for reading this...

Am attempting to sort out some Ibutton DS1990 security code.... I have this:-

OWOUT iButton,1,[$33]
PAUSE 100
OWIN iButton,0,[Serial[0],Serial[1],Serial[2],Serial[3],Serial[4],Serial[5],Serial[6],Serial[7]]

IF Serial[1]=$15 AND Serial[2]=$D3 AND Serial[3]=$7D AND Serial[4]=$16 AND Serial[5]=$00 AND Serial[6]=$00THEN correct1
IF Serial[1]=$68 AND Serial[2]=$36 AND Serial[3]=$4F AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00THEN correct2
IF Serial[1]=$1E AND Serial[2]=$1A AND Serial[3]=$2C AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00THEN correct3
'else gosub wrong
'endif
GOTO mainloop

This works fine for the correct1,correct2, and correct3 labels.....

What I am attempting to do is; If the code doesn't see one of the three detailed Ibuttons it runs error label "wrong"


I know it must be easy (should be) but not seeing it !

My Picbasic 2.50C

Help welcome.......Please. !!!:biggrin:

Your if correct then labels construction can not be in one line format.

so for example

IF Serial[1]=$15 AND Serial[2]=$D3 AND Serial[3]=$7D AND Serial[4]=$16 AND Serial[5]=$00 AND Serial[6]=$00THEN
correct1
else
gosub wrong
endif

Obviously you need to allow for the three "correct" options.

HenrikOlsson
- 8th May 2014, 09:12
Hi,
Since your IF-THEN statements are actually GOTOs (if evaluated true) all you need to do is add GOSUB (or GOTO) WRONG after the three IF statements.


IF Serial[1]=$15 AND Serial[2]=$D3 AND Serial[3]=$7D AND Serial[4]=$16 AND Serial[5]=$00 AND Serial[6]=$00 THEN correct1 '<----This will GOTO correct1
IF Serial[1]=$68 AND Serial[2]=$36 AND Serial[3]=$4F AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 THEN correct2
IF Serial[1]=$1E AND Serial[2]=$1A AND Serial[3]=$2C AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 THEN correct3
GOSUB WRONG
GOTO mainloop

Now the WRONG subroutine will only execute if none of the three IF statements evalutate true.

/Henrik.

andybarrett1
- 8th May 2014, 09:22
Henrik

You mean :-

Now the WRONG subroutine will only execute if none of the three IF statements evalutate FALSE.

Not near the units so cant try at moment !

Thank you for reading

EarlyBird2
- 8th May 2014, 09:26
Henrik,

That is neat! Although I do not like using goto statements as they can make the program logic difficult to follow.

EarlyBird2
- 8th May 2014, 09:29
Henrik

You mean :-

Now the WRONG subroutine will only execute if none of the three IF statements evalutate FALSE.

Not near the units so cant try at moment !

Thank you for reading

No he means:-

Now the WRONG subroutine will only execute if ALL of the three IF statements evalutate FALSE.

andybarrett1
- 8th May 2014, 10:06
No he means:-

Now the WRONG subroutine will only execute if ALL of the three IF statements evalutate FALSE.

So its like a :-

IF NOT THEN

Sorry for being so dim.....Thank you for help !

EarlyBird2
- 8th May 2014, 10:23
Not what was meant.

All you need to do is change this

else gosub wrong
endif

to this

gosub wrong

in your code

andybarrett1
- 8th May 2014, 10:35
All good....

Will try later and advise

Thank you for help.....

In Sunny Scunthorpe on Monday/Tuesday

Wonderful BOS Plant at Tata !!!

HenrikOlsson
- 8th May 2014, 10:39
Hi,

Now the WRONG subroutine will only execute if none of the three IF statements evalutate FALSE
No, as have been said. If the first condition evalutes TRUE the execution will GOTO correct1 and therefor bypass the GOSUB WRONG statement. Same thing if the second or third condition evalutes TRUE. However, if none of the three conditions evalutes true the GOSUB WRONG will execute. Remember that the way you have your IF statment written it'll issue a GOTO, not GOSUB so make sure that your correct1, 2, 3 routines does not end with a RETURN.


That is neat! Although I do not like using goto statements as they can make the program logic difficult to follow.
I totally agree, GOTOs can lead to some real mess but sometimes it's the easy way.....and there's almost always more than one way to do what one wants.

/Henrik.

EarlyBird2
- 8th May 2014, 10:47
In Scunthorpe to see the BOS plant?

I am amazed but thinking about it I can not think of any other reason to come here. :wink:

Hope you did not travel far!

andybarrett1
- 8th May 2014, 10:57
Laughs...

Yes BOS Plant ..... Told if Really lucky might get to see your 4 Queens of steel making too....

Before dining in style at Deans on Brigg road !

Thanks for help again all

EarlyBird2
- 8th May 2014, 11:02
You saw the four queens!

I bet that made your day.

I assume Deans is a burger van.

You are welcome.

andybarrett1
- 8th May 2014, 12:35
Will play about with it later...

Am understanding more now

Thank you

Dave
- 8th May 2014, 12:41
Steve, I beg to differ about your statement:"Your if correct then labels construction can not be in one line format." Oh really, and what makes you say that? If the statement is TRUE the operation is passed to the address of Correct#. Although it is not the way I would write the compound statement it is 100% acceptable.

andybarrett1
- 8th May 2014, 20:22
Code

mainloop: Owout ibutton,1,[$33]
pause 100
owin ibutton,0,[Serial[0],Serial[1],Serial[2],Serial[3],Serial[4],Serial[5],Serial[6],Serial[7]]

If Serial[1]=$15 and Serial[2]=$D3 and Serial[3]=$7D AND Serial[4]=$16 AND Serial[5]=$00 AND Serial[6]=$00 then correct1
If Serial[1]=$68 and Serial[2]=$36 and Serial[3]=$4F AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 then correct2
If Serial[1]=$1E and Serial[2]=$1A and Serial[3]=$2C AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 then correct3
If Serial[1]=$F0 and Serial[2]=$66 and Serial[3]=$2C AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 then nocache
gosub wrong
Goto mainloop

Hi all...

Tried the above and variants of it....It is now just going staight to the gosub routine (wrong) I suspect because with no Ibutton in place the "Serial Number" is after all wrong!!

Help........!!!!

EarlyBird2
- 8th May 2014, 22:05
Steve, I beg to differ about your statement:"Your if correct then labels construction can not be in one line format." Oh really, and what makes you say that? If the statement is TRUE the operation is passed to the address of Correct#. Although it is not the way I would write the compound statement it is 100% acceptable.

Dave you are correct as Henrik's post has already demonstrated. We both agree that we would not write the compound statement this way. My biggest objection is the use of the 'goto' statement which although perfectly valid is an instruction I was taught not to use a long time ago. I read the "Correct#" as a gosub instruction not a goto statement which was my mistake.

Amoque
- 9th May 2014, 02:06
It is typically necessary to use parenthesis between logical comparisons as:

If (Serial[1]=$15) and (Serial[2]=$D3) and (Serial[3]=$7D) AND (Serial[4]=$16) AND (Serial[5]=$00) AND (Serial[6]=$00) then correct1

I believe that it forces a Boolean comparison rather than a mathematical evaluation.

One last - is it necessary to compare ALL bytes, or might you only compare say... the first one. This would seem to yield a unique condition and save the effort and complexity of so long a line:

If (Serial[1]=$15) then Correct1

Demon
- 9th May 2014, 04:40
...

One last - is it necessary to compare ALL bytes, or might you only compare say... the first one. This would seem to yield a unique condition and save the effort and complexity of so long a line:

If (Serial[1]=$15) then Correct1


Speaking out my bellybutton, I'd say yes. The other serial numbers still have to be validated (knowing absolutely nothing about his application).

Robert
:)

EarlyBird2
- 9th May 2014, 07:11
There is some good code here

http://www.picbasic.co.uk/forum/content.php?r=374-How-to-read-a-one-wire-DS18B20-temperature-sensor-or-nine-of-them

I particularly noted there is no pause between the owout and owin.

HenrikOlsson
- 9th May 2014, 07:14
Hi,

Tried the above and variants of it....It is now just going staight to the gosub routine (wrong) I suspect because with no Ibutton in place the "Serial Number" is after all wrong!!
Well, if what you're saying is that you don't actually have an IButton connected then I think you've found the problem. Of course the numbers will be wrong, none of the IF statements will evaluate TRUE so the exection will fall thru to the GOSUB wrong statement.

Help........!!!!
This may sound like a totally crazy idea but you might try connecting an IButton and see if that possibly helps.

:-)

/Henrik.

andybarrett1
- 9th May 2014, 09:06
Hi Henrick

Yes works fine with button....But!

A lot of the time it needs to sit waiting for a button (looping till it sees one... then it jumps to sub routine if right button) "If" wrong button applied I need to jump out and go elsewhere !

So yes a lot of waiting with no button.

andybarrett1
- 9th May 2014, 09:17
Thank you Steve

Will read though later .... See it reveals any clues !!

BR
Andy

andybarrett1
- 9th May 2014, 09:18
Speaking out my bellybutton, I'd say yes. The other serial numbers still have to be validated (knowing absolutely nothing about his application).

Robert
:)

Yes they do ALL need verifying...

BR
Andy

EarlyBird2
- 9th May 2014, 09:23
Andy,

I get it now!

You are validating ibuttons that are not always present. In fact most of the time there will not be an ibutton to read.

You are saying you need to detect the presence of an ibutton and when detected check it's serial number and if correct take some action (open a lock for example) if incorrect take some other action (sound an alarm for example)

Is this true?

andybarrett1
- 9th May 2014, 09:28
That is the Badger!

Yes.

EarlyBird2
- 9th May 2014, 09:37
Your program needs a loop that checks for the presence of an ibutton and if not present continue looping other wise jump out the loop into a serial number read/verify routine eventualy going back to the loop.

Any ideas of how to test for an ibutton present?

andybarrett1
- 9th May 2014, 09:51
Steve

Could this be the Presence Pulse.. It mentions this in the Datasheet but no command. I Know the 1990R has it the 1990A has not .... I am using a "R"

Read Rom [33h] or [0Fh]
Search [F0h]
Match [55h]
Skip [CCh]

Presence [??h]

Searching around on a phone internet at moment so limited on resources!

BR
Andy

EarlyBird2
- 9th May 2014, 10:26
Could you use the value in S0 as a check. What value does it have with no button and what value with a button? There is also S7 CRC value that could be used in the same way.

You may need to reset S0 on each loop say to $0 then run owout owin and test the new value.

Just ideas nothing is tested :smile:

andybarrett1
- 9th May 2014, 10:40
Update:-

Just found this thread from a whilst back....

http://www.picbasic.co.uk/forum/showthread.php?t=9375

Seems am not the only one with these issues !

EarlyBird2
- 9th May 2014, 10:54
Brilliant there is you answer thanks to Darrel.

Start:
OWOUT IBUTTON, 1, [$33], start ; Detect if OW device is present
.
.
.

andybarrett1
- 9th May 2014, 11:41
mainloop:
Start:
OWOUT IBUTTON, 1, [$33], start ; Detect if OW device is present
Owout ibutton,1,[$33]
pause 100
owin ibutton,0,[Serial[0],Serial[1],Serial[2],Serial[3],Serial[4],Serial[5],Serial[6],Serial[7]]

Edited this from phone so might be dodgy..... Maybe the solution ?

andybarrett1
- 9th May 2014, 21:07
Code


mainloop:
High led
pause 250
low led
pause 250

Owout ibutton,1,[$33],mainloop
pause 100
owin ibutton,0,[Serial[0],Serial[1],Serial[2],Serial[3],Serial[4],Serial[5],Serial[6],Serial[7]]
'If Serial[1]=$15 and Serial[2]=$D3 and Serial[3]=$7D AND Serial[4]=$16 AND Serial[5]=$00 AND Serial[6]=$00 then correct1
'If Serial[1]=$68 and Serial[2]=$36 and Serial[3]=$4F AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 then correct2
'If Serial[1]=$1E and Serial[2]=$1A and Serial[3]=$2C AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 then correct3
'If Serial[1]=$F0 and Serial[2]=$66 and Serial[3]=$2C AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 then nocache
If Serial[7]=$E6 then correct1
If Serial[7]=$90 then nocorrect
gosub wrong
Goto mainloop

We seem to be getting there.... My issue now has changed, I suspect a timing issue.

It loves the "wrong" gosub...if I hold the button on the contacts all good but the slightest slip and I am in the wrong gosub!

I have tried just minimising the code to the CRC of the serial and using that but the same results... putting delays in not helping...

I am hoping it is not the debounce on the contacts.... though we could fix that ??

Any Ideas anyone

Thank you ...Andy

EarlyBird2
- 10th May 2014, 07:42
Change this

Owout ibutton,1,[$33],mainloop
pause 100
owin ibutton,0,[Serial[0],Serial[1],Serial[2],Serial[3],Serial[4],Serial[5],Serial[6],Serial[7]]

to this

Owout ibutton,1,[$33]
owin ibutton,0,[Serial[0],Serial[1],Serial[2],Serial[3],Serial[4],Serial[5],Serial[6],Serial[7]],mainloop

or this

Owout ibutton,1,[$33],mainloop
owin ibutton,0,[Serial[0],Serial[1],Serial[2],Serial[3],Serial[4],Serial[5],Serial[6],Serial[7]],mainloop

EarlyBird2
- 10th May 2014, 07:48
I am curious as to how many ibuttons and "doors" you expect to have in your system.

I have developed an entry system in the past which had many entry points and swipe cards. Hard coding the serial numbers into the program is not the way to go, for all but the smallest systems.

andybarrett1
- 10th May 2014, 09:07
I am curious as to how many ibuttons and "doors" you expect to have in your system.

I have developed an entry system in the past which had many entry points and swipe cards. Hard coding the serial numbers into the program is not the way to go, for all but the smallest systems.

Only three Buttons (One Door or in this case Gate)

The data sheet suggests the pause 100… So that good.
Have already tried the mainloop label on the Owin as well as the Owout !! (I did read the manual…again!)

Also seems to be no difference in operation between DS1990R device and DS1990A Device…. The "R" has the Presence…. But Datasheets are sparse !!!

I am still pondering over timing issues…..I know hardcoding serial numbers is not ideal but it is only small ,,
Having said that I just made the change from 1k to 2k memory..627 to 628 !

Also wondering "IF" I can shorten the VAR ARRAYS any!

Learning a lot here ….. Thank you!

EarlyBird2
- 10th May 2014, 09:42
You could set up a retry loop.

mainloop:
High led
pause 250
low led
pause 250

for I=1 to 5 'or anything you like
Owout ibutton,1,[$33],mainloop
pause 100
owin ibutton,0,[Serial[0],Serial[1],Serial[2],Serial[3],Serial[4],Serial[5],Serial[6],Serial[7]]
If Serial[1]=$15 and Serial[2]=$D3 and Serial[3]=$7D AND Serial[4]=$16 AND Serial[5]=$00 AND Serial[6]=$00 then correct1
If Serial[1]=$68 and Serial[2]=$36 and Serial[3]=$4F AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 then correct2
If Serial[1]=$1E and Serial[2]=$1A and Serial[3]=$2C AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 then correct3
Next i
'If Serial[1]=$F0 and Serial[2]=$66 and Serial[3]=$2C AND Serial[4]=$17 AND Serial[5]=$00 AND Serial[6]=$00 then nocache
gosub wrong
Goto mainloop

change the value of I to test for optimum performance.

andybarrett1
- 10th May 2014, 20:52
Steve

The retry loop seems to have cured the missreading.... If it dont get it the first time it tries 3 more times... I=3
I have also put :- If (Serial[1]=$F0) etc in brackets... Tides things up a bit.:-)

It seems to be working OK now

Thank you again for help....Maybe see you in Scunny on Monday ...!!!

THank you all others who helped..... Great Forum!!

EarlyBird2
- 10th May 2014, 22:29
Andy

You are welcome. If you need any more help just ask.

Cheers