;20x2 diesel sound generator.
#picaxe 20x2
#no_data
#no_table
'VARIABLE DEFINITIONS
'CONSTANT DEFINITIONS
'select variable 0-15
symbol var01=c.0
symbol var02=c.1
symbol var04=c.2
symbol var08=c.3
'select parameter
symbol par01=c.4
symbol par02=c.5
symbol par04=c.6
symbol par08=c.7
symbol PB1=pinb.0
symbol PB2=pinb.1
symbol ledY=b.2 ;yellow
symbol neutral_LED=b.3 'red neutral LED driven from "pin0" (leg 7) high=ON
symbol ledW=b.4 ;white
symbol ledG=b.5 ;green
symbol radio=b.6
symbol tda1052=b.7
symbol parameter=b0
symbol variable=b1
symbol running =b4 'engine running flag (1=running 0=stopped)
symbol demand=b5 'speed demand input, read from radio b.6
symbol kount=b6 'general purpose counter
symbol idle_count=w4 'counts revolutions at idle
symbol pawse=w5 'time between exhaust pulses: note spelling ("pause"
symbol p=b12 'exhaust pitch (passed from sound lookup table)
symbol t=b13 'exhaust duration (passed from sound lookup table)
symbol cownt=b15 'another general purpose counter
symbol timeout = b16 'number of revolutions at idle before engine stops
symbol speed=w9
symbol cyl_count=b20'number of cylinders in engine
symbol offset=b21
symbol ex_pitch=b22
symbol ex_dur=b23
symbol shutdown_cyl=b24
symbol shutdown_tmr=b25
symbol start_loop=b26
start_loop=5
symbol midpoint=w14
midpoint=290
symbol tickover=w15
tickover=18000 'EXPERIMENT WITH THESE VALUES TO CHANGE THE DYNAMICS OF THE ENGINE
symbol topspeed=b32
symbol revrange=w17
symbol stophalt=w18
symbol rate=w19
symbol limit=w20
symbol lo=w22
symbol hi=w23
symbol writ=b55
; 6 cylinder default for initial setup
offset=10
midpoint=10
cyl_count=6
ex_pitch=133
ex_dur=3
shutdown_cyl=6
;shutdown_tmr=
start_loop=5
midpoint=295
tickover=18000
topspeed=75
revrange=450
stophalt=30000
rate=3500
timeout=30
start_loop=5
timeout=cyl_count+1
limit=tickover-topspeed
if pb1=1 and pb2=1 then goto int2 ; needed for new chip
read 0,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31,b32,b33,b34,b35,b36,b37,b38,b39,b40,b41
int2:
do
sound tda1052,(50,50)
loop until pb1=0 and pb2=0
'========================= PROGRAM STARTS HERE ================================
'INITIALISATION
setfreq m8 'set 8Mhz operation to yield 5uSec resolution
main:
if pb1=1 and pb2=1 then goto setup
'get throttle demand from receiver at "b.6" and assign it to the variable 'speed'
pulsin radio,1,speed
;midpoint setup variable 7
lo=midpoint-offset
hi=midpoint+offset
if speed > lo and speed < hi then high ledy:else:low ledy:endif
if speed > lo and speed < hi then
speed=0
goto skipsped
endif
if speed>hi then ; forward
speed=speed-midpoint;hi
else ;reverse
speed=midpoint-speed;lo-speed
end if
skipsped:
'guard against over-range
if speed>120 then
speed=120
end if
'quantise speed into 25 values for lookup table
speed=speed/4
demand=speed*4
;==========================================================================================
'MAIN LOOP
if running=1 then
'ENGINE RUNNING
'process speed demand value and assign value to 'pawse'
pawse=demand*revrange max limit 'set spread of speed, limiting if necessary
pawse=tickover-pawse 'numerical sense of pawse is inversely related
pawse=pawse / 5
;pawse=pawse-90
'to speed (see above)
'generate a cycle of engine sound
for cownt=0 to cyl_count 'for each cylinder in turn
pause 2
sound b.7,(ex_pitch,ex_dur) 'make exhaust noise
pulsout ledg,pawse 'silence until next cylinder fires
next
'check if speed demand is IDLE
if demand<10 then
'ENGINE IDLING
high neutral_LED 'LED on
idle_count=idle_count+1 'increment idle time-out counter
else
'NOT IDLING
low neutral_LED 'LED off
idle_count=0 'reset idle time-out counter
end if
;=========================== STOP =======================================
'check if engine stop required
if idle_count>timeout then
'STOP ENGINE
for pawse=tickover to stophalt step rate 'gradually slow engine
'generate a cycle of engine sound
for cownt=0 to shutdown_cyl;cyl_count 'for each cylinder in turn
pause 2
sound b.7,(ex_pitch,ex_dur) 'make exhaust noise
pulsout ledg,pawse 'silence until next cylinder fires
next cownt
next pawse
running=0 'clear "engine is running" flag
end if
else
'=================================== START ==============================================
;debug'ENGINE IS NOT RUNNING
'but has demand been increased from idle in the meantime?
if demand>10 then
'YES - START ENGINE
for cownt=150 to 25 step -start_loop '25'make primitive engine cranking noise
sound b.7,(185,6)
sound b.7,(205,3)
sound b.7,(235,2) 'some experimentation may be of benefit!
sound b.7,(254,2) 'or omit this for-next loop altogether
pause cownt 'for instant start
next
'but leave this next line in!
running=1 'set "engine is running" flag
else
'NO - ENGINE REMAINS STOPPED
toggle neutral_LED 'flicker neutral LED to signify stopped
pause 80 'sets rate of flicker
end if
end if
goto main
;===============================SETUP=================================
;IN SETUP WHITE LED TOGGLES
setup:
sound tda1052,(20,30,40,50)
do
toggle ledw
pause 50
loop while pb1=1 or pb2=0
setup1:
toggle ledw
IF PB1=0 THEN SKIPSETUP
VARIABLE=PINSC AND %00001111
PARAMETER=PINSC AND %11110000
PARAMETER=PARAMETER/16
;debug
; center stick offset 0 to 15 either way
if parameter=0 then
offset=variable*2
endif
; cylinder
IF PARAMETER=1 THEN
;ADJUST CYLINDERS 1 TO 9 b14
CYL_COUNT=variable min 1 max 9
ENDIF
;exhaust pitch 133
IF PARAMETER=2 THEN
;ADJUST 125 + variable b22
ex_pitch=variable + 125
ENDIF
;exhaust duration 3
IF PARAMETER=3 THEN
;ADJUST 1 to 5 b23
ex_dur=variable min 1 max 15
ENDIF
;shutdown cycle cylinder 1-9
IF PARAMETER=4 THEN
;ADJUST
shutdown_cyl=variable min 1 max 15
ENDIF
;shutdown timer
IF PARAMETER=5 THEN
;time to shutdown b16
timeout= variable * 5 min 5 max 75
ENDIF
;start loop -5
IF PARAMETER=6 THEN
;ADJUST cranking start b26
start_loop= variable min 1 max 15
ENDIF
;mid point 270-310
IF PARAMETER=7 THEN
;ADJUST midpoint, stick to center.
pulsin radio,1,midpoint
ENDIF
;tickover 18000
IF PARAMETER=8 THEN
;ADJUST tickover w15
tickover=variable*1800 min 1800
ENDIF
;topspeed 75
IF PARAMETER=9 THEN
;ADJUST topspeed b32
topspeed=variable*15 min 15 max 255
ENDIF
;revrange 450 "A"
IF PARAMETER=10 THEN
;ADJUST revrange w17
revrange=variable * 50 min 50
ENDIF
;stophalt 30000 "B"
IF PARAMETER=11 THEN
;ADJUST stophalt w18
stophalt = variable * 3000 min 3000
ENDIF
;rate 3500 "C"
IF PARAMETER=12 THEN
;ADJUST step rate w19
rate= variable * 350 min 350
ENDIF
; default 4 cylinder "D"
IF PARAMETER=13 and variable=13 THEN
sound tda1052,(20,40,20,40)
;4 cylinder
offset=10
midpoint=10
cyl_count=4
ex_pitch=133
ex_dur=3
shutdown_cyl=4
;shutdown_tmr=
start_loop=5
midpoint=295
tickover=18000
topspeed=75
revrange=450
stophalt=30000
rate=3500
timeout=30
start_loop=5
timeout=cyl_count+1
limit=tickover-topspeed
ENDIF
; "E"
IF PARAMETER=14 and variable=14 THEN
sound tda1052,(20,50,20,50)
; 6 cylinder
offset=10
midpoint=10
cyl_count=6
ex_pitch=133
ex_dur=3
shutdown_cyl=6
;shutdown_tmr=
start_loop=5
midpoint=295
tickover=18000
topspeed=75
revrange=450
stophalt=30000
rate=3500
timeout=30
start_loop=5
timeout=cyl_count+1
limit=tickover-topspeed
ENDIF
;reset to default "F"
IF PARAMETER=15 and variable=15 THEN
sound tda1052,(20,60,20,60)
offset=10
midpoint=10
cyl_count=9
ex_pitch=133
ex_dur=3
shutdown_cyl=9
;shutdown_tmr=
start_loop=5
midpoint=295
tickover=18000
topspeed=75
revrange=450
stophalt=30000
rate=3500
timeout=30
start_loop=5
timeout=cyl_count+1
limit=tickover-topspeed
ENDIF
sound tda1052,(50,50)
SKIPSETUP:
'get throttle demand from receiver at "b.6" and assign it to the variable 'speed'
pulsin b.6,1,speed
lo=midpoint-offset
hi=midpoint+offset
if speed > lo and speed < hi then high ledy:else:low ledy:endif
if speed > lo and speed < hi then
speed=0
goto setup2
endif
if speed>hi then ; forward
speed=speed-midpoint;hi
else ;reverse
speed=midpoint-speed;lo-speed
end if
setup2:
;guard against over-range
if speed>120 then
speed=120
end if
'quantise speed into 25 values for lookup table
speed=speed/4
demand=speed*2
'MAIN LOOP
'ENGINE RUNNING
'process speed demand value and assign value to 'pawse'
pawse=demand*revrange max limit 'set spread of speed, limiting if necessary
pawse=tickover-pawse 'numerical sense of pawse is inversely related
pawse=pawse / 5
;pawse=pawse-90
'to speed (see above)
'generate a cycle of engine sound
for cownt=0 to cyl_count 'for each cylinder in turn
pause 2
sound tda1052,(ex_pitch,ex_dur) 'make exhaust noise
pulsout ledg,pawse 'silence until next cylinder fires
next
if pb1=1 and pb2=1 then goto exitsetup
goto setup1
exitsetup:
write 0,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31,b32,b33,b34,b35,b36,b37,b38,b39,b40,b41
sound tda1052,(50,40,30,20)
do
toggle ledw
pause 50
loop while pb1=1 or pb2=1
low ledw
goto main