Calculate Variable in Loop

I have encountered a strange problem when trying to use a variable in a loop.

Explonation of the code:

The variable fre (Frequency in ms) should be entered in the dialogue

The program should calculate this to pulse period in points:

per=1000/(fre*0.05) // 0.05 is the scaling factor

This however does not work. If I cut out the calculation and design the dialogue so that per is entered directly it works fine. Entering a number for the frequency in the code also works fine. Just this way it does not work and I don't understand. I also put the calculation of fre without inside the loop - same sad result.

This is the loop.
do

a=lat+(inter*count)
b=a+dur

p=per*countin
digital_out [a+p,b+p]=amp
countin+=1
if (countin continue
endif
countin=0
count+=1
while(count

The function as it works now looks like this (entering the pulse period in the dialogue and printing the frequency as a wave - all quite clumsy :(

Function SetUp_TTLTrainStim()

Variable amp
Variable/G n_points
Variable num,lat,dur,inter, numin
Variable count,countin, a,b,p,per,fre
Variable Wave_Duration




Dowindow/K graph0
Dowindow/K graph1
Dowindow/K graph2
Dowindow/K graph3
Dowindow/K graph4
Dowindow/K graph5
Dowindow/K graph6
Dowindow/K graph7
Dowindow/K graph8
Dowindow/K graph9

wave_duration=60000
n_points = Wave_duration*160
Make/O/N=(160*Wave_Duration) Chan8out, Chan8in //is this the big wave that gets devided into smaller ones -> yes
Make/O/N=(20*Wave_Duration) AD0_in, AD0_out, AD1_in, AD1_out, AD2_in, AD2_out, AD3_in, AD3_out, AD4_in, AD5_in, AD6_in, AD7_in,digital_out, AD8_in, AD8_out
SetScale/P x 0, 0.00005,"s",AD0_in, AD0_out, AD1_in, AD1_out,AD2_in, AD2_out,AD3_in, AD3_out,AD4_in, AD5_in, AD6_in,AD7_in, Chan8in, Chan8out, digital_out, AD8_in, AD8_out
Setscale/I y, -inf, +inf, "V", AD0_in,AD2_in,AD4_in,AD6_in,AD0_out,AD1_out,AD2_out,AD3_out,digital_out
Setscale/I y, -inf, +inf, "A", AD1_in,AD3_in,AD5_in,AD7_in
Setscale/I y, -inf, +inf, "Hz", AD8_out

AD0_in=0
AD1_in=0
AD2_in=0
AD3_in=0
AD4_in=0
AD5_in=0
AD6_in=0
AD7_in=0
AD0_out=0
AD1_out=0
AD2_out=0
AD3_out=0
digital_out=0
AD8_out=0 //to show frequency (no better way found)

Chan8in=0
Chan8out=0

amp=1
lat=1000
dur=5 // time of one stimulus within the train of stimuli
inter=3000 // time inbetween different trains of stimuili
numin=10
num=wave_duration/inter-1
count=0
countin=0
per=10
Prompt per, "length of a pulse period [ms]: "
Prompt dur, "single pulse duration [ms]:"
Prompt numin, "number of single pulses per train"
DoPrompt "Dialog", per,dur,numin

//per=20000/fre -> does not work
fre=1000/per
Printf "Laserfrequenz: %g Hz", fre //History
AD8_out=fre
per/=0.05
lat/=0.05 // means everything devided by 0.05 so the program calculates number of points and I enter ms
dur/=0.05 // duration of a single pulse
inter/=0.05

do
a=lat+(inter*count)
b=a+dur

p=per*countin
digital_out [a+p,b+p]=amp
countin+=1
if (countin continue
endif
countin=0
count+=1
while(count
Display/W=(0,0,300,150)/K=1 AD0_in //left,top,right, bottom
TextBox/C/N=text0/F=0/E=2 "\\f01 Vm1"
Display/W=(302,0,600,150)/K=1 AD1_in
TextBox/C/N=text0/F=0/E=2 "\\f01 nA1"
Display/W=(0,205,300,355)/K=1 AD2_in
TextBox/C/N=text0/F=0/E=2 "\\f01 Vm2"
Display/W=(302,205,600,355)/K=1 AD3_in
TextBox/C/N=text0/F=0/E=2 "\\f01 nA2"
Display/W=(0,380,300,530)/K=1 AD4_in
TextBox/C/N=text0/F=0/E=2 "\\f01 Vm3"
Display/W=(302,380,600,530)/K=1 AD5_in
TextBox/C/N=text0/F=0/E=2 "\\f01 nA3"
Display/W=(0,555,300,705)/K=1 AD6_in
TextBox/C/N=text0/F=0/E=2 "\\f01 Vm4"
Display/W=(302,555,600,705)/K=1 AD7_in
TextBox/C/N=text0/F=0/E=2 "\\f01 nA4"
Display/W=(604,0,900,150)/K=1 digital_out
TextBox/C/N=text0/F=0/E=2 "\\f01 Laser"
Display/W=(604,205,900,355)/K=1 AD8_out
TextBox/C/N=text0/F=0/E=2 "\\f01 LaserFrequenz"


End

First, please enclose any code in <igor></igor> tags so that it will be properly formatted. Otherwise, it's assumed that you are entering HTML, so parts of your code disappear and the rest is impossible to read.

You say that

The variable fre (Frequency in ms) should be entered in the dialogue

The program should calculate this to pulse period in points:

per=1000/(fre*0.05) // 0.05 is the scaling factor

This however does not work.


However, you don't explain what "this however does not work" means. What does not work? What result do you get? Is there an Igor error?

Looking at your code, you aren't prompting the user to enter the value of fre, so your line
//per=20000/fre -> does not work

will result in a divide by zero error, assuming that fre is 0.

Instead, your DoPrompt statement needs to look something like:
DoPrompt "Dialog", per,dur,numin,fre


If you can't figure things out I suggest you use the built in symbolic debugger. Set a breakpoint in your function and then step through to see what is happening.
Thank you for your comment.

The crux of the matter was really the division by zero.

Entering this BEFORE the loop solved the problem. I had tried it before within the loop which did not work, meaning that there was just one pulse instead of a train of pulses.

 
if (fre>0)
per=1000/fre
endif
dur=per/2


So thanks again,

Diana