Page 1 of 1
While loop glitch
Posted: Wed Nov 09, 2016 7:19 pm
by billduck
I have written a program to sound a buzzer (duration and pitch). The while loop controls the duration. The loop count is calculated. But when it is run in simulation, the variable evaluates to zero, and bypasses the loop. Can't figure it out. The program is attached.
Thanks in advance.
Re: While loop glitch
Posted: Wed Nov 09, 2016 8:33 pm
by EtsDriver
Have you seen
this btw?
But seems that your code fails to evaluate the floor thing there? It could be that its atmel specific code part:
Atmel.com site wrote:The floor() function returns the largest integral value less than or equal to __x, expressed as a floating-point number
Re: While loop glitch
Posted: Wed Nov 09, 2016 10:10 pm
by medelec35
I don't know if i'm missing something here?
If you look at the calculation:
You will see you have
Code: Select all
.Period_MicroSeconds = 1000000 / .Frequency_Hz = 1000000/440 = 2272
I agree with that, but then you have
Code: Select all
Num_PeriodsF = floor (.Duration_MilliSec / .Period_MicroSeconds) = 100/.000272
Since .Period_MicroSeconds is 2272 and not .00272 then loops = 100/2272 = 0.004 hence loop count = 0
The nearest I can get is
Code: Select all
.Num_PeriodsF = .Duration_MilliSec / (.Period_MicroSeconds / 1000)
Simulation result is 50 and not 44, not sure why?
So issue appears to be within the calculations?
Martin
Re: While loop glitch
Posted: Thu Nov 10, 2016 12:36 am
by billduck
Thanks. It simulates OK, but does not run on an UNO when I use .Num_Periods in the do_while loop. However if I change the loop counter to "44" it works on the UNO. I thought that since the .Num_Periods is an integer, that the number 44.01 would be cast to an integer, 44. Apparently not. I wonder how I can change 44.01 to an integer 44?
Re: While loop glitch
Posted: Thu Nov 10, 2016 12:48 am
by billduck
The following where .Num_PeriodsF is a float, does not work also.
".Num_PeriodsF = 1000 * .Duration_MilliSec / .Period_MicroSeconds // Periods = 1000*100 /.002272 = 44.01
.Num_Periods = float2int (.Num_PeriodsF) "
Dang.
Re: While loop glitch
Posted: Thu Nov 10, 2016 12:58 am
by billduck
Nor does this work on the UNO: where variables ending in F are floats.
.Period_MicroSecondsF = 1000000 / .Frequency_Hz // Example Period = 1000000/440 = 2272.72... microseconds
.Num_PeriodsF = 1000 * .Duration_MilliSec / .Period_MicroSecondsF // Periods = 1000*100 /.002272 = 44.01
.Period_MicroSeconds = float2int (.Period_MicroSecondsF)
.Num_Periods = float2int (.Num_PeriodsF)
Re: While loop glitch
Posted: Thu Nov 10, 2016 9:52 am
by LeighM
Hi,
Your variables that are not floats are INT, so are limited to -32768 to 32767
So any temporary calculations will be limited to 16 bit integer on the UNO
Also ".Duration_MilliSec / .Period_MicroSeconds" can only give an integer result.
You will need to break down the steps of the calculations and consider the precision at each step,
and also change the ints to long ints where needed.
Re: While loop glitch
Posted: Thu Nov 10, 2016 12:13 pm
by Benj
Also in addition to Leigh's post,
the 1000000 is an integer value, to cast the number as a float add the real portion of the number.
Re: While loop glitch
Posted: Sat Nov 12, 2016 4:24 pm
by billduck
Thanks to all. The adding ".0" helped. But there is are still issues in designing an generic program where tone frequency and tone duration are passed to a subroutine.