Ultimate Guide Links & resources
Side BAr Information
Sample Code
Chapter 10 – Triggers and sensors
SIDEBAR 1
#Picaxe 08M2
symbol PIR_IN=pinC.1
symbol PIR_COUNT=b5
Init: 'Just a starting point label
Pause 6000 'Wait 60 secs for PIR to 'settle down
Testit: 'PIR motion test
Do : Loop Until PIR_IN=1
Do
PIR_COUNT = PIR_COUNT + 1 * PIR_IN
Loop Until PIR_COUNT = 10
PIR_COUNT = 0
goto Routine
SIDEBAR 2
' {$STAMP BS2}
' {$PBASIC 2.5}
' {$PORT COM6}
'---------variables------------------------------------------
lid_servo VAR Word
repeat VAR Byte
rawDist VAR Word
inches VAR Word
'---------pins------------------------------------------
Ping PIN 2
servo1 PIN 0
'---------constants------------------------------------------
Sonar CON 5 ' trigger pulse = 10 uS
Scale CON $200 ' raw x 2.00 = uS
Baud CON $8000 | 6
RawToIn CON 889 ' 1 / 73.746 (with **)
RawToFt CON 889 / 12
'---setup--screen-----
'---------program-----------------------------------------------
main:
GOSUB Get_Sonar ' get sensor value
inches = rawDist ** RawToIn ' convert to inches
DEBUG CRSRXY, 20,3, DEC inches, CR
IF inches < 40 AND inches > 10 THEN GOSUB move_servo
IF inches < 8 THEN GOSUB close_lid
GOTO main
move_servo:
lid_servo = inches *30 MAX 1050 MIN 300
FOR repeat = 0 TO 4
PULSOUT 0, lid_servo
PAUSE 12
NEXT
DEBUG DEC lid_servo
PAUSE 20
DEBUG CLS
RETURN
close_lid:
FOR repeat = 0 TO 5
PULSOUT 0, 240
PAUSE 15
NEXT
RETURN
Get_Sonar:
Ping = 0 ' make trigger 0-1-0
PULSOUT Ping, Sonar ' activate sensor
PULSIN Ping, 1, rawDist ' measure echo pulse
rawDist = rawDist */ Scale ' convert to uS
rawDist = rawDist / 2 ' remove return trip
RETURN
Chapter 14 – Puppeteering
Picaxe Code for Switch
01 ' Program: PWM to On-Off1.bas
02
03 ' Status: Good
04 '
05 ' Description: Program to convert an standard servo PWM command to an on/off state. It is to be used when
06 ' servo commands are the only available output and need to be converted to a simple
07 ' on or off state (e.g.,relay control). Standard PWM range is 500ms to 2500ms with an update
08 ' period of ~20ms to 40ms (50Hz to 25Hz).
09 '
10 ' Ouput PICAXE port will be as follows based on PWM input:
11 ' 500 ms to 1500ms input pulse = OFF (logical 0 0r 0VDC)
12 ' 1501ms to 2500ms input pulse = ON (Logical 1 or 5VDC)
13 '
14 ' Employs the Pulsin command that measures pulse widths in 10 us increments.
15 ' If no pulse is detected within a 0.635 seconds period, it will output a "0".
16 ' This would imply the code logic would set the ouput port to OFF (logical 0 or 0VDC).
17 '
18 ' Runs on 08M2 project board using 3 AA batteries; Default 4 MHz clock
19 ' Servopos (75 to 225)is the expanded range of typical servo motor
20 '
21 '
22 ' Author: Brian Lincoln
23 ' Date: 6/6/16 Original
24
25 ' Updates:
26 ' 6/6/16 BTL Original Code
27
28 '
29
30 '
31
32 '---------- Declarations ------------------------------------------------
33 #Picaxe 08M2 ' Default PICAXE 08m2+ running at 4 MHz
34
35 'symbol xxx = C.0 ' Serial Out - Do not use
36 symbol new_out = C.1 ' Converted Output On or Off
37 'symbol xxx = C.2 ' Spare
38 symbol PWM_Input = C.3 ' PWM signal to be converted INPUT ONLY
39 'symbol xxx = C.4 ' Spare INPUT or OUTPUT
40 'symbol xxx - C.5 ' Serial In - Do not use
41
42
43 symbol pulse_read = w2 ' Variable to read pulse counts in 10us increments
44
45
46
47 '---------- Initialize --------------------------------------------------
48 init:
49 low new_out 'Initialize output pin to low (or OFF)
50
51 '---------- Main Code ----------------------------------------------------
52
53 main:
54
55 pulsin PWM_Input,1,pulse_read ' record the length of a pulse on C.3 into w2 in 10us increments (or counts)
56 ' for example 500us = 50 counts, 1500us = 150 counts, 2500us - 250 counts
57
58 if pulse_read > 150 then 'If pulse that was read is bigger than 150 counts (or 1500us) then set output pin high
59 high new_out
60 else
61 low new_out 'If pulse that was read is smaller than 150 counts (or 1500us) then set output pin low
62 endif 'Note: If pulse is not detected at all, it will record a "0" which defaults to this "OFF" state.
63
64
65 goto main ' loop back to start
Chapter 16 - Eyes
SIDEBAR 1
#Picaxe 08M2
'C.1 is Eyes Left/Right servo
'C.2 is Eye Up/down servo
'C.4 is is Eye Lid servo
symbol Eyelr = C.1
symbol Eyeud = C.2
symbol Eyelid = C.4
symbol counter = b9
symbol pointer = b8
symbol delay = 10
symbol counter1 = b19
symbol pointer1 = b18
symbol delay1 = 39
symbol counter2 = b25
symbol pointer2 = b24
symbol delay2 = 45
start0:
pause 500
start1:'Eye Lids
let b8=133 'set pointer offset so that Eye Lid loop does not get stuck
let b6=130
let b7=130 'set default center values
servo Eyelid,pointer 'center servo
pause 7500
for counter = 130 to 83 step -1
servopos Eyelid,counter
pause delay
next counter
pause 3000
for counter = 83 to 180 step 1
servopos Eyelid,counter
pause delay
next counter
pause 3000
for counter = 180 to 130 step -1
servopos Eyelid,counter
pause delay
next counter
pause 4000
move: random w3 'place a random number in wo (also B0 & B1)
if b7<83 then left 'check for random down
if b7>=83 and b7<=130 then middle 'check for random middle
if b7>=130 then right
goto move 'keep moving the Eye Lid back to move loop
right: if pointer = 83 then move
if pointer = 130 then mr
for counter = 180 to 83 step -1
servopos Eyelid,counter
pause delay
next counter
pointer = 83
goto skipmr
mr: for counter = 130 to 83 step -1
servopos Eyelid,counter
pause delay
next counter
pointer = 83
skipmr: pause 7000 'delay to look natural
goto move 'return to move loop
middle: if pointer = 130 then move 'if Eye Lid is already in the middle then go back to move
if pointer = 83 then ml
for counter = 180 to 130 step -1
servopos Eyelid,counter
pause delay
next counter
pointer = 130
goto skipml
ml: for counter = 83 to 130 step 1
servopos Eyelid,counter
pause delay
next counter
pointer = 130
skipml: pause 9000 'delay to look natural
goto move 'return to move loop
left: if pointer = 180 then move
if pointer = 83 then lmr
for counter = 130 to 180 step 1
servopos Eyelid,counter
pause delay
next counter
pointer = 180
goto skiplmr
lmr: for counter = 83 to 180 step 1
servopos Eyelid,counter
pause delay
next counter
pointer = 180
skiplmr: pause 7000 'delay to look natural
goto move 'return to move loop
start2:'Eye Left/Right
let b18=153 'set pointer offset so that Eye Left/Right loop does not get stuck
let b16=150
let b17=150 'set default center values
servo Eyelr,pointer1 'center servo
pause 8000
for counter1 = 150 to 125 step -1
servopos Eyelr,counter1
pause delay1
next counter1
pause 3000
for counter1 = 125 to 175 step 1
servopos Eyelr,counter1
pause delay1
next counter1
pause 3000
for counter1 = 175 to 150 step -1
servopos Eyelr,counter1
pause delay1
next counter1
pause 2000
move1: random w8 'place a random number in wo (also B0 & B1)
if b17<125 then left1 'check for random left
if b17>=125 and b17<=150 then middle1 'check for random middle
if b17>=150 then right1
goto move1 'keep moving the Eye Left/Right back to move loop
right1: if pointer1 = 125 then move1
if pointer1 = 150 then mr1
for counter1 = 175 to 125 step -1
servopos Eyelr,counter1
pause delay1
next counter1
pointer1 = 125
goto skipmr1
mr1: for counter1 = 150 to 125 step -1
servopos Eyelr,counter1
pause delay1
next counter1
pointer1 = 125
skipmr1: pause 7000 'delay to look natural
goto move1 'return to move loop
middle1: if pointer1 = 150 then move1 'if Eye left/Right is already in the middle then go back to move
if pointer1 = 115 then ml1 'check to see if Eye is pointed left or right
for counter1 = 175 to 150 step -1
servopos Eyelr,counter1
pause delay1
next counter1
pointer1 = 150
goto skipml1
ml1: for counter1 = 125 to 150 step 1
servopos Eyelr,counter1
pause delay1
next counter1
pointer1 = 150
skipml1: pause 9000 'delay to look natural
goto move1 'return to move loop
left1: if pointer1 = 175 then move1
if pointer1 = 125 then lmr1
for counter1 = 150 to 175 step 1
servopos Eyelr,counter1
pause delay1
next counter1
pointer1 = 175
goto skiplmr1
lmr1: for counter1 = 125 to 175 step 1
servopos Eyelr,counter1
pause delay1
next counter1
pointer1 = 175
skiplmr1: pause 7000 'delay to look natural
goto move1 'return to move loop
start3: 'Eye Up/Down
let b24=153 'set pointer offset so that Eye Up/Down loop does not get stuck
let b22=150
let b23=150 'set default center values
servo Eyeud,pointer2 'center servo
pause 9500
for counter2 = 150 to 120 step -1 'move Eye from middle to up
servopos Eyeud,counter2
pause delay2
next counter2
pause 3000
for counter2 = 120 to 200 step 1
servopos Eyeud,counter2 'move Eye from up to down
pause delay2
next counter2
pause 3000
for counter2 = 200 to 150 step -1
servopos Eyeud,counter2 'move Eye from down to middle
pause delay2
next counter2
pause 4000
move2: random w11 'place a random number in wo (also B0 & B1)
if b23<120 then left2 'check for random up
if b23>=120 and b23<=150 then middle2 'check for random middle
if b23>=150 then right2 'check for random down
goto move2 'keep moving the Eye back to move loop
right2: if pointer2 = 120 then move2 'if Eye is already down goto back to move
if pointer2 = 150 then mr2 'check to see if Eye in pointed middle or up
for counter2 = 200 to 120 step -1 'move Eye from up to down
servopos Eyeud,counter2
pause delay2
next counter2
pointer2 = 120 'set Eye pointer direction to down
goto skipmr2
mr2: for counter2 = 150 to 120 step -1 'move Eye from middle to down
servopos Eyeud,counter2
pause delay2
next counter2
pointer2 = 120 'set Eye pointer direction to down
skipmr2: pause 7000 'delay to look natural
goto move2 'return to move loop
middle2: if pointer2 = 150 then move2 'if Eye is already in the middle then go back to move
if pointer2 = 120 then ml2 'check to see if head is pointed up or down
for counter2 = 200 to 150 step -1
servopos Eyeud,counter2 'move Eye from down to middle
pause delay2
next counter2
pointer2 = 150
goto skipml2
ml2: for counter2 = 120 to 150 step 1
servopos Eyeud,counter2 'move servo from left to middle
pause delay2
next counter2
pointer2 = 150
skipml2: pause 9000 'delay to look natural
goto move2 'return to move loop
left2: if pointer2 = 200 then move2 'if head is already left then return to move
if pointer2 = 120 then lmr2 'check if the head is in the middle or the right
for counter2 = 150 to 200 step 1
servopos Eyeud,counter2 'move head from middle to left
pause delay2
next counter2
pointer2 = 200
goto skiplmr2
lmr2: for counter2 = 120 to 200 step 1
servopos Eyeud,counter2 'move head from right to left
pause delay2
next counter2
pointer2 = 200
skiplmr2: pause 7000 'delay to look natural
goto move2 'return to move loop
Chapter 21 - Bandit
#Picaxe 18M2
'C.1 is to Tenda
'B.1 is Waist Bend
'B.2 is Spread Wings
'B.3 is Head Nod
Symbol Waist = B.1
Symbol Wings = B.2
Symbol Head = B.3
Symbol Tenda = C.1
Init:
serout Tenda,4800, ($EF); 'STOP MP3 module
pause 1000
serout Tenda,4800, ($E1); 'Set MP3 volume
pause 1000
Servo Waist, 50
Servo Head, 210
Servo Wings, 170
Routine:
servopos Head, 130
pause 1000
servopos head, 210
pause 1000
servopos Wings, 90
pause 250
servopos Wings, 170
pause 1500
servopos Waist, 150
pause 1000
servopos Waist, 100
pause 3000
serout Tenda,4800,($01) 'Start playing first mp3
pause 500
servopos Waist, 50
pause 1000
servopos Waist, 100
pause 3000
servopos Wings, 90
pause 250
servopos Wings, 170
pause 1500
servopos Waist, 150
pause 1000
servopos Waist, 75
pause 1500
servopos Head, 100
pause 1000
servopos head, 160
pause 1000
servopos wings, 90
pause 500
servopos wings, 170
pause 1500
servopos Waist, 120
pause 1000
servopos Waist, 90
pause 2750
servopos Head, 210
pause 1000
servopos head, 140
pause 1000
servopos wings, 90
pause 250
servopos wings, 170
pause 5000
servopos Waist, 150
pause 1000
servopos Waist, 90
pause 2000
servopos Head, 210
pause 1000
servopos head, 140
pause 1000
servopos wings, 100
pause 500
servopos wings, 170
pause 2500
servopos Head, 130
pause 1000
servopos head, 180
pause 1000
servopos Wings, 90
pause 250
servopos Wings, 170
pause 1500
servopos Waist, 150
pause 1000
servopos Waist, 100
pause 3000
for time = 1 to 60'60 is the number of seconds of retrigger delay
pause 1000 'Pause for 1 sec
next time
goto Routine