help with input from a R/C rx servo output driving picaxe

GAP

Senior Member
OK here I go

I want to control a relay ON/Off using a picaxe that responds to the outputs of a 2.4GHz radio control RX. Basically an R/C switch.

I want to use this to replace a servo that moves a mechanical switch that is used to change the direction of a DC motor in a large scale model train loco.

The pulses out from the RX that correspond to a stick on the TX and are in the count range of 75 (left), 150 (centre), 225 (right), being the pulse width, in mS, of the input signal that drives a servo.

The stick is self-centering so when released returns to the centre position and outputs a 150 count till moved again.

The logic goes like this;
1. Look at the input pin.
2. If pulses = 175-225 then output = 1 - stick moved right
3. If pulses = 125-75 then output = 0 - stick moved left
4. If pulses = 125-175 then do nothing – stick in centre
5. If output = 1 and pulses = 175-225 then do nothing
6. If output = 0 and pulses = 125-75 then do nothing

The pulse number ranges are adjustable, but these are chosen so that if the stick is bumped it does not cause a sudden change of direction causing damage to the loco

My question is “where do I start with this”?

I have looked at the manuals and have found servo commands that control a servo eg “servpos” but nothing about a servo signal as an input.

I have looked at “pulsein” but cannot see how it would fit my need.

At present I am staring at a blank page in editor with the first line of “start” being the only entry.

I do have a big issue of being able to convert normal speak logic into picaxe speak.

I really need a push in the right direction and a bit of mentoring to get me on my way and I thought a simple task like this would be just what I need. I do not want a solution handed to me as I will not learn from that.

I do have programs written by others and can break them down but have never gone the other way and started from scratch.
 

xtech007

Senior Member
Pulsin is great for this!

First, what Picaxe are u planning to use?
In the past I have gather the data out of a rc receiver and onto the picaxe 14M2 to do stuff!
Here is a clue:

Pulsin C.3,0,W1
If W1 <125 then high redled
If W1 >130 then low redled
Hope this helps!
 

hippy

Technical Support
Staff member
There are other posts on this subject on the forum but basically you need to use PULSIN to measure the pulse width into a variable and a SELECT-CASE is probably the best thing to use to handle what value that holds.

PULSIN in measures in 10us units so a 1.50ms 'centred' pulse would be 150. If you have the dead-band between 1.25ms and 1.75ms, something like ...

Code:
Do
  PulsIn RX_PIN, 1, b0
  Select Case b0
     Case < 125 : Gosub MovedLeft
     Case > 175 : Gosub MovedRight
  End Select
Loop
 

GAP

Senior Member
xtech I am planning on using a 08M2.

Hippy thank you for the info, now to research Case, how do I find the other posts?

Another question;
I have been given a program that uses a 08M2 to generate sound in sync with the motor voltage which is working well in a loco. Is it possible to run this type of program in parallel with the sound program in maybe a 14M2,thus eliminating having to use 2 picaxe chips or an I biting off more than I can chew?
 

hippy

Technical Support
Staff member
There is a forum search option at the top right of the page.

There is also Google and other search engines. With Google one can add site:www.picaxeforum.co.uk to the search text to have just the forum searched.
 

xtech007

Senior Member
Generatig sound.

The sound is generated by the picaxe or a sound module?
If it's a module,what are the specs?
Some MP3 modules would need a high or low signal to control the volume and move to the next track.

A picaxe can easily do that!
 

GAP

Senior Member
The sound is generated by the picaxe or a sound module?
If it's a module,what are the specs?
Some MP3 modules would need a high or low signal to control the volume and move to the next track.

A picaxe can easily do that!
The sound is generated by the picaxe.
The motor voltage is measured by READADC10
A simple chuff is white noise produced in the picaxe and sound is used to switch it on and off.
 

JimPerry

Senior Member
xtech I am planning on using a 08M2.

Hippy thank you for the info, now to research Case, how do I find the other posts?

