You are on page 1of 7

CODE: MICROSWITCH The following program looks for microswitch activations on input pin C.

0 and counts those activations. When five activations have been counted a LED on output pin B.1 will be set. The 'pause 100' acts as a contact de-bounce; if it is removed contact bouncing may cause the LED to be lit prematurely. init: let b0 = 0

main: if pinC.0 = 1 then add goto main

add:

pause 100 let b0 = b0 + 1 if b0 < 5 then main high B.1 goto main

; short delay

KEYPAD This program demonstrates example code for the CHI008 Keypad Lock kit using a PICAXE-18M2 #picaxe 18m2

;output 7 - FET to drive solenoid bolt ;output 6 - piezo sounder ;output 4,5 - bicolour LED ;output 3 - row 4 ;output 2 - row 3 ;output 1 - row 2

;output 0 - row 1

;input 0 - column 1 ;input 1 - column 2 ;input 2 - column 3

symbol key_pos = b0 symbol key_value = b1

; number of keys pressed ; value of key pressed

; *** reset position to zero ***

init: let dirsB = 255 let key_pos = 0

; *** now scan each row in turn *** ; *** by setting only 1 row (and LED) high *** ; *** if a switch is hit jump call score sub below ***

scan: let key_value = 0 let pinsB = %00010001 gosub key_test

let key_value = 3 let pinsB = %00010010

gosub key_test

let key_value = 6 let pinsB = %00010100 gosub key_test

let key_value = 9 let pinsB = %00011000 gosub key_test

goto scan

; *** Score sub procedure. *** ; *** return straight away if no key pressed ***

key_test: if pinC.0 = 1 then add1 if pinC.1 = 1 then add2 if pinC.2 = 1 then add3 return

; *** key value will already be 0, 3, 6, or 9 *** ; *** so add 1, 2 or 3 to this value ***

add3: let key_value = key_value + 1 add2: let key_value = key_value + 1

add1: let key_value = key_value + 1

; *** Make a beep ***

sound B.6,(60,50)

; *** Now increase position counter by 1 *** ; *** and test for 1st, 2nd 3rd or 4th push ***

let key_pos = key_pos + 1

if key_pos = 1 then test1 if key_pos = 2 then test2 if key_pos = 3 then test3 ; if key_pos = 4 then test4

; *** Now test the value for each position individually *** ; *** If value is wrong restart, if correct loop until *** ; *** fourth go. If fourth is correct open lock! ***

; *** Key code is set to 9-3-5-1 ***

test4: if key_value = 1 then open goto reset1

test3: if key_value = 5 then continue goto reset1

test2: if key_value = 3 then continue goto reset1

test1: if key_value = 9 then continue goto reset1

; *** Got here so open lock and set LED green for 5 sec ***

open: let pinsB = %10100000 wait 5 goto reset1

; *** Not correct value so reset position counter then return ***

reset1: let key_pos = 0

; *** Okay so continue by returning back to main loop ***

continue:

return

SERIAL OLED Reads the temperature from a DS18B20 and displays the result on the LCD low b.2 pause 500 main: readtemp c.1, b0 bintoascii b0, b1,b2,b3 digits serout b.2, n2400, ( 254, $80 ) serout b.2, n2400, ( "Temperature" ) serout b.2, n2400, ( 254, $C0 ) serout b.2, n2400, ( b1, b2, b3, "C" ) pause 1000 goto main ; First line of display ; Display "Temperature" ; Second line of display ; Display the temperature ; Initialise OLED output ; Waif for OLED to initialise ; Read DS18B20 on pin C.1 ; Convert temperature to ascii

; Wait a second ; Display latest temperature

INPUT PIN symbol MotA1 = 0 symbol MotA2 = 1 symbol MotB1 = 2 symbol MotB2 = 4 ;00 forward motor one, 01 backward motor 1, 10 forward motor 2, 11 backward motor 2 main: if input 3 = 0 then goto motorA ; jump to flsh if pin0 is high

end if goto main ; else loop back to start motorA:;run motor A pause 2000 ; wait 0.2 seconds if input 3 = 0 then goto goforward goforward: High MotA1 Low MotA2 ; switch off output B.1 end if goto main ; loop back to start

You might also like