Shop OBEX P1 Docs P2 Docs Learn Events
serout str array\7 doesn't behave as expected — Parallax Forums

serout str array\7 doesn't behave as expected

blittleblittle Posts: 19
edited 2007-09-30 21:26 in BASIC Stamp
This is just a simple project with a 4x4 keypad, 2x16 serial lcd, and an inductive proximity sensor to count metal pieces.
The keypad key values are stored in a byte array, and I want to send the string to the lcd.
I'm not sure what the lcd is seeing (hex?) but it doesn't work as-is.
What am I doing wrong please?
Bill
update: oops forgot: the problem part is at line 65.

' {$STAMP BS2}
' {$PBASIC 2.5}

db       VAR  Bit              ' keypad debounce bit
press    VAR  Bit              ' Flag to indicate keypress.
sdb      VAR  Bit              ' sensor debounce bit
schanged VAR  Bit              ' sensor trigger flag for display update
scount   VAR  Word             ' Sensor count
mo       VAR  Byte(8)          ' manufacturing order number
moidx    VAR  Nib              ' mo array index
key      VAR  Nib              ' Key number 0-15.
row      VAR  Nib              ' Counter used in scanning keys.
i        VAR  Nib              ' generic counter var
cols     VAR  IND              ' Input states of pins P4-P7.
time       VAR  Word
TX              PIN     0               ' serial output to LCD
baud            VAR     Word            ' baud rate for Serial LCD
dark            CON     50              ' rctime high value to turn on backlight
T19K2           CON     32              ' con for baud var
LcdCls          CON     $0C             ' clear LCD (use PAUSE 5 after)
LcdCr           CON     $0D             ' carriage return
LcdBLon         CON     $11             ' backlight on
LcdBLoff        CON     $12             ' backlight off
LcdOn1          CON     $16             ' LCD on; cursor off, blink off


HIGH TX                               ' setup TX pin for LCD
PAUSE 100                             ' allow LCD to intialize
baud = T19K2
scount = 0                            ' zero the counter
key = 0                               ' zero the key buffer
moidx = 0                             ' zero the mo index

SEROUT TX, baud, [noparse][[/noparse]LcdOn1, LcdCls, LcdBlOn]     ' initialize lcd
PAUSE 5
SEROUT TX, baud, [noparse][[/noparse]"MO ", LcdCr, DEC scount]    ' initialize display

main:
   HIGH 2
   PAUSE 100
   RCTIME 2, 1, time
   IF (time > dark) THEN
    SEROUT TX, baud, [noparse][[/noparse]LcdBlOn]
   ELSE
    SEROUT TX, baud, [noparse][[/noparse]LcdBlOff]
   ENDIF

   GOSUB checkSensor
   IF schanged = 1 THEN
    SEROUT TX, baud, [noparse][[/noparse]LcdCls]
    PAUSE 5
    'SEROUT TX, baud, [noparse][[/noparse]"MO ", STR mo\7, LcdCr, DEC scount]
    SEROUT TX, baud, [noparse][[/noparse]"MO ", LcdCr, DEC scount]
    schanged = 0
   ENDIF

   GOSUB keyScan
   IF press = 0 THEN main
   mo(moidx) = key
   IF moidx < 7 THEN
    moidx = moidx + 1
   ENDIF
   SEROUT TX, baud, [noparse][[/noparse]LcdCls]
   PAUSE 5
   SEROUT TX, baud, [noparse][[/noparse]"MO ", STR mo\7, LcdCr, DEC scount]
   FOR i = 0 TO 7
     DEBUG "-", DEC mo(i)
   NEXT
   DEBUG CR
   'DEBUG "MO ", STR DEC mo\7, CR
   press = 0

GOTO main


keyScan:
  FOR row = 8 TO 11
    LOW row
    key = ~cols
    key = NCD key
    IF key <> 0 THEN push
    INPUT row
  NEXT
  db = 0
RETURN

push:
  IF db = 1 THEN done
  db = 1: press = 1
  key = (key-1)+(row*4)
  LOOKUP key,[noparse][[/noparse]1,2,3,10,4,5,6,11,7,8,9,12,14,0,15,13],key

done:
  INPUT row
RETURN

checkSensor:
  IF IN1 = 1 THEN
    IF sdb = 1 THEN RETURN
    sdb = 1
    schanged = 1
    scount = scount+1
  ELSE
    'sensor went low so we can reset debounce
    sdb = 0
  ENDIF
RETURN