Another question;
I have been given a program that uses a 08M2 to generate sound in sync with the motor voltage which is working well in a loco. Is it possible to run this type of program in parallel with the sound program in maybe a 14M2,thus eliminating having to use 2 picaxe chips or an I biting off more than I can chew?
The program that works should be posted.. a second routine using SOUND would probably block the first program effect :confused:
 

GAP

Senior Member
The program that works should be posted.. a second routine using SOUND would probably block the first program effect :confused:

This is the original program that is used for the loco, the only changes were the "INV" command is used (this is just a function of which ESC is used), and the "speedconstant' to match the wheel diameter and connecting rod action of the loco. I have forgotten to mention this is for a steam engine sorry.

'a voltage on pin1 represents the speed of the loco.
'a 'white noise' chuff sound is produced on pin0 and pin2, which must be amplified externally.
'suitable for 08M and 08M2 at 4 MHz
'pin0 is sound output 'pin1 is speed voltage input 0V=stopped, +5V=max speed
'if speed volts are inverted ie 5V=stopped and 0V=max speed, then
'invoke line INVspd: 'pin2 is PWM output voltage to control volume and shape the chuff
'define variables 'w0=b0,b1 not used SYMBOL speed=w1
'will be 0-maxspeedcounts in prog after reading in and processing
SYMBOL chufftime=b4
'length of chuff in 12 ms periods (use in SOUND command)
'=b5
SYMBOL offtime=w3
'in ms. need a word as can be >255 at slow speed SYMBOL oldspeed1=b10 SYMBOL oldspeed2=b11 SYMBOL PWMvolume=w6
'0=max volume, 255 =min volume
'has to be a word, but using 15kHz PWM, max value is 255
'define constants
SYMBOL maxspeedcounts=100
'max speed will be represented as this in prog.
SYMBOL stoppedcounts=5
'define less than this as
'stopped'= about 5% of maxspeedcounts SYMBOL maxspeedIN=255
'see notes at end to calc this value
SYMBOL speedconstant=3000
'adjust for 4 chuffs per wheel revolution 'depends on maxspeedcounts too.
'determine these by experiment.....
SYMBOL loudVol=150
'chuff volume when accelerating
SYMBOL offVol=250
'hiss volume when stopped and between chuffs
SYMBOL midVol=210 'chuff volume when slowing
'--------------------------------------------------------------
start:
oldspeed2=oldspeed1 oldspeed1=speed
READADC10 1, speed 'now adjust for actual input voltage and normalise to a specified maximum (maxspeedcounts) speed=speed/4*maxspeedcounts/maxspeedIN MAX maxspeedcounts
'INVspd: speed=maxspeedcounts-speed 'only use if speed voltage is inverted
'*now speed is always represented by 0 when stopped and maxspeedcounts at max input voltage*
if speed<stoppedcounts then stopped L20:
if OLDspeed1=0 and speed>stoppedcounts then loud L35:
if speed>=OLDspeed1 AND OLDspeed1>OLDspeed2 then loud L40:
if speed<OLDspeed1 AND OLDspeed1<OLDspeed2 then soft 'otherwise just leave as it was
chuff:
offtime=speedconstant/speed '(in ms) and chufftime=offtime/12 'in 12ms counts offtime=offtime/12 'needs to be in 12ms counts now. 'start chuff shape charging capacitor PWMOUT 2,63, PWMvolume 'at 15.7kHz, 255=min vol, 0=max vol
SOUND 0, (255, chufftime) 'start hiss sound
PWMOUT 2,63,offvol 'start to decay chuff SOUND 0, (255, offtime) goto start
stopped: speed=0 oldspeed1=0 PWMOUT 2,63,offvol 'soft volume sound 0, (255, 100) '3/4 sec of hiss goto start
loud: PWMvolume=loudVol goto chuff soft: PWMvolume=midVol goto chuff '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'NOTES: 'the max volts applied to pin1 for the speed MUST NOT EXCEED the picaxe supply voltage. 'Assuming that is 5V, then a voltage divider on pin1 must reduce the motor volts to 'less than or equal to 5V at the MAXIMUM SUPPLY VOLTAGE ! 'It would be prudent to reduce the voltage to a bit less than 5V
'if max volts on pin1 are less than picaxe volts (5V) then you must determine the value 'for maxspeedIN as follows: 'using a multimeter measure the picaxe supply volts (Vp) and 'the voltage on pin1 with max supply voltage (Vm1). Then 'maxspeedIN= Vm1/Vp*256. (round the result UP, and must max of 256.) 'example, you measure Vp=4.28V and Vm1=2.65V, then enter SYMBOL maxspeedIN=15

