Model Train sound and light program advice

GAP

Senior Member
I have written a program that creates hissing and chuffing sounds plus controls the directional lighting for my garden railway loco.
All runs OK except that there is a noticeable quiet period in the sounds when the loco is running.
I have the sounds and lights running in parallel on an 08M2 chip and I am wondering if the 2 programs are conflicting causing the problem.
I have copied the program (see below) and any advice on how to overcome the problem without having to resort to the use of 2 08M2 chips will be appreciated but if that is the only way it will not be an issue except that using 2 chips for such such simple task may be a waste.

Code:
[color=Green]'**** Two programs running in parallel Shay Sound Card MK3 and Shay Lights****

'MK3 added Crawl and Shunt steps to eliminate the sudden changes in the chuff beat at the specific throttle setting transition point present in MK1 and 2.
'Lights added directional Light control.

'########## NOTE; The lights and sound running together give a noticable blank sound space it may be better to split the 2 programs and use 2 seperate 08M2 1 to run sound and 1 to run lights unless a 14M2 can be configured to act as 2 seperate PICAXE chips.#######  
'The sound card should fit in the ashpan with the speaker and the light chip with the motor controller[/color]



[color=Blue]SYMBOL [/color][color=Black]soundpin[/color][color=DarkCyan]=[/color][color=Blue]C.0[/color]

[color=Black]START:[/color]

[color=Blue]pulsin C.3[/color][color=Black],[/color][color=Navy]1[/color][color=Black],[/color][color=Purple]b1 [/color][color=Green]'Look at the input pin. RX pulse on leg 4 and put into variable b1. Read RX pulses going to SyRen10A input.

'Stopped[/color]
[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]146 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]154 [/color][color=Blue]then goto [/color][color=Black]Hiss [/color][color=Green]'pulses between 1.46mS and 1.54ms the train is stopped
 
'Forward[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]155 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]165 [/color][color=Blue]then goto [/color][color=Black]crawl [/color][color=Green]'pulses between 1.55mS and 1.65mS the train is moving crawling forward slowly[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]166 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]175 [/color][color=Blue]then goto [/color][color=Black]shunt [/color][color=Green]'pulses between 1.66mS and 1.75mS the train is moving forward at shunt speed[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]176 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]185 [/color][color=Blue]then goto [/color][color=Black]slow [/color][color=Green]'pulses between 1.76mS and 1.85mS the train is moving forward slowly[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]186 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]195 [/color][color=Blue]then goto [/color][color=Black]medium [/color][color=Green]'pulses between 1.86mS and 1.95mS the train is moving forward at medium speed[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]196 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]200 [/color][color=Blue]then goto [/color][color=Black]topspeed [/color][color=Green]'pulses between 1.96mS and 2.00mS the train is moving forward at top speed 

'Reverse[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]147 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]136 [/color][color=Blue]then goto [/color][color=Black]crawl [/color][color=Green]'pulses between 1.47mS and 1.36mS the train is moving crawling forward slowly[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]135 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]125 [/color][color=Blue]then goto [/color][color=Black]shunt [/color][color=Green]'pulses between 1.35mS and 1.25mS the train is moving forward at shunt speed[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]126 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]115 [/color][color=Blue]then goto [/color][color=Black]slow [/color][color=Green]'pulses between 1.26mS and 1.15mS the train is moving in reverse slowly[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]114 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]105 [/color][color=Blue]then goto [/color][color=Black]medium [/color][color=Green]'pulses between 1.14mS and 1.05mS the train is moving in reverse at medium speed[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]104 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]100 [/color][color=Blue]then goto [/color][color=Black]topspeed [/color][color=Green]'pulses between 1.04mS and 1.00mS the train is moving in reverse at top speed [/color]

[color=Blue]Goto [/color][color=Black]START[/color]

