0

I’m trying to make a basic timer, this code seems to print out the number of milliseconds correctly based on the value in duration

But can someone tell me why it prints the output 4 times for each ‘tick’regardless of what the value of duration is, and how to correct it so that it only outputs once on every ‘tick’ set in duration? I’m sure it must be something simple I’m overlooking but I’m still learning the basics of C++ and I can’t see the error.

I’m running it on the iOS “Mobile C” app, but I don’t imagine that that would be what’s causing the problem.

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono;

    auto start = high_resolution_clock::now();

    int duration = 100;

    int i = 0;
    while (i <= 100)
    {
        auto now = high_resolution_clock::now();

        auto millis = duration_cast<milliseconds>(now - start).count();

        if (millis % duration == 0)
        {
            std::cout << "millis: " << millis << std::endl;
            i++;
        }
    }
}
Paul
  • 115
  • 2
  • 10
  • 1
    `while (1)` is an infinite loop and you have no `break`, `return`, `goto` or `longjmp` statement *in* the loop. Are you sure that's what you want? That it happens to run 4 times while `millis % duration == 0` on your machine is purely a coincidence. On a faster or slower machine it would output more or fewer times. – Jesper Juhl Apr 20 '19 at 13:51
  • Instead of performing % operation to do the timer, why not use the sleep function instead? , based on https://stackoverflow.com/questions/4184468/sleep-for-milliseconds, just wait and print when the sleep duration is over – dee cue Apr 20 '19 at 13:58
  • I was just using that to keep the loop going while I figure out how to make something happen at specific intervals. I did that to simulate the Arduino loop. I’m trying to work on some Arduino code without having to use the actual Arduino IDE and a board, so I’m looking for a way to simulate what the Arduino millis() function does. – Paul Apr 20 '19 at 13:58
  • I edited the code to get rid of the infinite loop, it didn’t change the behavior. – Paul Apr 20 '19 at 14:40
  • @dee cue - I will look into using the sleep function, at a first glance it’s over my head, but looks like a good thing to learn about. I’m still working my way through “C Programming” book and about 20 pages into the C++ Primer, so I’m just not there yet. – Paul Apr 20 '19 at 17:00

1 Answers1

4

Since I'm still new here I can't post a comment but the reason you are getting that output 4 times is because that if statement is true 4 times for that one millisecond on your machine like Jesper Juhl was saying. Consider using a bool to make sure it only runs once. Something like

bool hasRun = false;
if (millis % duration == 0)
    {
        if (!hasRun)
        {
            std::cout << "millis: " << millis << std::endl;
            hasRun = true;
        }
    }
else
    hasRun = false;

Then you would be able to keep you infinite Arduino simulation loop like you mentioned in the comments but only have the statement ring true once per duration.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
Joshua Stevens
  • 200
  • 1
  • 9