The program used to drive the relay will only need to do anything if the direction of the loco requires changing hence my question about using one chip instead of two.
If it is not possible the 2 chips it is.
 

GAP

Senior Member
Code:
[color=Green]'Centre = b2[/color]

[color=Black]START: [/color]
[color=Green]'Centre=150      'Set centre to 1.50mS[/color]
[color=Blue]output c.1 [/color][color=Green]'Set C.1 as an outout [/color]
[color=Blue]high C.1[/color][color=Green]'relay set to forward[/color]
[color=Blue]pulsin C.3[/color][color=Black],[/color][color=Navy]1[/color][color=Black],[/color][color=Purple]b2 [/color][color=Green]'Look at the input pin. RX pulse on pin 4 and putinto variable b2[/color]
[color=Blue]Select Case [/color][color=Purple]b2
     [/color][color=Blue]Case [/color][color=DarkCyan]< [/color][color=Navy]125 [/color][color=Black]: [/color][color=Blue]Goto [/color][color=Black]BACKsymbol
     [/color][color=Blue]Case [/color][color=DarkCyan]> [/color][color=Navy]175 [/color][color=Black]: [/color][color=Blue]Goto [/color][color=Black]FWD
     [/color][color=Blue]Case [/color][color=DarkCyan]>[/color][color=Navy]125 [/color][color=DarkCyan]<[/color][color=Navy]175 [/color][color=Black]: [/color][color=Blue]Goto [/color][color=Black]CNTR
  [/color][color=Blue]End Select[/color]
[color=Green]'Loop

'if b2 < 125 then goto BACK 'If pulses = 130 to 75 then output = 0 - stick moved left direction b2 is less than 130
'if b2 > 175 then goto FWD 'If pulses = 175 to 225 then output = 1 - stick moved right direction b2 more than 170
' b2 >125 then goto CNTR 'if output = 0 and pulses = 125-75 then do nothing
' b2 <175 then goto CNTR 'If output = 1 and pulses = 175-225 then do nothing
'goto START 'jump to START and await next RX pulse[/color]

[color=Black]BACK:[/color]
[color=Blue]low C.1 [/color][color=Green]'+0V on chip pin 6 connected to relay[/color]
[color=Blue]goto [/color][color=Black]START

FWD:[/color]
[color=Blue]high C.1 [/color][color=Green]'+5V on chip pin 6 connected to relay[/color]
[color=Blue]goto [/color][color=Black]START 


CNTR:[/color]
[color=Green]'If pulses = 125 to 175 then do nothing stick returned to centre - how does this happen?[/color]
I have gotten this far.
There are 2 options that both work in the simulation.
One option is is commented out.
I am stuck with how to get the program to do nothing when the stick is in the centre.
I am using a self centring stick ie push it right and release and it springs back to the centre and vice versa.
I can figure out how to change the output but when the stick goes back to centre I want the output not to change.
I have thought about some sort of pause but I want an indefinite setting till the input changes.

there are 2 syntax error in line 1 and in line 11 which are baffling me. what I am trying to do is set the the default to forward but want the centre to be a reference.

Hopefully this is all clear if not please put requests into simple questions that I can reply to.

Another point is that I cannot find out how to simulate pulse in in the simulator so I can check my code any help there appreciated.