[color=Green]'Chuff beats[/color]
[color=Black]Hiss:[/color]
[color=Blue]SOUND [/color][color=Black]soundpin,[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]125 [/color][color=Blue]) [/color][color=Green]'constant hiss with slow chuff[/color]
[color=Blue]Goto [/color][color=Black]START

crawl:[/color]
[color=Blue]SOUND [/color][color=Black]soundpin,[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]20[/color][color=Blue])  [/color][color=Green]'crawl sound output[/color]
[color=Blue]Goto [/color][color=Black]START

shunt:[/color]
[color=Blue]SOUND [/color][color=Black]soundpin,[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]18[/color][color=Blue])  [/color][color=Green]' shunt sound output[/color]
[color=Blue]Goto [/color][color=Black]START

slow:[/color]
[color=Blue]SOUND [/color][color=Black]soundpin,[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]16[/color][color=Blue])  [/color][color=Green]'slow sound output[/color]
[color=Blue]Goto [/color][color=Black]START

medium:[/color]
[color=Blue]SOUND [/color][color=Black]soundpin,[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]14[/color][color=Blue])  [/color][color=Green]'medium sound output[/color]
[color=Blue]Goto [/color][color=Black]START

topspeed:[/color]
[color=Blue]SOUND [/color][color=Black]soundpin,[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]12[/color][color=Blue])  [/color][color=Green]'top speed sound output[/color]
[color=Blue]Goto [/color][color=Black]START[/color]

[color=Blue]START1:

pulsin C.3[/color][color=Black],[/color][color=Navy]1[/color][color=Black],[/color][color=Purple]b1 [/color][color=Green]'Look at the input pin. RX pulse on leg 4 and put into variable b1. Read RX pulses going to SyRen10A input.

'Lights - turn on Front or Rear LED light depending on stick position[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]146 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]154 [/color][color=Blue]then gosub [/color][color=Black]Lightsoff[/color]
[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]155 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]200 [/color][color=Blue]then gosub [/color][color=Black]Front[/color]
[color=Blue]If [/color][color=Purple]b1[/color][color=DarkCyan]=< [/color][color=Navy]146 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]100 [/color][color=Blue]then gosub [/color][color=Black]Rear

Lightsoff: [/color][color=Green]'Lights off at neutral stick position[/color]
[color=Blue]Low C.1
Low C.2
Goto [/color][color=Black]START1
Front:  [/color][color=Green]'Turns front light on and rear light off with stick in up position[/color]
[color=Blue]High C.1 
Low C.2
Goto [/color][color=Black]START1

Rear: [/color][color=Green]'Turns front light off and rear light on with stick in down position[/color]
[color=Blue]High C.2
Low C.1
Goto [/color][color=Black]START1[/color]
 

AllyCat

Senior Member
Hi,

The PICaxe (and most microcontrollers) can do only "one thing at a time", but the "multitasking" (multiple starts) tries to "hide" this by switching rapidly between the tasks. But unfortunately PULSIN is a "blocking" command which needs to be completed before any other tasks can execute. This means the PICaxe must wait for the pulse to start and then measure its width before any (more) sound can be generated. You haven't said how long is the (maximum) gap (space) between pulses, but I suspect it might be 20 ms?

First, "comment out" the PULSIN commands in your program and add a simple assignment (e.g. b1 = 100) to replace them. Does this solve the "quiet" period problem adequately? If not, then it might be possible to "speed up" the program a little, but you probably will need to use two 08M2s (or totally change the program structure).

If the sound is now "acceptable", then I can suggest two things to try:

1. Use only one PULSIN command (probably in the SOUNDs thread) and store the value (b1) for the other thread(s) to use.

2. Convert the "pulse" input waveform to an analogue value using a low-pass filter (perhaps 22k from the pulse to a PICaxe ADC input pin and 100 uF from the pin to ground). Pin C.3 is not an ADC input so you would need to change to (say) C.4. Replace the PULSIN with a READADC10 , c.4 , b1 and change all the speed testing values as appropriate (a quick fix might be to "scale" the b1 value by multiplication and/or divison). Note: I'm assuming the pulse is positive-going and relatively narrow compared to the gap (so the ADC value will fit within a single byte).

