Shop OBEX P1 Docs P2 Docs Learn Events
RF_ID Reader project — Parallax Forums

RF_ID Reader project

Rod_NSCCRod_NSCC Posts: 5
edited 2011-11-17 09:34 in BASIC Stamp
I'm trying to create a time clock on a Professional Development Board with a BS2 that will use an RF-ID reader (#28140),4 * 20 LCD display (#27979), and a clock circuit . I have created both circuits so that they work separately however once I combined the code together. The program freezes and will not re-display the clock on the LCD.

The clock is always displayed on the first two lines of the LCD. When a RF-ID is passed by the reader a Tag# will be displayed on the third line of the LCD. Then it should reset only displaying the clock on the LCD. But as I mentioned above the program freezes. Looking for assistance. I think that it is simply my placement of the clock code within the RF-ID program, however I've tried moving the clock code to several different places, and it is still will not work properly.

Regards,

Rod

Comments

  • SRLMSRLM Posts: 5,045
    edited 2011-11-16 09:14
    Your code is very jumpy in your main loop area, and difficult to follow. I added some comments:
    ' -----[ Program Code ]----------------------------------------------------
    
     DO
    	LOW Enable                                   ' activate the reader
    	DO
    		GOSUB Clock
    	LOOP UNTIL (RX = 0)
    	
    	SERIN RX, T2400, [WAIT($0A), STR buf\10]    ' wait for hdr + ID
    	HIGH Enable                                   ' deactivate reader
    
    Check_List:
      FOR tagNum = 1 TO LastTag                     ' scan through known tags
        FOR idx = 7 TO 9                            ' scan bytes in tag
          READ (tagNum - 1 * 10 + idx), char        ' get tag data from table
            IF (char <> buf(idx)) THEN Bad_Char     ' compare tag to table
            										' <--- does this loop actually ever do anything?
    
        NEXT
        GOTO Tag_Found                              ' all bytes match!
    
    Bad_Char:                                       ' try next tag
      NEXT                                          '<--- What would this do? It's not in a loop...
    
    Bad_Tag:
      tagNum = 0
      FREQOUT Spkr, 1000, 115                       ' groan
      GOSUB LCDDisplay
      PAUSE 500
    												<--- Even if you have a bad tag it will still
    												<--- go through tag found
    
    Tag_Found:
    
      FREQOUT Spkr, 1000, 880                       ' beep
      GOSUB LCDDisplay
      PAUSE 500
    
     LOOP
    

    But I would probably rewrite the entire main loop to use a series of GOSUBS and a single loop. Something like this:
    main:
       GOSUB get_tag
       GOSUB compare_tag
       GOSUB display_result
    GOTO main
    
  • Rod_NSCCRod_NSCC Posts: 5
    edited 2011-11-16 10:03
    Thanks for the reply and I've made some of your suggestions. However, when I remove the Bad_Char it causes issues with the cards that it should find in the EEPROM. My main issue is with the clock code. Once I pass the card by the reader I want the clock to remain on the LCD or to simply reappear, after it has cleared the tag number from the screen.
  • SRLMSRLM Posts: 5,045
    edited 2011-11-16 20:16
    But I still don't get how your Check_list function does anything? The inner loop always terminates with no effect on anything while the outer loop terminates when it encounters the GOTO Tag_Found line.

    Basically, it appears that you are trying to use some of the more esoteric allowances of PBASIC, and that makes your code very difficult to debug. I would get rid completely of the label scheme in the "Program Code" section, and put all the important code blocks as subroutines. Once you've done that it will make it much easier to add functionality and debug. Don't forget to always post the most recent code that you have.
  • Rod_NSCCRod_NSCC Posts: 5
    edited 2011-11-17 04:50
    The Check_ list function scans the ID into the buf, then grabs the data from the EEPROM, verify's it, if it does not equal what is found in the EEPROM then it goes to Bad_Char, if it is found then it goes to Tag_Found.

    Changes made to code below:



    Main:

    GOSUB Clock


    LOW Enable ' activate the reader
    IF (RX = 0) THEN
    GOSUB Tag
    ENDIF
    GOSUB Check_List




    GOTO Main
















    '
    [ Subroutines ]
    Tag:


    SERIN RX, T2400, [WAIT($0A), STR buf\10] ' wait for hdr + ID
    HIGH Enable ' deactivate reader
    RETURN


    Check_List:
    FOR tagNum = 1 TO LastTag ' scan through known tags
    FOR idx = 8 TO 9 ' scan bytes in tag
    READ (tagNum - 1 * 10 + idx), char ' get tag data from table
    IF (char <> buf(idx)) THEN GOSUB Bad_Tag ' compare tag to table


    NEXT
    NEXT
    RETURN




    Bad_Char:
    RETURN


    Bad_Tag:
    tagNum = 0
    FREQOUT Spkr, 1000, 215 ' groan
    GOSUB LCDDisplay
    PAUSE 500
    GOTO Main


    Tag_Found:


    FREQOUT Spkr, 1000, 880 ' beep
    GOSUB LCDDisplay
    PAUSE 500
    GOTO Main
  • SRLMSRLM Posts: 5,045
    edited 2011-11-17 09:34
    A side note (something to look up/test) but I don't think you need to have an explicit test on before the SERIN command for RX=0. IIRC the command will hang until serial data transmission is detected.

    Your updated code (post #5) will never get to tag_found... There was a recent discussion on the forum about GOTO, and it's a bit up in the air, but I think your code would be better if you minimized their use. Right now it appears that you're trying many small changes and seeing if that works, then trying something else without correcting the previous changes.

    When I'm debugging and I can't find the problem I find it very helpful to go through and format the code really nicely, and to follow the flow of the program. I start at the beginning and work my way through the entire program, visualizing precisely what the code will do for any particular input. It will help you find small inconsistencies in the code that, by themselves, may not be significant (for example, you GOSUB into Bad_Tag but never return from it...) but never the less make the code harder to understand.

    To maximize the answers that you get here on the forum, please remember these simple rules:
    1. Attach your full code every time.
    2. Explain precisely what the problem is with the code and what you want to do.
    3. Answer any questions that are asked

    With these, it makes our job easier of helping to get the code working.
Sign In or Register to comment.