This is a learning exercise so please do not provide a solution right away just hints at the moment when I forced to give up then a solution would be appreciated.
 

hippy

Technical Support
Staff member
I am stuck with how to get the program to do nothing when the stick is in the centre.
The code in post #3 ( repeated below ) will do nothing if the stick is centred, is between 125 and 175. The outputs will remain however they were set when the stick was below 125 or above 175 -

Code:
Do
  PulsIn RX_PIN, 1, b0
  Select Case b0
     Case < 125 : Gosub MovedLeft
     Case > 175 : Gosub MovedRight
  End Select
Loop
 

GAP

Senior Member
The code in post #3 ( repeated below ) will do nothing if the stick is centred, is between 125 and 175. The outputs will remain however they were set when the stick was below 125 or above 175 -

Code:
Do
  PulsIn RX_PIN, 1, b0
  Select Case b0
     Case < 125 : Gosub MovedLeft
     Case > 175 : Gosub MovedRight
  End Select
Loop
Thank you I did not realise it would be that simple.

Another question is how do I simulate the pulses in in a simulation so I can test this, or should I just build a circuit?
 

hippy

Technical Support
Staff member
Another question is how do I simulate the pulses in in a simulation so I can test this, or should I just build a circuit?
Within PE6, select the "Values" tab of the Simulation panel ( usually bottom left of the screen ). Then edit the value in the "Word" column for the pin you are reading the PULSIN from.
 

GAP

Senior Member
A whole new project that I am working on, utilising the advice from previous posts, but I am having an issue with it.

I am feeding in pulses from a 2.4Ghz radio Control receiver into pin C.3 of the Picaxe (checked with a CRO).

As the pulse width varies the sound doesn't change, all I am hearing is a constant hiss.


This is the program;
Code:
'Sound Card program that reads pulses from a 2.4Hz radio Control receiver and produces sounds for a Shay geared steam loco with a 3 cylinder steam motor.

'***SOUND is taken from a regular steam loco and will need to be modified to simulate a Shay once the program has been verified***.



SYMBOL Pulsecount = w1
SYMBOL soundpin=C.0

Pause 500  'Allows Rx to stabilise

START:

pulsin C.3,1,w1 'Look at the input pin. RX pulse on leg 4 and put into variable w1. Read RX pulses going to SyRen10A input.

Select Case w1

'Stopped
  Case >146: goto HISS:
  Case <152: goto HISS: 'pulses between 1.46mS and 1.52mS, Stick is set to centre so loco is stopped then constant hiss.

'FORWARD

  Case>153:goto slow
  Case<175:goto slow   'fast speed chuffs shorter ie more per shaft rev

  Case>176:goto fast
  Case<200:goto fast   'slow speed chuffs shorter ie less per shaft rev


'REVERSE

  Case>147:goto slow
  Case<130:goto slow   'fast speed chuffs shorter ie more per shaft rev

  Case>129:goto fast
  Case<100:goto fast   'slow speed chuffs shorter ie less per shaft rev

End Select

HISS:
SOUND soundpin,(255,150) 'constant hiss

slow:
SOUND soundpin,(255,20)  'slow sound output

fast:
SOUND soundpin,(225,50)  'fast sound output

goto START:
This the circuit;

Shay Steam Sound using Pulses in.jpg


Any help will be appreciated, all I ask is with a possible solution an explanation of where I went wrong accompanies it.

This is a learning exercise as well as a practical something that will be used exercise.
 

hippy

Technical Support
Staff member
It could be that you are reading the wrong pulse polarity, but more likely because your code drops through into other parts -

Code:
End Select

HISS:
SOUND soundpin,(255,150) 'constant hiss

slow:
SOUND soundpin,(255,20)  'slow sound output

fast:
SOUND soundpin,(225,50)  'fast sound output

goto START:
You probably want more 'goto START' commands to control program flow better.

Also ...

Code:
  Case<200:goto fast
  Case<130:goto slow
You will never reach that second case because the < 200 condition will have been met; 129 will be treated as fast rather than the slow you intended.

