#picaxe 08m 'setfreq m4 'Not necessary at 4MHz - Saves 4 bytes ' Assign names to pins, variables to registers,and constants. symbol kdel = w5 'Word variable : Delay between last and current knock input symbol cnt1 = b0 symbol kcnt = b1 'Number of knocks symbol pptr = b2 'Pointer to knock delay storage array symbol mic = pin2 'Microphone input (can also be output) symbol schalter = pin3 'Schalter zum Schliessen symbol led = 0 'Indicator LED for testing symbol pstart = $50 'Start of storage area symbol tmax = 1000 'Time-out value for delay - About 1.5 seconds symbol settle = 90 'Settling time for microphone init: servo 4,190 'Schliesst den Servo input 3 'pin 3 als Schliesseingang Pin3 = 0 'setzt pin3 auf 0 ' This section is where the person knocks. ' The time between successive knocks is stored ' until there is a pause of 1.5 seconds or so. do 'Start of main loop kcnt = 0 'Initialise knock counter input 2 'Define mic as input to detect knocks do for kdel = 1 to tmax 'KDel is counting loops to determine time between knocks if mic = 1 then 'Detect HI on microphone input inc kcnt 'Add 1 to number of knocks high led 'Flash LED to help debugging pause settle 'Wait for oscillations to stop low led kdel = kdel + settle 'Add settle time to loop counter for accuracy pptr = kcnt * 2 + pstart'KCnnt is a word variable so need to add 2 to pointer poke pptr,word kdel 'Write the delay from last knock to storage area kdel = 0 'Reset kdel ready for next knock end if next kdel loop until kdel >= tmax 'If no knocks for over TMax loops then leave loop 'This section will operate sepending on the number of knocks detected. 'If there are no knocks it will exit. 'Other things happen on 13 or 20 knocks, or it will echo the knocks for any other number. 'The maximum number of knocks is 23 to fill buffer. Incorrect count after that. select case kcnt case 0 'Do nothing if no knocks have occurred case 6 tune 0, 5,($23,$26,$29,$6C,$6B,$2A,$26,$23,$6C,$2B,$2A,$23,$26,$29,$EB,$6C,$2A,$22,$25,$6C,$66,$28,$2B,$2A,$6C,$68,$26,$25,$23,$22,$E3) gosub Openservo else 'Any other number of knocks will be echoed tune 0, 6,($20,$20,$60,$20,$23,$62,$22,$60,$20,$60,$E0) pause 9000 pause 200 endselect loop ' This section contains the knocking routines Openservo: servopos 4,75 do high led pause 400 low led pause 400 high led pause 400 low led pause 5000 loop until schalter = 1 gosub Closeservo 'löst den schliessvorgang aus return Closeservo: servopos 4,190 return