Cheers, Alan.
 

techElder

Well-known member
Not to be picking on you GAP, but I completely don't understand this rational that I read on the forum occasionally.

... except that using 2 chips for such such simple task may be a waste.
We may assume this to be a "simple" task, but from a hardware point-of-view it isn't really that simple. Actually, you've proven in your program that it isn't a simple task.

There are many dedicated sound devices out there that most folks wouldn't hesitate driving with a PICAXE. Guess what they are? Another chip for your system.

So, spend some time making the sounds that you want in an 08M2, and then figure out a way to use another 08M2 to drive it. Now you have more time and space to create a real controlling program without reducing the user experience while generating sound.
 

AllyCat

Senior Member
Hi,

Certainly SOUND is an application where (unwanted) delays can occur, so using a second processor may be an appropriate solution. In this case you might just put the "Start" program in one PICaxe and "Start1" in another.

However, I've taken a closer look at your program and the "Lights" section is relatively trivial (but it contains bugs) so a single 08M2 should be sufficient. The bugs are that the start1 and start code uses GOSUBs, but the "suboutines" are not exited with a RETURN, but with a GOTO Start{1} . With that construction you shouldn't be using GOSUBs, just GOTOs.

But IMHO you don't need to use multitasking at all:

Just use the "lights" loop in the program but after the two "Lights off" commands add the "stopped" sound test command. Then after the "front" lights commands add all the "Forward" sound tests, and after the "Rear" lights commands add all the "Reverse" sound tests. START1 simply becomes "main:" and change all the GOTO Starts and GOTO Start1s to GOTO main. Or preferably restructure it all with "real" GOSUBs and a DO ... LOOP.

If there are still undesirable pauses in the Sounds, then you will need to modify the PULSIN as mentioned in #2.

Cheers, Alan.
 

GAP

Senior Member
Tex,
A bit of background, I already use manufactured sound cards for some of my diesel locos and add a PICAXE to create inertia ie sound of engine revs increase before loco moves, this is a program written by someone else and not my creation.
In this case the PICAXE is receiving a signal from a 2.4GHz R/C receiver and creating the sounds dependent on the "throttle" setting of the transmitter, the sounds are then amplified and sent to a speaker so it is just a basic sound generator. Normally I would connect the LEDs across the motor inputs, orientated so that they light depending on direction sent to the motor, but this controller does not allow that to happen, my other ones are simple H Bridge outputs but I cannot use them in this situation because I am driving 3 motors wired in parallel and needed a higher current above what the H Bridge could supply.
What I was trying to say with the comment was that using an 08M2 just to switch 2 LED lights on and off may be a waste of hardware ie using 2 chips when 1 could do both jobs. A big consideration in my thinking is the limited space inside the loco there would be an advantage to have both functions on the same chip.

Alley Cat,
As a complete novice to all this I am not really up to speed on the ways of doing things with PICAXE eg difference between gosub and goto I usully use the error in editor to help me find the routine that works.
I am going to have a go at the suggestions in post #5 first up I think I can understand the suggestions.
Could you please give me an example or two of what you are suggesting (maybe a few bits of code ) to get me on my way?
 

AllyCat

Senior Member
Hi,

Not tested (or complete), but cutting, adapting and pasting some lines from your original code:

Code:
main:
   pulsin C.3,1,b1    'Look at the input pin.
   If b1 => 146 AND b1 =< 154 then goto Lightsoff
   If b1 => 155 AND b1 =< 200 then goto Front
   If b1 =< 146 AND b1 => 100 then goto Rear
   goto main      ' Only required in case all tests fail

Lightsoff:    'Lights off at neutral stick position
  Low C.1 : Low C.2
'Stopped
  goto Hiss     'pulses between 1.46mS and 1.54ms the train is stopped