Use the Simulation option of PE6 and you will be able to follow the actual program execution flow.
 

GAP

Senior Member
It could be that you are reading the wrong pulse polarity, but more likely because your code drops through into other parts -
The pulse is a positive going leading edge with the trailing edge that moves with width instructions.
Code:
End Select

HISS:
SOUND soundpin,(255,150) 'constant hiss

slow:
SOUND soundpin,(255,20)  'slow sound output

fast:
SOUND soundpin,(225,50)  'fast sound output

goto START:
You probably want more 'goto START' commands to control program flow better.
goto START between each command?

Also ...

Code:
  Case<200:goto fast
  Case<130:goto slow
You will never reach that second case because the < 200 condition will have been met; 129 will be treated as fast rather than the slow you intended.
Could I please get an explanation of this?
What I am trying to achieve is;
When the transmitter stick is centred (pulse width between 146 & 152 mS) the motors are stopped and a hiss is played.
When the transmitter stick is halfway between centred and fully up (pulse width between 153 & 175 mS) the motors are running slowly and a "chuff", which is the hiss turned on and off rapidly, is played.
When the transmitter stick is fully up (pulse width between 176 & 200 mS) the motors are running fast and a "chuff", which is the hiss turned on and off not as rapidly as when the motors are running slow, is played.

When the stick is moved from centre to halfway fully down and then fully down the motors are running in a reverse direction with the same sounds being played at the respective positions.

So I am guessing that I need some way to differentiate the direction am I correct?

Use the Simulation option of PE6 and you will be able to follow the actual program execution flow.


I have PE6 installed onto my hobby computer ,running XP, but the "simulate" buttons are greyed out and do not work, I will have to resorted to PE5 to run the simulation.

I will make some changes and see how I go, but I expect more questions will be forthcoming.
 
Last edited by a moderator:

GAP

Senior Member
The pulse is a positive going leading edge with the trailing edge that moves with width instructions.

goto START between each command?


Could I please get an explanation of this?
What I am trying to achieve is;
When the transmitter stick is centred (pulse width between 146 & 152 mS) the motors are stopped and a hiss is played.
When the transmitter stick is halfway between centred and fully up (pulse width between 153 & 175 mS) the motors are running slowly and a "chuff", which is the hiss turned on and off rapidly, is played.
When the transmitter stick is fully up (pulse width between 176 & 200 mS) the motors are running fast and a "chuff", which is the hiss turned on and off not as rapidly as when the motors are running slow, is played.

When the stick is moved from centre to halfway fully down and then fully down the motors are running in a reverse direction with the same sounds being played at the respective positions.

So I am guessing that I need some way to differentiate the direction am I correct?


[/B]
I have PE6 installed onto my hobby computer ,running XP, but the "simulate" buttons are greyed out and do not work, I will have to resorted to PE5 to run the simulation.

I will make some changes and see how I go, but I expect more questions will be forthcoming.

I have been experimenting and have come up with this;

Code:
[color=Blue]SYMBOL [/color][color=Purple]Pulsecount [/color][color=DarkCyan]= [/color][color=Purple]b1[/color]
[color=Blue]SYMBOL soundpin[/color][color=DarkCyan]=[/color][color=Blue]C.0

Pause [/color][color=Navy]500  [/color][color=Green]'Allows Rx to stabilise[/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.[/color]

[color=Blue]Read [/color][color=Purple]b1[/color][color=Black],[/color][color=Purple]b2[/color]
[color=Blue]Read [/color][color=Purple]b1[/color][color=Black],[/color][color=Purple]b3[/color]
[color=Blue]Read [/color][color=Purple]b1[/color][color=Black],[/color][color=Purple]b4[/color]

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]>[/color][color=Navy]148 [/color][color=DarkCyan]or [/color][color=Purple]b1 [/color][color=DarkCyan]<[/color][color=Navy]152 [/color][color=Blue]then goto [/color][color=Black]Hiss 
 [/color]
