programming the basic stamp with keypad to be able to read and write 00-99
I am quite new at programming and I am using the program for the keypad. The program only reads 0-9 and a-d and i want to be able to read and write 00-99 a 2 digit number.
any help will be appreciated.
Thank You,
Paul
any help will be appreciated.
Thank You,
Paul
Comments
Please link to the code you're referring to, attach it, or post it in code tags so others can see what you're referring to. Also you should link the keypad being used as well.
I apologize since I am very new but I will be getting better as I use this web-site an well as learning programming.
Thank You,
Paul A. Valdez
I have no BS2 to try, and also not much expirience with the BasicStamp, but here is a code-fragment that should build a 2 digit dec from two keypresses.
If I understand the ReadKeypad subroutine right it returns a 16bit value where each bit stands for a pressed key.
The GetDecKey subroutine waits until all keys are released and then a new key is pressed. Then the key value is decoded:
With NCD keypad you get a number 1..16 for the key, and the LOOKUP reads the corresponding dec value for the key from a table (0..9).
value VAR Word DO GOSUB WaitDecKey value = keypad GOSUB WaitDecKey value = value * 10 + keypad DEBUG value, CR LOOP WaitDecKey: DO GOSUB ReadKeypad LOOP UNTIL keypad = 0 DO GOSUB ReadKeypad LOOP UNTIL keypad <> 0 LOOKUP NCD keypad, [0,0,0,0,0,9,8,7,0,6,5,4,0,3,2,1], keypad RETURN 'add also the ReadKeypad subroutine and the needed variables
AndyI still can't put the code on this forum. The code I am trying to modify is the demo given for the keypad on your web site. Please help me.
Thank You,
Paul
Type (code) on the line before you code and (/code) on the line below it. Use [] instead of the () I used.
Did Ariba's example not work for you? It looks like it should at first glance. You need only add the existing keypad read subroutine and necessary variable declarations as he suggested.
' Display buttons pressed on the 4x4 Matrix Membrane Keypad
' Author: Parallax HK Engineering
' +---+---+---+---+
' P0 --X--X--X--X | 1 | 2 | 3 | A |
' | | | | +---+---+---+---+
' P1 --X--X--X--X | 4 | 5 | 6 | B |
' | | | | +---+---+---+---+
' P2 --X--X--X--X | 7 | 8 | 9 | C |
' | | | | +---+---+---+---+
' P3 --X--X--X--X | * | 0 | # | D |
' | | | | +---+---+---+---+
' R R R R <
4x1k resistors
' | | | |
' P4 P5 P6 P7
'
' {$STAMP BS2}
' {$PBASIC 2.5}
row VAR Nib ' Variable space for row counting column VAR Nib ' Variable space for column counting keypad VAR Word ' Variable space to store keypad output keypadOld VAR Word ' Variable space to store old keypad output temp VAR Nib ' Variable space for polling column states DEBUG CLS ' Clear Debug Terminal GOSUB Update ' Display keypad graphic DO GOSUB ReadKeypad ' Read keypad button states DEBUG HOME, BIN16 keypad, CR, CR, ' Display 16-bit keypad value BIN4 keypad >> 12,CR, ' Display 1st row 4-bit keypad value BIN4 keypad >> 8, CR, ' Display 2nd row 4-bit keypad value BIN4 keypad >> 4, CR, ' Display 3rd row 4-bit keypad value BIN4 keypad ' Display 4th row 4-bit keypad value IF keypad <> keypadOld THEN ' If different button is pressed, GOSUB Update ' update the keypad graphic to clear ENDIF ' old display IF keypad THEN ' Display button pressed in graphic GOSUB display ENDIF keypadOld = keypad ' Store keypad value in variable keypadOld LOOP
'[ Subroutine - ReadKeypad ]
'
Read keypad BUTTON states ReadKeypad: keypad = 0 OUTL = 000000 ' Initialize IO DIRL = 000000 FOR row = 0 TO 3 DIRB = 11 ' Set columns (P7-P4) as outputs OUTB = 00 ' Pull columns low (act as pull down) OUTA = 1 << Row ' Set rows high one by one DIRA = 1 << Row temp = 0 ' Reset temp variable to 0 FOR column = 0 TO 3 INPUT (column + 4) ' Set columns as inputs one by one temp = temp | (INB & (1 << column)) ' Poll column state and store in temp NEXT keypad = keypad << 4 | (Temp REV 4) ' Store keypad value NEXT RETURN
'[ Subroutine - Update ]
' Graphical depiction of keypad Update: DEBUG CRSRXY,0,7, "+---+---+---+---+",CR, "| | | | |",CR, "+---+---+---+---+",CR, "| | | | |",CR, "+---+---+---+---+",CR, "| | | | |",CR, "+---+---+---+---+",CR, "| | | | |",CR, "+---+---+---+---+" RETURN
'[ Subroutine - Display ]
' Display button pressed in keypad graphic Display: IF KeyPad.BIT15 THEN DEBUG CRSRXY, 02,08,"1" IF Keypad.BIT14 THEN DEBUG CRSRXY, 06,08,"2" IF KeyPad.BIT13 THEN DEBUG CRSRXY, 10,08,"3" IF Keypad.BIT12 THEN DEBUG CRSRXY, 14,08,"A" IF KeyPad.BIT11 THEN DEBUG CRSRXY, 02,10,"4" IF Keypad.BIT10 THEN DEBUG CRSRXY, 06,10,"5" IF KeyPad.BIT9 THEN DEBUG CRSRXY, 10,10,"6" IF Keypad.BIT8 THEN DEBUG CRSRXY, 14,10,"B" IF KeyPad.BIT7 THEN DEBUG CRSRXY, 02,12,"7" IF Keypad.BIT6 THEN DEBUG CRSRXY, 06,12,"8" IF KeyPad.BIT5 THEN DEBUG CRSRXY, 10,12,"9" IF Keypad.BIT4 THEN DEBUG CRSRXY, 14,12,"C" IF KeyPad.BIT3 THEN DEBUG CRSRXY, 02,14,"*" IF Keypad.BIT2 THEN DEBUG CRSRXY, 06,14,"0" IF KeyPad.BIT1 THEN DEBUG CRSRXY, 10,14,"#" IF Keypad.BIT0 THEN DEBUG CRSRXY, 14,14,"D" RETURN
Paul
I got my hardware assembled and code started and ran out of time. What I will be posting today doesn't use the example code at all. I actually decided against that the moment I looked at it.
Thanks
Attached is a simple program to do what you asked. Please note the schematic in the comments is different than the example code you have. I didn't write the original example and felt it was overly complex and wasted resources. The attached example uses the same I/O pin connections, except that there are 4 x 10K pull-down resistors instead of the 4 x 1K inline resistors. It is important to make these connections properly. My code uses a traditional row/column bit-scan and adds an offset to the key value until a key has been found. This value is the translated through the LOOKUP command to return the value of the key (or ASCII value in the case of the non-numeric keys). This example does not handle timeouts on the input, has minimal error checking and is hard-coded to a single key value or a 2-digit number (see comments in code). Note that the reason it took me so long today was I was playing around with using the * key as a backspace key, the C to clear the entry and the # to terminate input for up to a 5-digit number. In this case though I decided to stick to what you were trying to do since I didn't think you would be able to modify the full version. I hope this helps. I finished it during my lunch break. :cool:
Your program worked. But how can I assign each value from 00 to 99 a different variable for instance 00 apples, 01 screws etc.
Thank You,
Paul
You wouldn't assign each number to a variable. The number is returned in a variable already and you just need to say:
IF num = 1 THEN
IF num = 2 THEN
etc.
Depending on how many values you use there are other ways to do this. You'll also be limited by the memory of the BASIC Stamp as to how many words you can put into memory. If you need more you'll need a different model of BS2 or an external EEPROM to hold the data.
Thank You,
Paul
Did you get the Private Message I sent you? As for where to check the characters, the main loop calls a subroutine to get the value of the number you type in. When it returns the variable specified contains that number. It is then displayed on the LCD. However you could instead have it print different messages based on the value. My example was designed to show you how to get a 2-digit value back from the keypad the way I felt the example code should have worked.
I'm pavan aditya
I'm trying to take up small module which displays the required text on the LCD when entered the repective code on the numeric pad.........
If you dont mind can you assist me with a helpful code for it......please.....
You will get better results by asking your question in one place rather than duplicating it in private conversations. It helps for you to specify what LCD and keypad you're using and whether you're using a Basic
Stamp or Propeller for a controller. An example of what output you want to see on the LCD for a key or keys on the keypad would also help.