Front:     'Turns front light on and rear light off with stick in up position
  High C.1  :  Low C.2
'Forward
  If b1 => 155 AND b1 =< 165 then goto crawl
  If b1 => 166 AND b1 =< 175 then goto shunt
  If b1 => 176 AND b1 =< 185 then goto slow 
  If b1 => 186 AND b1 =< 195 then goto medium 
  If b1 => 196 AND b1 =< 200 then goto topspeed 
  goto main   ' In case all tests fail

Rear: 'Turns front light off and rear light on with stick in down position
  High C.2  :  Low C.1
'Reverse
  If b1 =< 147 AND b1 => 136 then goto crawl 
  If b1 =< 135 AND b1 => 125 then goto shunt 
  If b1 =< 126 AND b1 => 115 then goto slow
  If b1 =< 114 AND b1 => 105 then goto medium 
  If b1 =< 104 AND b1 => 100 then goto topspeed  
  goto main    ' In case all tests fail

Hiss:
SOUND soundpin,(255,125 )   'constant hiss with slow chuff
Goto main

crawl:
SOUND soundpin,(255,20)  'crawl sound output
Goto main

' etc.......
Using GOSUBs, and RETURNs in place of the GOTO mains, would be better "structured" but actually rather slower! Also, with the GOTOs, a lot of the combined =< and => tests could be omitted.

Cheers, Alan.
 

AllyCat

Senior Member
Hi,

Not as well commented or as versatile as the original program, but the following (untested) should do much the same, and sufficiently quickly to show that a second PICaxe is really not needed: ;)

Code:
#picaxe 08m2
main:
   pulsin C.3,1,b1                                        ; Look at the input pin
   If b1 > 199 OR b1 < 101 then goto Error      ; Unexpected pulse width
   If b1 > 154 then goto Front                  ; Can't use "Forward" as it's a reserved word !
   If b1 < 146 then goto Rear                   ; Else "fall into" Lightsoff
Lightsoff:                                             ; Lights off at neutral stick position
  Low C.1 : Low C.2
'Stopped
  SOUND C.0,(255,125 )                           ; Constant hiss with slow chuff
  goto main
  
Front:                      ; Turn front light on and rear light off with stick in up position
  High C.1 : Low C.2
  b2 = b1                                      ; In case value in b1 is needed again in the program
noise:
  b2 = b2 - 145 / 10                    ; Calculate the sound range (formula not fully checked)
  b2 = 22 - b2 - b2                      ; Calculate the sound pitch (same calculation as 11 - b1 * 2)
  SOUND C.0,(255,b2)  
  goto main	 
Rear:                       ; Turn front light off and rear light on with stick in down position
  High C.2 : Low C.1
  b2 = 155 - b1                          ; Convert "reverse" speed to a forward speed
  goto noise
Error:  ; Invalid speed indication
  goto main                                 ; Try again (and/or do whatever else might be wanted)
Or, the increasing sound pitch could be made smoother by doubling the number of steps, i.e. by changing the "10" to a "5" after the "noise" label and removing one "-b2" in the subsequent line.

Cheers, Alan.
 

GAP

Senior Member
Hi,

Not tested (or complete), but cutting, adapting and pasting some lines from your original code:

Code:
main:
   pulsin C.3,1,b1    'Look at the input pin.
   If b1 => 146 AND b1 =< 154 then goto Lightsoff
   If b1 => 155 AND b1 =< 200 then goto Front
   If b1 =< 146 AND b1 => 100 then goto Rear
   goto main      ' Only required in case all tests fail

Lightsoff:    'Lights off at neutral stick position
  Low C.1 : Low C.2
'Stopped
  goto Hiss     'pulses between 1.46mS and 1.54ms the train is stopped

Front:     'Turns front light on and rear light off with stick in up position
  High C.1  :  Low C.2
