Help needed with Select Case?


Closed Thread
Results 1 to 9 of 9
  1. #1
    jessey's Avatar
    jessey Guest

    Default Help needed with Select Case?

    Hello Everyone,
    I'm having problems with a Select Case routine that I'm hoping someone can help me understand why I can't get any If-Then-Else-Endif statements to work in it. I just modified the example program (remotext.bas) that came with my LAB-XT Telephony Experimenter's Board so that it will require the caller to enter an access number within a specified time in order to use the remote functions of the answering machine.

    The Select Case below works good but if the caller doesn't enter "8", "9" or "0" then he could try different sequences of the remaining numbers and possibly gain access. I'd like to be able to use "If-Then-Else-Endif" statements so that if any of the remaining numbers are pressed out of sequence then the variable for that number will be set to 2 which would make it impossible to gain access unless the caller hangs up then try's again. I'd like to be able to use this If-Then-Else-Endif below for all the Case statements but for some reason that I can't figure out, it just won't work? I even tried using GOSUB's to use the If-Then-Else-Endif's but still no go. I'm including the whole program in an attachment if that'll help. Any help will be greatly appreciated.

    Thanks
    jessey

    Code:
      Case "1"
       IF (A=0) AND (B=1) AND (C=1) AND (D=1) AND _
           (E=1) AND (F=0) AND (G=1) THEN
         A=1
       ELSE
         A=2
       ENDIF

    This works but I'd like to be able to add If-Then-Else-Endif's
    Code:
    Enter_Access_Code:
     select_dtmf = 1 ' Enable the data output from the MT8870
     Pause 1  ' Pause 1 mS to let data settle
     dtmf_digit = (PORTB >> 4) ' Read the top nibble of PORTB for DTMF data
    
     ' Use LOOKUP to change the DTMF data to an ASCII character
     LookUp dtmf_digit, ["_1234567890*#ABCD"], dtmf_digit
    
    dtmf_wait2:     
     IF dtmf_ready Then dtmf_wait2 ' Loop here until DTMF signal stops
     select_dtmf = 0              ' Disable the MT8870 data output
    
    ' access code is 2457316
    
     Select Case dtmf_digit  ' Take action based on the dtmf digit
    
    '6th # in the correct sequence --- 1 = A
      Case "1"
       IF (A=0) AND (B=1) AND (C=1) AND (D=1) AND _
           (E=1) AND (F=0) AND (G=1) THEN A=1
    
    '1st # in the correct sequence --- 2 = B
      Case "2"   
       IF (A=0) AND (B=0) AND (C=0) AND (D=0) AND _
           (E=0) AND (F=0) AND (G=0) THEN B=1
    
    '5th # in the correct sequence --- 3 = C 
      Case "3"  
       IF (A=0) AND (B=1) AND (C=0) AND (D=1) AND _
           (E=1) AND (F=0) AND (G=1) THEN C=1
    
    '2nd # in the correct sequence --- 4 = D
      Case "4"
       IF (A=0) AND (B=1) AND (C=0) AND (D=0) AND _
           (E=0) AND (F=0) AND (G=0) THEN D=1
    
    '3rd # in the correct sequence --- 5 = E
      Case "5"
       IF (A=0) AND (B=1) AND (C=0) AND (D=1) AND _
           (E=0) AND (F=0) AND (G=0) THEN E=1
    
    '7th # in the correct sequence --- 6 = F   
      Case "6"
       IF (A=1) AND (B=1) AND (C=1) AND (D=1) AND _
           (E=1) AND (F=0) AND (G=1) THEN F=1
    
    '4th # in the correct sequence --- 7 = G   
      Case "7"
       IF (A=0) AND (B=1) AND (C=0) AND (D=1) AND _
           (E=1) AND (F=0) AND (G=0) THEN G=1
    
      Case "8"
        A=2   ' If the #8 key is pressed then it's a no go
    
      Case "9"
        A=2   ' If the #9 key is pressed then it's a no go
    
      Case "0"
        A=2   ' If the #0 key is pressed then it's  a no go
    
     End Select
    
     IF (A=1) AND (B=1) AND (C=1) AND (D=1) AND _
         (E=1) AND (F=1) AND (G=1) THEN
       access_code = is_verified  ' remote functions will work now 
       FreqOut DTMF_out, 500, 800 ' Play 3 tones to confirm to the caller that
       PAUSE 100                  ' the correct access code has been entered
       FreqOut DTMF_out, 500, 800
       PAUSE 100
       FreqOut DTMF_out, 500, 800
     ENDIF
    IF k = 0 THEN listen
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Chances are that you have more AND's than the compiler can keep track of. Even if your numerous AND's did work, your code would compile smaller if you use this technique:
    IF (A=0) then
    if (B=1) then
    if (C=1) then
    if (D=1) then
    if (E=1) then
    if (F=0) then
    if (G=1) THEN
    A=1
    endif
    endif
    endif...repeat as needed...
    ELSE
    A=2
    ENDIF

  3. #3
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Thanks tenaja

    Hi tenaja,

    I'll try your suggestion...

    Thanks
    jessey

  4. #4
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Hello tenaja,

    I tried your suggestion and still no go. Can you or anyone else here think of any other avenues that I could explore to get it working?

    Thanks
    jessey

    Code:
    Enter_Access_Code:
     select_dtmf = 1   ' Enable the data output from the MT8870
     Pause 1     ' Pause 1 mS to let data settle
     dtmf_digit = (PORTB >> 4) ' Read the top nibble of PORTB for DTMF data
    
     ' Use LOOKUP to change the DTMF data to an ASCII character
     LookUp dtmf_digit, ["_1234567890*#ABCD"], dtmf_digit
    
    dtmf_wait2:     
     IF dtmf_ready Then dtmf_wait2 ' Loop here until DTMF signal stops
     select_dtmf = 0   ' Disable the MT8870 data output
    
    ' access code is 2457316
    
     Select Case dtmf_digit       ' Take action based on the dtmf digit
    
    '6th # in the correct sequence --- 1 = A
      Case "1"    ' sets A var to 1 if 1 is pressed and If-Then below is correct..
     'IF (A=0) AND (B=1) AND (C=1) AND (D=1) AND (E=1) AND (F=0) AND (G=1) THEN A=1
      IF (A=0) then
       if (B=1) then
        if (C=1) then
         if (D=1) then
          if (E=1) then
           if (F=0) then
            if (G=1) THEN
      A=1
       endif
        endif
         endif
          endif
           endif
            endif
            ELSE
        A=2 ' 2681
      ENDIF ' 2639
            '   52
    
    
    '1st # in the correct sequence --- 2 = B
      Case "2"    ' sets B var to 1 if 2 is pressed and If-Then below is correct..
     'IF (A=0) AND (B=0) AND (C=0) AND (D=0) AND (E=0) AND (F=0) AND (G=0) THEN B=1
       IF (A=0) then
        if (B=0) then
         if (C=0) then
          if (D=0) then
           if (E=0) then
            if (F=0) then
             if (G=0) THEN
       B=1
        endif
         endif
          endif
           endif
            endif
             endif
             ELSE
         B=2
       ENDIF
    
    '5th # in the correct sequence --- 3 = C 
      Case "3"    ' sets C var to 1 if 3 is pressed and If-Then below is correct..
      'IF (A=0) AND (B=1) AND (C=0) AND (D=1) AND (E=1) AND (F=0) AND (G=1) THEN C=1
       IF (A=0) then
        if (B=1) then
         if (C=0) then
          if (D=1) then
           if (E=1) then
            if (F=0) then
             if (G=1) THEN
       C=1
        endif
         endif
          endif
           endif
            endif
             endif
             ELSE
         C=2
       ENDIF
    
    '2nd # in the correct sequence --- 4 = D
      Case "4"    ' sets D var to 1 if 4 is pressed and If-Then below is correct..
      'IF (A=0) AND (B=1) AND (C=0) AND (D=0) AND (E=0) AND (F=0) AND (G=0) THEN D=1
       IF (A=0) then
        if (B=1) then
         if (C=0) then
          if (D=0) then
           if (E=0) then
            if (F=0) then
             if (G=0) THEN
       D=1
        endif
         endif
          endif
           endif
            endif
             endif
             ELSE
         D=2
       ENDIF
    
    '3rd # in the correct sequence --- 5 = E
      Case "5"    ' sets E var to 1 if 5 is pressed and If-Then below is correct..
      'IF (A=0) AND (B=1) AND (C=0) AND (D=1) AND (E=0) AND (F=0) AND (G=0) THEN E=1
       IF (A=0) then
        if (B=1) then
         if (C=0) then
          if (D=1) then
           if (E=0) then
            if (F=0) then
             if (G=0) THEN
       E=1
        endif
         endif
          endif
           endif
            endif
             endif
             ELSE
         E=2
       ENDIF
    
    '7th # in the correct sequence --- 6 = F   
      Case "6"    ' sets F var to 1 if 6 is pressed and If-Then below is correct..
      'IF (A=1) AND (B=1) AND (C=1) AND (D=1) AND (E=1) AND (F=0) AND (G=1) THEN F=1
       IF (A=1) then
        if (B=1) then
         if (C=1) then
          if (D=1) then
           if (E=1) then
            if (F=0) then
             if (G=1) THEN
       F=1
        endif
         endif
          endif
           endif
            endif
             endif
             ELSE
         F=2
       ENDIF
    
    '4th # in the correct sequence --- 7 = G   
      Case "7"    ' sets G var to 1 if 6 is pressed and If-Then below is correct..
      'IF (A=0) AND (B=1) AND (C=0) AND (D=1) AND (E=1) AND (F=0) AND (G=0) THEN G=1
       IF (A=0) then
        if (B=1) then
         if (C=0) then
          if (D=1) then
           if (E=1) then
            if (F=0) then
             if (G=0) THEN
       G=1
        endif
         endif
          endif
           endif
            endif
             endif
             ELSE
         G=2
       ENDIF
    
      Case "8"
        A=2   ' If the #8 key is pressed then it's a no go
    
      Case "9"
        A=2   ' If the #9 key is pressed then it's a no go
    
      Case "0"
        A=2   ' If the #0 key is pressed then it's  a no go
    
     End Select
    
    'IF (A=1) AND (B=1) AND (C=1) AND (D=1) AND (E=1) AND (F=1) AND (G=1) THEN
     IF (A=1) then
      if (B=1) then
       if (C=1) then
        if (D=1) then
         if (E=1) then
          if (F=1) then
           if (G=1) THEN
       access_code = is_verified 
       FreqOut DTMF_out, 500, 800  ' Play 3 tones to confirm to the caller that
       PAUSE 100                   ' the correct access code has been entered
       FreqOut DTMF_out, 500, 800
       PAUSE 100
       FreqOut DTMF_out, 500, 800
      endif
       endif
        endif
         endif
          endif
           endif
     ENDIF
    IF k = 0 THEN listen

  5. #5
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    Change this type of code to
    Code:
     Case "1"		  ' sets A var to 1 if 1 is pressed and If-Then below is correct..
     IF (A=0) AND (B=1) AND (C=1) AND (D=1) AND (E=1) AND (F=0) AND (G=1) THEN A=1
    
    '1st # in the correct sequence --- 2 = B
     Case "2"		  ' sets B var to 1 if 2 is pressed and If-Then below is correct..
     IF (A=0) AND (B=0) AND (C=0) AND (D=0) AND (E=0) AND (F=0) AND (G=0) THEN B=1
    to this type of code throughout the case structures.
    Code:
     Case "1"		  ' sets A var to 1 if 1 is pressed and If-Then below is correct..
     IF (A=0) AND (B=1) AND (C=1) AND (D=1) AND (E=1) AND (F=0) AND (G=1) THEN
          A=1
     endif
    
    '1st # in the correct sequence --- 2 = B
     Case "2"		  ' sets B var to 1 if 2 is pressed and If-Then below is correct..
     IF (A=0) AND (B=0) AND (C=0) AND (D=0) AND (E=0) AND (F=0) AND (G=0) THEN
         B=1
     endif

  6. #6
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Two things to try... first, don't convert your dtmf_digit value to a string (ascii) value. (And if you must, then use a second variable to put the result in.) If you did this just to make the code more readable, then use constants in your CASE's. This, at the least, will allow you to eliminate a code-hungry command.

    Second, if those vars are all bits (and it does look like it) then put them all in one byte, and compare byte values. So, instead of seven compares, you only have one that's efficient and easy to read ... and you can use constants for them, too...
    Code:
    Case 1   'note it's a byte, not a string
        if bByte_Values = %00111101 then
          bByte_Values.6 = 1
        endif 
    Case cdtmf_value_2    'note the constant.
        if bByte_Values = 0 then  'can replace BV compare value with constant like cBV_dtmf_2
          bByte_Values.1 = 1
        endif          
    'etc...

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hiya Jessey.

    Combination recognition?
    Seems like I've been down this road before.

    Well, ... I'm not going to try to fix what you have.
    I'd suggest highlighting the entire area, and press Delete.
    Much easier ways to go about it.

    Although the text of this post only partially applies to your case,
    I think the code example is more like what you want.
    https://www.picbasic.co.uk/forum/showthread.php?p=32566

    There are several other examples in that thread too.

    HTH,
    DT

  8. #8
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Thanks

    Happy New Year everyone!

    Thanks so much tenaja, Jerson & Darrel

    Hi Darrel,

    Your code sure looks good, I can't wait to try it out. I'm at a friends place visiting for a while so I can't try it out until I get home but it'll be the first thing on my to do list.....

    Thanks Again
    jessey


    Code:
    combocount VAR BYTE
    
    Enter_Access_Code:
    	select_dtmf = 1	          ' Enable the data output from the MT8870
    	Pause 1		          ' Pause 1 mS to let data settle
    	dtmf_digit = (PORTB >> 4) ' Read the top nibble of PORTB for DTMF data
    
    	' Use LOOKUP to change the DTMF data to an ASCII character
    	LookUp dtmf_digit, ["_1234567890*#ABCD"], dtmf_digit
    
    dtmf_wait2:					
    	IF dtmf_ready Then dtmf_wait2	' Loop here until DTMF signal stops
    	select_dtmf = 0			' Disable the MT8870 data output
    
    ' access code is 2457316
    
        IF (combocount = 0) AND (dtmf_digit = 2) THEN GoodKey
        IF (combocount = 1) AND (dtmf_digit = 4) THEN GoodKey
        IF (combocount = 2) AND (dtmf_digit = 5) THEN GoodKey
        IF (combocount = 3) AND (dtmf_digit = 7) THEN GoodKey
        IF (combocount = 4) AND (dtmf_digit = 3) THEN GoodKey
        IF (combocount = 5) AND (dtmf_digit = 1) THEN GoodKey
        IF (combocount = 6) AND (dtmf_digit = 6) THEN GoodKey
        combocount = 0     'invalid keypress
    IF k = 0 THEN listen
    
    GoodKey:
        combocount = combocount + 1
     IF combocount = 6 THEN
        access_code = is_verified 
        FreqOut DTMF_out, 500, 800  ' Play 3 tones to confirm to the caller that
        PAUSE 100                   ' the correct access code has been entered
        FreqOut DTMF_out, 500, 800
        PAUSE 100
        FreqOut DTMF_out, 500, 800
     ENDIF
    IF k = 0 THEN listen

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Thumbs up

    There ya go.
    With no PIC or compiler in front of you .... Pretty good!

    Just a couple comments for when you get home.

    Make sure the program can't accidently "Fall" into the GoodKey routine.
    If it does fall-in, it was a bad key, so you definately don't want to run the GoodKey routine.

    And the example has seven digits.

    IF combocount = 7 THEN have a

    Happy New Year!
    DT

Similar Threads

  1. Using Nokia LCD
    By BobP in forum mel PIC BASIC Pro
    Replies: 300
    Last Post: - 3rd May 2018, 04:47
  2. Sony SIRC IR Issue
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th August 2015, 08:10
  3. Write Onewire data toa I2C memory / read ASCI
    By Eugeniu in forum mel PIC BASIC Pro
    Replies: 67
    Last Post: - 16th November 2008, 19:19
  4. Crystalfontz LCD
    By jman12 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 9th February 2007, 15:04
  5. Interrupt/timer not really interrupting...
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd May 2005, 22:05

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts