-2

I have an array with data from the adc, for this data I need to save 10 periods per second in the sd card, to detect the periods I use a zerocrossing function then I add one to a variable averytime it crosses zero( changes sings) and write a file, I have two problem, first is seting up the timer to send 10 periods of data every second. My second problem is that I just want to send those 10 period of data, have a break and continue sending the next 10 periods. this code works, my question is how can I implement the timing, How can I send 10 periods in 1 second?

void zeroCrossing(float* data, float* zerCross, int nx)
{
    int i;
    int a = 0;
    bool sign1, sign2;

    memset(zerCross, 0, nx * sizeof(float)); //copies the 0  to the first characters of the string
    //pointed to, by argument
    for (i = 0; i < nx - 1; i++) {
        float b[16]; /* loop over data  */
        b[i] = data[i];
        sign1 = getSign(data[i]);
        sign2 = getSign(data[i + 1]);
        if (sign1 != sign2) { /* set zero crossing location */
            zerCross[i + 1] = 1;
            a++;
            // break ;
            // continue;
            if (a == 10) { // 10
                break;
            }
        }
    }
    //cout << a<<endl;
}

/* get sign of number */
bool getSign(float data)
{
    if (data > 0) /* positif data */
        return (1);
    else /* negatif data */
        return (0);
}
joe
  • 19
  • 3
  • So what language are you using; and if you know that some code isn't relevant; don't post it. – UKMonkey Mar 21 '18 at 11:54
  • What is the question? – Ron Mar 21 '18 at 11:55
  • So [familiar](https://stackoverflow.com/questions/49383512/need-to-produce-a-function-that-detects-the-zeros-in-a-array)... – Aconcagua Mar 21 '18 at 11:59
  • Format your code properly, don't use C tag if you're using C++ and describe what the code does and and what it should do instead. – klutt Mar 21 '18 at 12:04
  • 1
    You don't ask a question, or describe any error conditions. You only only describe what you want to do. Does your code work? If not what part is not working? And as commented, everything about this post is deja vu – ryyker Mar 21 '18 at 12:05
  • The `getSign` function as is can produce false positives, consider a sequence `1.0, 0.0, 1.0`; if this is to be considered a crossing, too, then you get false negatives with `-1.0, 0.0, -1.0`... – Aconcagua Mar 21 '18 at 12:06
  • Have you looked at the _[clock() function](https://www.tutorialspoint.com/c_standard_library/c_function_clock.htm)_? Or, you could read about a millisecond timer, _[HERE](https://stackoverflow.com/questions/10192903/time-in-milliseconds)_. – ryyker Mar 21 '18 at 12:08
  • 1) You should [edit](https://stackoverflow.com/posts/49405662/edit) your SO-question such that it contains the real question instead of posting the latter in comments. 2) The code you provide is irrelevant for the question you actually ask... – Aconcagua Mar 21 '18 at 12:10
  • I looked at the clock() function, but don't understand how to use it. – joe Mar 21 '18 at 12:16
  • @joe You can do e. g. a busy wait on: read the value once as reference, read the value again and again until its difference to the reference exceeds the desired period. Don't forget to sleep minimally within the loop for not exhausting CPU usage... – Aconcagua Mar 21 '18 at 12:28

1 Answers1

2

One possibility is using sleep_until:

auto timestamp = std::chrono::high_resolution_clock::now();
for(;;)
{
    doTheStuff();
    timestamp += std::chrono::milliseconds(100);
    std::this_thread::sleep_until(timestamp);
}

While this could be achieved similarly with sleep_for with slightly less effort, above approach is slightly more precise because it considers calculation time of whatever you do.

For small tasks, such precision might be irrelevant as sleeping precision itself is likely to be worse anyway...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59