'Forward
  If b1 => 155 AND b1 =< 165 then goto crawl
  If b1 => 166 AND b1 =< 175 then goto shunt
  If b1 => 176 AND b1 =< 185 then goto slow 
  If b1 => 186 AND b1 =< 195 then goto medium 
  If b1 => 196 AND b1 =< 200 then goto topspeed 
  goto main   ' In case all tests fail

Rear: 'Turns front light off and rear light on with stick in down position
  High C.2  :  Low C.1
'Reverse
  If b1 =< 147 AND b1 => 136 then goto crawl 
  If b1 =< 135 AND b1 => 125 then goto shunt 
  If b1 =< 126 AND b1 => 115 then goto slow
  If b1 =< 114 AND b1 => 105 then goto medium 
  If b1 =< 104 AND b1 => 100 then goto topspeed  
  goto main    ' In case all tests fail

Hiss:
SOUND soundpin,(255,125 )   'constant hiss with slow chuff
Goto main

crawl:
SOUND soundpin,(255,20)  'crawl sound output
Goto main

' etc.......
Using GOSUBs, and RETURNs in place of the GOTO mains, would be better "structured" but actually rather slower! Also, with the GOTOs, a lot of the combined =< and => tests could be omitted.

Cheers, Alan.
Had a play around with this program and came up with this.

Code:
[color=Green]'Modified from Sounds and lights as suggested by Alan (Alley Cat)from forum 
'Pulse width values derived from oscilliscope measurements of receiver signal output
'Controls lights as well as sound[/color]

[color=Blue]SYMBOL soundpin[/color][color=DarkCyan]=[/color][color=Blue]C.0[/color]

[color=Black]main:
   [/color][color=Blue]pulsin C.3[/color][color=Black],[/color][color=Navy]1[/color][color=Black],[/color][color=Purple]b1    [/color][color=Green]'Look at the input pin.
   [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]139 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]141 [/color][color=Blue]then goto [/color][color=Black]Lightsoff
   [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]142 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]175 [/color][color=Blue]then goto [/color][color=Black]Front
   [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]138 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]112 [/color][color=Blue]then goto [/color][color=Black]Rear
   [/color][color=Blue]goto [/color][color=Black]main      [/color][color=Green]' Only required in case all tests fail[/color]

[color=Black]Lightsoff:    [/color][color=Green]'Lights off at neutral stick position
  [/color][color=Blue]Low C.1 [/color][color=Black]: [/color][color=Blue]Low C.2[/color]
[color=Green]'Stopped
  [/color][color=Blue]goto [/color][color=Black]Hiss     [/color][color=Green]'pulses between 1.39mS and 1.41ms the train is stopped[/color]

[color=Black]Front:     [/color][color=Green]'Turns front light on and rear light off with stick in up position
  [/color][color=Blue]High C.1  [/color][color=Black]:  [/color][color=Blue]Low C.2[/color]
[color=Green]'Forward
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]142 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]154 [/color][color=Blue]then goto [/color][color=Black]crawl
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]155 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]160 [/color][color=Blue]then goto [/color][color=Black]shunt
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]161 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]165 [/color][color=Blue]then goto [/color][color=Black]slow 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]166 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]170 [/color][color=Blue]then goto [/color][color=Black]medium 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]171 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]175 [/color][color=Blue]then goto [/color][color=Black]topspeed 
  [/color][color=Blue]goto [/color][color=Black]main   [/color][color=Green]' In case all tests fail[/color]

[color=Black]Rear:       [/color][color=Green]'Turns front light off and rear light on with stick in down position
  [/color][color=Blue]High C.2  [/color][color=Black]:  [/color][color=Blue]Low C.1[/color]
[color=Green]'Reverse
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]141 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]132 [/color][color=Blue]then goto [/color][color=Black]crawl 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]133 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]128 [/color][color=Blue]then goto [/color][color=Black]shunt 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]127 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]123 [/color][color=Blue]then goto [/color][color=Black]slow
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]122 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]117 [/color][color=Blue]then goto [/color][color=Black]medium 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]116 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]112 [/color][color=Blue]then goto [/color][color=Black]topspeed  
  [/color][color=Blue]goto [/color][color=Black]main    [/color][color=Green]' In case all tests fail

