Not sure if I am bad at basic physics or getting an odd computation?

I am trying to get a countdown timer on a plate of rotating of LED, but for some reason the numbers are not calculating correctly.

For a duration of 10 seconds and a freq of 30hz
I am getting these values on the crystal LCD
Ie it is going 10.00, 9.00, 7.00, 4.00, 0.00 , -5.00, -11.00, -18.00, -26.00, -45.00

I have also tried using millis to count up, but that is causing even more problems.

void laserStimulate(float frequency, float duration) //frequency in hz and duration in seconds
{ // this method will stimulate for a given frequency and duration
int totalPulses;
float timeLeft;

timeLeft = float(duration); // begin timeLeft with duration
totalPulses = int(duration * frequency); // find total number of pulses, multiply duration and frequency
for (int pulses = 0; pulses <= totalPulses; pulses++) {
if(pulses % int(frequency) == 0)
timeLeft = timeLeft - (float(pulses) / float(frequency));
lcd.print("Time Left: ");
lcd.print(timeLeft);
lcd.print(" s ");

Your (badly posted) code doesn't compile.

 int totalPulses;
  float timeLeft;
 
  timeLeft = float(duration); // begin timeLeft with duration
  totalPulses = int(duration * frequency); // find total number of pulses, multiply duration and frequency

I truly do not understand this coding (lack of) style. (unless you're getting paid by the LOC)

      timeLeft = timeLeft - (float(pulses) / float(frequency));

(weird *)Why (weird *)is (weird *)it (weird *)necessary (weird *)to (weird *)cast (weird *)a (weird *)float (weird *)to (weird *)a (weird *)float?

All that casting does not improve the readability of your program.

You are expecting that dividing an int by a float is going to equal exactly 1.000000000. Bad assumption.

What values are you using for frequency? Why is duration in an odd unit like seconds? The Arduino does not measure time in seconds. Milliseconds, yes. Microseconds, yes. Seconds, no.

well, i don't know if you noticed but at first, you remove one. so far so good. but then you remove 2. it would be fine if you removed it from 10... but here you remove it from 9. so it doesn't give you 10-2 = 8 but 9-2 =7. Next with 3: 7-3 =4 and so on so forth. So you could do:
float output = timeLeft -pulses/(float) frequency;
and then display output instead of timeleft.

I would also then rename timeLeft into: totalTime.

RED_:
well, i don't know if you noticed but at first, you remove one. so far so good. but then you remove 2. it would be fine if you removed it from 10... but here you remove it from 9. so it doesn't give you 10-2 = 8 but 9-2 =7. Next with 3: 7-3 =4 and so on so forth. So you could do:
float output = timeLeft -pulses/(float) frequency;
and then display output instead of timeleft.

I would also then rename timeLeft into: totalTime.

Remove 2 of what from what? Remove what from 10? What are you talking about?

(unless you're getting paid by the LOC)

Obviously not, or the curly braces would be on new lines where they belong.

Your original code is doing exactly what you asked it to do.

Hi,
6 posts and you still haven't asked for.

Can you please post a COMPLETE copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

What is your application?
What are you detecting the "plate of rotating of LED" with?

Tom.... :slight_smile:

@aarg

aarg:
Remove 2 of what from what? Remove what from 10? What are you talking about?

pd1793:
Ie it is going 10.00, 9.00, 7.00, 4.00, 0.00 , -5.00, -11.00, -18.00, -26.00, -45.00

That is obviously not a count-down...

His program does:
10.0 - 1.0 = 9.0
9.0 - 2.0 = 7.0
7.0 - 3.0 = 4.0
4.0 - 4.0 = 0.0
0.0 - 5.0 = -5.0
...

So at every loop, it increments a value and removes it from the result. If instead he was subtracting not from the previous result but from the original value, he would have a proper count down:

10.0 - 1.0 = 9.0
10.0 - 2.0 = 8.0
10.0 - 3.0 = 7.0
10.0 - 4.0 = 6.0
10.0 - 5.0 = 5.0

Therefore, all he needs to do is:

void laserStimulate(float frequency, float duration) //frequency in hz and duration in seconds
{ // this method will stimulate for a given frequency and duration
  int totalPulses;
  float timeLeft;
 
  timeLeft = float(duration); // begin timeLeft with duration
  totalPulses = int(duration * frequency); // find total number of pulses, multiply duration and frequency
  for (int pulses = 0; pulses <= totalPulses; pulses++) {
    if(pulses % int(frequency) == 0)
      timeLeft = duration - (pulses / frequency);
      lcd.print("Time Left: ");
      lcd.print(timeLeft);
      lcd.print(" s     ");

[code]

Please don't assign people a gender without evidence.