p.s. Should the code tag on the forum preserve the code formatting? i.e. fixed width

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-09-29 19:47
    Hi blittle, try with the values of the lookup table in quotes eg: LOOKUP key,[noparse][[/noparse]"1","2", etc.

    Jeff T.
  • blittleblittle Posts: 19
    edited 2007-09-29 20:02
    I don't think that's quite what I'm looking for.
    I tried it anyway and still got the lcd junk and the debug keys where wrong somehow.
    Thanks for the suggestion though.
    Bill
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-09-29 20:17
    Hi blittle, I believe the lookup values still need to be enclosed in quotes so you have a problem in the way you are decoding the keypad. Is it possible to run a small routine containing just the keypad routine and see what values the LCD displays. Do you have a data sheet for the keypad, is it a Parallax keypad.

    Jeff T.

    EDIT I also notice you have cols described as IND P4 - P7 when P4 - P7 is actually INB


    Post Edited (Unsoundcode) : 9/29/2007 8:36:29 PM GMT
  • blittleblittle Posts: 19
    edited 2007-09-29 20:41
    It's the Parallax 4x4 keypad here:
    http://www.parallax.com/detail.asp?product_id=27944
    I used the example code from listing 22.1 in this Nuts and Volts article mostly:
    http://www.parallax.com/dl/docs/cols/nv/vol1/col/nv22.pdf

    Yeah, I noticed the comment error after IND after I posted it. [noparse]:)[/noparse]

    It would have been helpful of me to add that the keypad code works fine if I just send key to the lcd.
    The problem is trying to stick key into the mo array and then send the array to the lcd.
    The lcd becomes corrupted.

    Bill

    Post Edited (blittle) : 9/29/2007 9:05:33 PM GMT
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-09-29 21:07
    I was wondering if we could catch the first digit from the keypad, I added 3 lines in your code to see if we are catching the first digit after the text TEST

    IF moidx < 7 THEN
    ··· moidx = moidx + 1
    ·· ENDIF
    ·· SEROUT TX, baud, [noparse][[/noparse]LcdCls]
    ·· PAUSE 5
    SEROUT TX, baud, [noparse][[/noparse]128]
    PAUSE 5
    SEROUT TX, baud, [noparse][[/noparse]"TEST ", mo(0)]
    PAUSE 10000


    what do you get if you run the above

    Jeff T
  • blittleblittle Posts: 19
    edited 2007-09-29 21:13
    The LCD display reads "TEST ~" without quote marks.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-09-29 21:26
    Your variable row will always be at least 8. So (key-1)+(row*4) will be a bigger number than your LOOKUP table can handle, which will leave key unchanged.

    -Phil
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-09-29 21:27
    Ok the tilde is LCD custom character 0 meaning mo(0) was passed the number zero. Which number did you press during the test?

    Just to clear it up in my mind you do have P8 to P15 connected to the keypad·and quotes around the lookup values yes?

    Jeff T.


    EDIT nice catch Phil.....the example code uses 0 to 3 for rows keeping it with bounds

    Post Edited (Unsoundcode) : 9/29/2007 9:33:25 PM GMT
  • blittleblittle Posts: 19
    edited 2007-09-29 21:53
    Phil, I'm not sure about your comment on key not updating, since it works. "DEBUG DEC key" shows the correct number values.

    I pressed number 1 for the test.
    Yep, the keypad is on P8-P15.

    I do not have the lookup values quoted because I get the wrong values if I do.
    1,2,3 return correct. A returns 1, B returns 6, C returns 8, D returns 1, 5 returns 44, etc.
    If I leave them unquoted, the debug in the FOR block visualy returns the array as I would expect.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-09-29 22:10
    Yes the DEC formatter in the FOR loop will display the characters correctly in the debug window but for the LCD you need the quotes.·The reason you get a value in the key variable is its the lookup index, the variable is never assigned a value from within the lookup table.

    Testing what Phil is saying would be real quick. Add a NIB variable ·and call it TEST and add the following code to see what happens.

    ·FOR·row·=·8·TO·11

    TEST=row-8· 'add this line

    then adjust

    key·=·(key-1)+(row*4) to read key·=·(key-1)+(TEST*4)

    Jeff T
  • blittleblittle Posts: 19
    edited 2007-09-29 22:23
    Ok. Here's what I tested last, and the results.
    I pressed 1,2,3,4,A,4,5,6,B in order and got this in the debug window:
    -1-0-0-0-0-0-0-01
    -1-2-0-0-0-0-0-02
    -1-2-3-0-0-0-0-03
    -1-2-3-1-0-0-0-01
    -1-2-3-1-0-0-0-00
    -1-2-3-1-0-4-0-04
    -1-2-3-1-0-4-5-05
    -1-2-3-1-0-4-5-66
    The LCD shows garbage.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    db       VAR  Bit              ' keypad debounce bit
    press    VAR  Bit              ' Flag to indicate keypress.
    sdb      VAR  Bit              ' sensor debounce bit
    schanged VAR  Bit              ' sensor trigger flag for display update
    scount   VAR  Word             ' Sensor count
    mo       VAR  Byte(8)          ' manufacturing order number
    moidx    VAR  Nib              ' mo array index
    key      VAR  Nib              ' Key number 0-15.
    row      VAR  Nib              ' Counter used in scanning keys.
    i        VAR  Nib              ' generic counter var
    cols     VAR  IND              ' Input states of pins P8-P11.
    time       VAR  Word
    TX              PIN     0               ' serial output to LCD
    baud            VAR     Word            ' baud rate for Serial LCD
    dark            CON     50              ' rctime high value to turn on backlight
    T19K2           CON     32              ' con for baud var
    LcdCls          CON     $0C             ' clear LCD (use PAUSE 5 after)
    LcdCr           CON     $0D             ' carriage return
    LcdBLon         CON     $11             ' backlight on
    LcdBLoff        CON     $12             ' backlight off
    LcdOn1          CON     $16             ' LCD on; cursor off, blink off
    test            VAR     Nib
    
    HIGH TX                               ' setup TX pin for LCD
    PAUSE 100                             ' allow LCD to intialize
    baud = T19K2
    scount = 0                            ' zero the counter
    key = 0                               ' zero the key buffer
    moidx = 0                             ' zero the mo index
    
    SEROUT TX, baud, [noparse][[/noparse]LcdOn1, LcdCls, LcdBlOn]     ' initialize lcd
    PAUSE 5
    SEROUT TX, baud, [noparse][[/noparse]"MO ", LcdCr, DEC scount]    ' initialize display
    
    main:
       HIGH 2
       PAUSE 100
       RCTIME 2, 1, time
       IF (time > dark) THEN
        SEROUT TX, baud, [noparse][[/noparse]LcdBlOn]
       ELSE
        SEROUT TX, baud, [noparse][[/noparse]LcdBlOff]
       ENDIF
    
       GOSUB checkSensor
       IF schanged = 1 THEN
        SEROUT TX, baud, [noparse][[/noparse]LcdCls]
        PAUSE 5
        'SEROUT TX, baud, [noparse][[/noparse]"MO ", STR mo\7, LcdCr, DEC scount]
        SEROUT TX, baud, [noparse][[/noparse]"MO ", LcdCr, DEC scount]
        schanged = 0
       ENDIF
    
       GOSUB keyScan
       IF press = 0 THEN main
       mo(moidx) = key
       IF moidx < 7 THEN
        moidx = moidx + 1
       ENDIF
       SEROUT TX, baud, [noparse][[/noparse]LcdCls]
       PAUSE 5
    SEROUT TX, baud, [noparse][[/noparse]128]
    PAUSE 5
    SEROUT TX, baud, [noparse][[/noparse]"TEST ", mo(0)]
       SEROUT TX, baud, [noparse][[/noparse]"MO ", STR mo\7, LcdCr, DEC scount]
       FOR i = 0 TO 7
         DEBUG "-", DEC mo(i)
       NEXT
       DEBUG DEC key, CR
       'DEBUG "MO ", STR DEC mo\7, CR
       press = 0
    
    GOTO main
    
    
    keyScan:
      FOR row = 8 TO 11
    TEST=row-8
        LOW row
        key = ~cols
        key = NCD key
        IF key <> 0 THEN push
        INPUT row
      NEXT
      db = 0
    RETURN
    
    push:
      IF db = 1 THEN done
      db = 1: press = 1
    '  key = (key-1)+(row*4)
    key = (key-1)+(TEST*4)
      LOOKUP key,[noparse][[/noparse]"1","2","3","10","4","5","6","11","7","8","9","12","14","0","15","13"],key
    
    done:
      INPUT row
    RETURN
    
    checkSensor:
      IF IN1 = 1 THEN
        IF sdb = 1 THEN RETURN
        sdb = 1
        schanged = 1
        scount = scount+1
      ELSE
        'sensor went low so we can reset debounce
        sdb = 0
      ENDIF
    RETURN
    
    
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-09-30 00:40
    Hi blittle, Phil is definately right on the lookup table so we need to keep the test variable for now. The lcd expects ASCII character codes which I wrongly tried to pass in to a nib variable. I adjusted the code and commented out the sensor sub for now, I figured it would be best to see if you could get some sense on the LCD first.

    Try the attached mod of your code

    Jeff T.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-09-30 14:43
    Just as a note the code in the modified version has the ASCII character codes entered directly. The codes can just as easily be passed by typing the characters inside quotation marks, and probably be easier to read. So the two lookup tables below will have the same result in your code.

    LOOKUP key,[noparse][[/noparse]49,50,51,65,52,53,54,66,55,56,57,67,69,48,70,68],mo(moidx)
    LOOKUP key,[noparse][[/noparse]"1","2","3","A","4","5","6","B","7","8","9","C","E","0","F","D"],mo(moidx)

    BTW the modified code in the last post has two typos in the lookup table, the zero and the F were given the wrong values. The above is correct.

    Jeff T.

    EDIT: another possible problem in your code is the statement IF key <> 0 THEN push I believe should be IF key <> 0 THEN GOSUB push


    Post Edited (Unsoundcode) : 9/30/2007 8:52:45 PM GMT
  • blittleblittle Posts: 19
    edited 2007-09-30 21:26
    It works. [noparse]:)[/noparse]
    Thanks for your help.
    I'll post the final code and schematic back here after I add some features and get it cleaned up.
    It's going to be a simple keypad, lcd, and mountable sensor so a worker can key a Manufacturing Order number then count pieces produced, that will be sent to a pc for data collection.
    A basic commercial sensor counter can be quite expensive, around $300.
    This project costs somewhere around half that, including the prox sensor, and of course it's hackable and has a pc interface.
    So people in manufacturing (like me) might appreciate it.
    Thanks again.
    Bill
Sign In or Register to comment.