PICAXE Inputs
Zip File ©Christopher Leigh
4 Key Input
For input to the PICAXE here is a 4 key matrix. It uses input pins 0,1,6,7 in the order shown in the PCB layout. Input pin 2 is not used but is connected to the central input of this block so that you can use another switch or sensor block. Pin 2 can take an analog input so you could use a light sensor block or other analog sensor block. There is a 1M pull-up resistor indicated on input 2, but this does not need to be used in many cases.
Here's a program that lights an LED corresponding to the key pressed:symbol key1=input1 'rename input source
symbol key2=input0
symbol key3=input7
symbol key4=input6
main: if key1=0 then flash1
if key2=0 then flash2
if key3=0 then flash3
if key4=0 then flash4
goto main
flash1: high 1
goto endloop
flash2: high 2
goto endloop
flash3: high 3
goto endloop
flash4: high 4
endloop: let pins=%00000000 'reset all LEDs
goto main
|
|
Another 4 Key Input
This block is exactly the same as the one above but simply with the keys in a different arrangement. It is useful for controlling a buggy with left, right, forward and back commands. Both blocks use an active low input for the PICAXE. In other words the inputs pins are held high except when you press a key. If you only want one input to the PICAXE you only need to use the block you want, but that will be connected to input 1.
Here is a shorter program which allows you to press more than one key at once:main: let b0=pins 'get inputs
b1=b0&%11000011 'mask unwanted bits
let pins=b1 'set outputs
goto main
|
|
Light Input
You can use standard sensor blocks with any PICAXE input, perhaps using a comparator to set the switching level. However a PICAXE 18 has analog inputs on pins 0,1 and 2. Connecting directly to the Core block will use pin 1. If you put the sensing block before one of the 4 key units then the input will be on pin 2.main: readadc 1,b0 'read input 1 into variable b0
debug b0 'check value on computer
b1=b0/20 'change into 8 levels
if b1<8 then skip
b1=7
skip: let pins=0 'turn current LED off
high b1 'show value on 8 LEDs
goto main
|
|
Temperature Input
This uses the DS18B20 digital thermometer. The voltage required is between 3v and 5.5v so a 5v power supply should be just right. See ds18b20.pdf or go to the products/upgrades page at www.picaxe.co.uk. You should also check out axe001_basic_commands.pdf for details about using the readtemp command. The pcb shown passes 4 inputs through to a preceding block.loop: pause 500
readtemp 1,b1
if b1>127 then neg
serout 4,N2400,(254,128,"Temp is ",#b1," ")
goto loop
neg: let b1=b1-128
serout 4,N2400,(254,128,"Temp is ","-",#b1," ")
goto loop
|
|
Infra-red Input
You can use a Temic 1836 device to receive signals from an ordinary remote control. As long as it uses the standard RC5 coding system then the Picaxe can understand it using Infrain and Infra commands. The first program turns LEDs on and off depending on which keys on the remote are pressed. The pcb looks more complicated than it is really because it passes through 4 of the inputs.loop: infrain 'must uses pin 0
if infra = 1 then swon1
if infra = 2 then swon2
if infra = 3 then swon3
if infra = 4 then swoff1
if infra = 5 then swoff2
if infra = 6 then swoff3
goto loop
swon1: high 1
goto loop
swon2: high 2
goto loop
swon3: high 3
goto loop
swoff1: low 1
goto loop
swoff2: low 2
goto loop
swoff3: low 3
goto loop
|
|
12 Key Matrix (home-made)
In order to read 12 keys we use outputs 0-2 of the PICAXE to push each column low in order. At the same time the 4 inputs 0,1,2,6 are used to read each row to see whether it has gone low. If it has then the key pressed can be deduced by the program. Pin 7 is available as an input pin for the preceding block. The pull-up resistors hold the inputs normally high.' Keypad Reader for PICAXE18
symbol col1=output0
symbol col2=output1
symbol col3=output2
symbol row1=input6
symbol row2=input0
symbol row3=input1
symbol row4=input2
symbol key_value = b1 ' value of key pressed
' *** scan each column in turn ***
' *** by setting only 1 column low ***
init: let pins=%00000111 'normally high
scan: let key_value = 1
low col1
gosub key_test
let key_value = 2
low col2
gosub key_test
let key_value = 3
low col3
gosub key_test
goto scan 'loop back
' *** return straight away if no key pressed ***
key_test:
if row1 = 0 then add1
if row2 = 0 then add2
if row3 = 0 then add3
if row4 = 0 then add4
let pins=%00000111 'reset pins
return
' *** key value will already be 1, 2, or 3 ***
' *** so add 0 or 3 to this value ***
add4: let key_value = key_value + 3
add3: let key_value = key_value + 3
add2: let key_value = key_value + 3
add1: let pins=%10000111 'set pin 7
debug key_value 'send key to computer
pause 200
low 7
return
|
|
12 Key Matrix (commercial)
|
This block works in the opposite fashion to the one above. In order to read 12 keys we use outputs 0-2 of the PICAXE to push each column high in order. At the same time the 4 inputs 0,1,2,7 are used to read each row to see whether it has gone high. If it has then the key pressed can be deduced by the program. Pin 6 is available as an input pin for the preceding block. The pull-down resistors hold the inputs normally low. This is the same keypad used in the PICAXE lock kit, and the program given above is a modified version of one that appears in the documentation. See chi008.pdf or go to the documentation page at www.picaxe.co.uk.
For more inputs see the document axe001_pic_electronics.pdf
|
|