'Chuff beats[/color]
[color=Black]Hiss:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]200 [/color][color=Blue]) [/color][color=Green]'constant hiss with slow chuff[/color]
[color=Blue]Goto [/color][color=Black]main 

crawl:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]20[/color][color=Blue])  [/color][color=Green]'crawl sound output[/color]
[color=Blue]Goto [/color][color=Black]main

shunt:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]18[/color][color=Blue])  [/color][color=Green]' shunt sound output[/color]
[color=Blue]Goto [/color][color=Black]main

slow:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]16[/color][color=Blue])  [/color][color=Green]'slow sound output[/color]
[color=Blue]Goto [/color][color=Black]main

medium:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]14[/color][color=Blue])  [/color][color=Green]'medium sound output[/color]
[color=Blue]Goto [/color][color=Black]main

topspeed:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]12[/color][color=Blue])  [/color][color=Green]'top speed sound output[/color]
[color=Blue]Goto [/color][color=Black]main[/color]
I ran it in the train and it works well but has a few small bugs when compared to what the train is doing and the sound is doing but they are easy to overcome I think.

The sound in PICAXE at 255 gives a reasonably good hiss when stopped although it is a bit harsh, I tried to changing to other settings but they were not good at all for what I wanted, I might have to play with the amplifier to soften the hiss a bit.

Thanks for the help.
 

GAP

Senior Member
' Alternate sound and lights program provided by Alan (Alley Cat)from forum

#picaxe 08m2
main:
pulsin C.3,1,b1 ; Look at the input pin
If b1 > 199 OR b1 < 101 then goto Error ; Unexpected pulse width
If b1 > 145 then goto Front ; Can't use "Forward" as it's a reserved word !
If b1 < 139 then goto Rear ; Else "fall into" Lightsoff
Lightsoff: ; Lights off at neutral stick position
Low C.1 : Low C.2
'Stopped
SOUND C.0,(200,125 ) ; Constant hiss with slow chuff
goto main

Front: ; Turn front light on and rear light off with stick in up position
High C.1 : Low C.2
b2 = b1 ; In case value in b1 is needed again in the program
noise:
b2 = b2 - 145 / 7 ; Calculate the sound range (formula not fully checked)
b2 = 22 - b2 - b2 ; Calculate the sound pitch (same calculation as 11 - b1 * 2)
SOUND C.0,(255,b2)
goto main
Rear: ; Turn front light off and rear light on with stick in down position
High C.2 : Low C.1
b2 = 140 - b1 ; Convert "reverse" speed to a forward speed
goto noise
Error: ; Invalid speed indication
goto main ; Try again (and/or do whatever else might be wanted)


I gave this a go at first and it worked well I then went to the second suggestion and played with that.

"Or, the increasing sound pitch could be made smoother by doubling the number of steps, i.e. by changing the "10" to a "5" after the "noise" label and removing one "-b2" in the subsequent line."

As suggested above I might have a bash at changing the pitch to see if I can improve it.
 

GAP

Senior Member
Had a play around with this program and came up with this.

Code:
[color=Green]'Modified from Sounds and lights as suggested by Alan (Alley Cat)from forum 
'Pulse width values derived from oscilliscope measurements of receiver signal output
'Controls lights as well as sound[/color]

[color=Blue]SYMBOL soundpin[/color][color=DarkCyan]=[/color][color=Blue]C.0[/color]