[color=Blue]If [/color][color=Purple]b3 [/color][color=DarkCyan]>[/color][color=Navy]153 [/color][color=DarkCyan]or [/color][color=Purple]b3 [/color][color=DarkCyan]<[/color][color=Navy]170 [/color][color=Blue]then goto [/color][color=Black]slow[/color]

[color=Blue]If [/color][color=Purple]b4 [/color][color=DarkCyan]>[/color][color=Navy]176 [/color][color=DarkCyan]or [/color][color=Purple]b4 [/color][color=DarkCyan]<[/color][color=Navy]200 [/color][color=Blue]then goto [/color][color=Black]fast


Hiss:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]100[/color][color=Blue]) [/color][color=Green]'constant hiss[/color]

[color=Black]slow:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]155[/color][color=Black],[/color][color=Navy]100[/color][color=Blue])  [/color][color=Green]'slow sound output[/color]

[color=Black]fast:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]125[/color][color=Black],[/color][color=Navy]100[/color][color=Blue])  [/color][color=Green]'fast sound output[/color]

[color=Blue]Goto [/color][color=Black]START[/color]
From what I can see I will never satisfy most of the instructions because they are valid from previous instructions eg
If b1 >148 or b1 <152 then goto Hiss will cancel out any readings above 152 because they will be above 152.
What I need to do is quarantine any > or < from the previous reading of b1

So my thinking is possible for stopped, Forward and Reverse not to be in the flow ie is it possible to read them in isolation?

If I was writing this in plain language I would state look at b1 and if it is between 146 and 152 then hiss, if b1 is not between 146 and 152 but is between 153 and 175 then slow anyting above 176 then fast.

I have the logic just not the instructions necessary to make it happen.

And I reinstalled PE6 and it all works.
 

AllyCat

Senior Member
Hi,

Code:
[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]>[/color][color=Navy]148 [/color][color=DarkCyan]or [/color][color=Purple]b1 [/color][color=DarkCyan]<[/color][color=Navy]152 [/color][color=Blue]then goto [/color][color=Black]Hiss 
 [/color]
[color=Blue]If [/color][color=Purple]b3 [/color][color=DarkCyan]>[/color][color=Navy]153 [/color][color=DarkCyan]or [/color][color=Purple]b3 [/color][color=DarkCyan]<[/color][color=Navy]170 [/color][color=Blue]then goto [/color][color=Black]slow[/color]

[color=Blue]If [/color][color=Purple]b4 [/color][color=DarkCyan]>[/color][color=Navy]176 [/color][color=DarkCyan]or [/color][color=Purple]b4 [/color][color=DarkCyan]<[/color][color=Navy]200 [/color][color=Blue]then goto [/color][color=Black]fast

Hiss:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]100[/color][color=Blue]) [/color][color=Green]'constant hiss[/color]

[color=Black]slow:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]155[/color][color=Black],[/color][color=Navy]100[/color][color=Blue])  [/color][color=Green]'slow sound output[/color]

[color=Black]fast:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]125[/color][color=Black],[/color][color=Navy]100[/color][color=Blue])  [/color][color=Green]'fast sound output[/color]

[color=Blue]Goto [/color][color=Black]START[/color]
Yes, you're probably better using IF (and maybe .. ELSE .. ENDIF ) than SELECT ... CASE , but you have two fundamental errors. You should be uisng AND not OR and after "Hiss:" the program will "fall-though" into "slow:" and then "fast:". The following might be better (note also the extra =s and change to 175):


Code:
[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]148 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]152 [/color][color=Blue]then goto [/color][color=Black]Hiss 
 [/color]
[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]153 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]17[B]5[/B] [/color][color=Blue]then goto [/color][color=Black]slow[/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]200 [/color][color=Blue]then goto [/color][color=Black]fast
[color=Blue]Goto [/color][color=Black]START[/color]

Hiss:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]100[/color][color=Blue]) [/color][color=Green]'constant hiss[/color]
[color=Blue]Goto [/color][color=Black]START[/color]

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

