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 ");
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)
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?
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:
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]