[color=Black]main:
   [/color][color=Blue]pulsin C.3[/color][color=Black],[/color][color=Navy]1[/color][color=Black],[/color][color=Purple]b1    [/color][color=Green]'Look at the input pin.
   [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]139 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]141 [/color][color=Blue]then goto [/color][color=Black]Lightsoff
   [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]142 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]175 [/color][color=Blue]then goto [/color][color=Black]Front
   [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]138 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]112 [/color][color=Blue]then goto [/color][color=Black]Rear
   [/color][color=Blue]goto [/color][color=Black]main      [/color][color=Green]' Only required in case all tests fail[/color]

[color=Black]Lightsoff:    [/color][color=Green]'Lights off at neutral stick position
  [/color][color=Blue]Low C.1 [/color][color=Black]: [/color][color=Blue]Low C.2[/color]
[color=Green]'Stopped
  [/color][color=Blue]goto [/color][color=Black]Hiss     [/color][color=Green]'pulses between 1.39mS and 1.41ms the train is stopped[/color]

[color=Black]Front:     [/color][color=Green]'Turns front light on and rear light off with stick in up position
  [/color][color=Blue]High C.1  [/color][color=Black]:  [/color][color=Blue]Low C.2[/color]
[color=Green]'Forward
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]142 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]154 [/color][color=Blue]then goto [/color][color=Black]crawl
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]155 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]160 [/color][color=Blue]then goto [/color][color=Black]shunt
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]161 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]165 [/color][color=Blue]then goto [/color][color=Black]slow 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]166 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]170 [/color][color=Blue]then goto [/color][color=Black]medium 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]171 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]175 [/color][color=Blue]then goto [/color][color=Black]topspeed 
  [/color][color=Blue]goto [/color][color=Black]main   [/color][color=Green]' In case all tests fail[/color]

[color=Black]Rear:       [/color][color=Green]'Turns front light off and rear light on with stick in down position
  [/color][color=Blue]High C.2  [/color][color=Black]:  [/color][color=Blue]Low C.1[/color]
[color=Green]'Reverse
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]141 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]132 [/color][color=Blue]then goto [/color][color=Black]crawl 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]133 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]128 [/color][color=Blue]then goto [/color][color=Black]shunt 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]127 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]123 [/color][color=Blue]then goto [/color][color=Black]slow
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]122 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]117 [/color][color=Blue]then goto [/color][color=Black]medium 
  [/color][color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]116 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]112 [/color][color=Blue]then goto [/color][color=Black]topspeed  
  [/color][color=Blue]goto [/color][color=Black]main    [/color][color=Green]' In case all tests fail

'Chuff beats[/color]
[color=Black]Hiss:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]200 [/color][color=Blue]) [/color][color=Green]'constant hiss with slow chuff[/color]
[color=Blue]Goto [/color][color=Black]main 

crawl:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]20[/color][color=Blue])  [/color][color=Green]'crawl sound output[/color]
[color=Blue]Goto [/color][color=Black]main

shunt:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]18[/color][color=Blue])  [/color][color=Green]' shunt sound output[/color]
[color=Blue]Goto [/color][color=Black]main

slow:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]16[/color][color=Blue])  [/color][color=Green]'slow sound output[/color]
[color=Blue]Goto [/color][color=Black]main

medium:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]14[/color][color=Blue])  [/color][color=Green]'medium sound output[/color]
[color=Blue]Goto [/color][color=Black]main

topspeed:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]12[/color][color=Blue])  [/color][color=Green]'top speed sound output[/color]
[color=Blue]Goto [/color][color=Black]main[/color]
I ran it in the train and it works well but has a few small bugs when compared to what the train is doing and the sound is doing but they are easy to overcome I think.

The sound in PICAXE at 255 gives a reasonably good hiss when stopped although it is a bit harsh, I tried to changing to other settings but they were not good at all for what I wanted, I might have to play with the amplifier to soften the hiss a bit.

Thanks for the help.
Overcame the bugs just had to make the numbers match the throttle settings, sorted the amplifier I was running it off 12V and that was overdriving the speaker causing distortion I am now running it off 5V and it sounds really good.
Thank you again for the help.
 
Top