[color=Black]fast:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]125[/color][color=Black],[/color][color=Navy]100[/color][color=Blue])  [/color][color=Green]'fast sound output[/color]

[color=Blue]Goto [/color][color=Black]START[/color]
But I don't understand what your READs are supposed to do.

To handle the "reverse" motor direction, you should be able to "mirror" the speed range, e.g.:
Code:
	b2 = b1      ; Not less than 45
	if b2 < 150 then
		b2 = 300 - b2
	endif
;  b2  now should be between 150 and 255
Cheers, Alan.

PS to Rev Ed: Is it possible to get rid of all those horrid "color" commands from code taken from PE6? They soon reach the forum's 10,000 character limit and make editing the direct post an absolute nightmare. :(
 
Last edited:

hippy

Technical Support
Staff member
I have PE6 installed onto my hobby computer ,running XP, but the "simulate" buttons are greyed out and do not work
Check you have the source program file saved as a .BAS file, not as a .TXT file.
 
Last edited:

hippy

Technical Support
Staff member
PS to Rev Ed: Is it possible to get rid of all those horrid "color" commands from code taken from PE6?
If you mean when a poster has included syntax colouring, you hit reply and those colouring tags are present in that reply; it is probably not something we could easily do, would be something for the third-party forum software authors.

What I tend to do is copy from the post as viewed before replying, then paste that over whatever is actually in the reply.
 

GAP

Senior Member
Check you have the source program file saved as a .BAS file, not as a .TXT file.
I reinstalled PE6 and it all works fine now, looks like when I originally downloaded it did not install properly as the installed program size was far less than what I downloaded this time.
 

GAP

Senior Member
Hi,



Yes, you're probably better using IF (and maybe .. ELSE .. ENDIF ) than SELECT ... CASE , but you have two fundamental errors. You should be uisng AND not OR and after "Hiss:" the program will "fall-though" into "slow:" and then "fast:". The following might be better (note also the extra =s and change to 175):


Code:
[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]148 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]152 [/color][color=Blue]then goto [/color][color=Black]Hiss 
 [/color]
[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=> [/color][color=Navy]153 [/color][color=DarkCyan]AND [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]17[B]5[/B] [/color][color=Blue]then goto [/color][color=Black]slow[/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]200 [/color][color=Blue]then goto [/color][color=Black]fast
[color=Blue]Goto [/color][color=Black]START[/color]

Hiss:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]100[/color][color=Blue]) [/color][color=Green]'constant hiss[/color]
[color=Blue]Goto [/color][color=Black]START[/color]

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

[color=Black]fast:[/color]
[color=Blue]SOUND soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]125[/color][color=Black],[/color][color=Navy]100[/color][color=Blue])  [/color][color=Green]'fast sound output[/color]

[color=Blue]Goto [/color][color=Black]START[/color]
But I don't understand what your READs are supposed to do.

To handle the "reverse" motor direction, you should be able to "mirror" the speed range, e.g.:
Code:
	b2 = b1      ; Not less than 45
	if b2 < 150 then
		b2 = 300 - b2
	endif
;  b2  now should be between 150 and 255
Cheers, Alan.

PS to Rev Ed: Is it possible to get rid of all those horrid "color" commands from code taken from PE6? They soon reach the forum's 10,000 character limit and make editing the direct post an absolute nightmare. :(
This is where the program is at.

Code:
[color=Blue]SYMBOL 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]175 [/color][color=Blue]then goto [/color][color=Black]slow [/color][color=Green]'pulses between 1.55mS and 1.75mS the train is moving forward slowly[/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]184 [/color][color=Blue]then goto [/color][color=Black]medium [/color][color=Green]'pulses between 1.76mS and 1.84mS the train is moving forward at medium speed[/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]200 [/color][color=Blue]then goto [/color][color=Black]topspeed [/color][color=Green]'pulses between 1.55mS and 1.75mS 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]135 [/color][color=Blue]then goto [/color][color=Black]slow [/color][color=Green]'pulses between 1.47mS and 1.35mS the train is moving in reverse slowly[/color]

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

