Arduino code- to do the same operation every minute

I am trying to run the following code. But I couldn't get the expected result. It is for an Arduino-based system. I need to run some Tests. Each test has two parameters. The number of 'Samples' and how long each sample to be tested. For example, if the s_count = 5 and t_count = 10, there are a total of 5 samples and each sample should be tested for 10 minutes. During this 10 minutes sample, I want to generate the result every minute and at the end, I calculate the average (of 10 minutes result). Following is my code. I ended up in an infinite loop inside the second while loop. Any suggestions, please.

unsigned long previousMillis = 0;
const long interval = 60000;

void startTest(int s_count, int t_count)

  int sample_number = s_count;
  int test_time = t_count;

        while(sample_number > 0)
                while(test_time >0)
                        unsigned long currentMillis = millis();

                        // do measurement every one minute
                        if (currentMillis - previousMillis >= interval)
                                previousMillis = currentMillis;


                // reset the time counter after finishing one sample
                test_time = t_count;

               // go to the next sample


Post the entire sketch that way no one has to ask what all the missing pieces do

What happens when
(currentMillis - previousMillis >= interval)
is false? How do you then get out of the loop?

1 Like

presumably every minute a test is performed. test the same sample up to nTimes until nSamples have been tested.

doTest() doesn't seem to care what sample is tested. why not just call doTest() nMinutes * nSamples?

what does the code do after testing all samples?

there is an extra controller to move the test sample. So sample count and duration are importnat. the test data will be used to do some calculations at the end of the test ( after testing all the samples).

A delay before calling the startTest() method solved the issue !!!

it's unclear how you manage samples and tests/sample

rather than bury the test in nested loops for the # of samples and # test/sample, you could perform a test only every minute and when a test is performed manage the # tests/sample and # of samples.

this approach allow other processing (e.g. checking buttons) between tests

run ()
    static unsigned long msecLst = 0;
           unsigned long msec    = millis();

    if ( (msec - msecLst) > ONE_MINUTE)  {
        msecLst = msec;
        doTest ();

        return 1;
    return 0;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.