[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]=< [/color][color=Navy]119 [/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.55mS and 1.75mS 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 soundpin[/color][color=Black],[/color][color=Blue]([/color][color=Navy]255[/color][color=Black],[/color][color=Navy]140 [/color][color=Blue]) [/color][color=Green]'constant hiss with slow chuff[/color]
[color=Blue]Goto [/color][color=Black]START

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

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

topspeed:[/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]'top speed sound output[/color]

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

[color=Green]'####### Note for future development. Consider an extra speed step and chuff beat before slow, and adjust medium and top speed with the aim to eliminate the sudden changes in the chuff beat at the specific throttle setting transition points #####[/color]
It all works fine and is doing what I want it to do, next step is connect it to the loco and tweek it to synchronize the chuffs to the wheel revolutions as per the Note at the end.

AllyCat,

The Reads were a left over from something else that I was trying to get the reverse working that I forgot to delete.

Thank You to both Hippy and AllyCat for your patience and guidance you have made the experience the most enjoyable and I have learned heaps.
 

hippy

Technical Support
Staff member
Code:
If b1 => 148 AND b1 =< 152 then goto Hiss 
If b1 => 153 AND b1 =< 175 then goto slow
If b1 => 176 AND b1 =< 200 then goto fast
Conditions such as this can usually be structured as ...

Code:
select case b1
  case >= 201 : goto ?    ; 201 to 255
  case >= 176 : goto fast ; 176 to 200
  case >= 153 : goto slow ; 153 to 175
  case >= 148 : goto hiss ; 148 to 152
  else        : goto ?    ;   0 to 147
end select
 

AllyCat

Senior Member
Hi,

I'm pleased that the program is now working to the OP's satisfaction, but some of us do like to "Gild the Lily". ;)

Firstly, I introduced the => to avoid changing any of the numbers, but it's worth noting that (in PICaxe Basic) it simply means "Equal to OR Greater than", so just the > can be used if the number is reduced by 1. However, if you want to perform two different operations depending on a particular number or a variable (e.g. b1), then you could use < b1 and => b1 to be sure that all possibilities are covered.

Personally, I prefer the IF .. ELSEIF .. ELSE .. ENDIF command structure which IMHO is more "intuitive" (i.e. Plain Language or WYSIWYG) than CASE .. SELECT , particularly for the "non-goto" format. I wasn't sure if b1 was being used for any other purpose in the program, but if not, then we could go minimalistic with (not tested) :

Code:
symbol soundpin	= c.0
symbol RXpin 	= c.3				; RX pulse on leg 4 . Read RX pulses going to SyRen10A input.6
do						; START the loop
	pulsin RXpin,1,b1 			; Look at the input pin and put into variable b1. 
	if b1 < 150 then 			; Convert from reverse direction
		b1 = 300 - b1 			; Convert reverse to forward direction (eg 140 becomes 160)
	endif
	if b1 < 155 then 			; Hiss pulses between 1.46mS and 1.54ms the train is stopped
		SOUND soundpin,(255,140 )	; constant hiss with slow chuff 
	elseif b1 < 176 then 			; Slow pulses between 1.55mS and 1.75mS the train is moving slowly
		SOUND soundpin,(255,24)		; slow sound output
	elseif b1 < 185 then 			; Medium 'pulses between 1.76mS and 1.84mS the train is at medium speed
		SOUND soundpin,(255,21)  	; medium sound output
	elseif b1 < 201 then 			; Top speed 'pulses between 1.85mS and 2.00mS the train is at top speed 
		SOUND soundpin,(255,18)		; top speed sound output
;	else					; Can b1 ever exceed 200 ?    ;)		
	endif
loop						; Go back to START
Sorry, no pretty colours, I'm using PE5. ;)

Cheers, Alan.
 
Top