Question:

Hi, I'm trying to write a C function that will detect three switch activations within 3 seconds. Any ideas?

by  |  earlier

0 LIKES UnLike

Hi, I'm trying to write a C function that will detect three switch activations within 3 seconds. Any ideas?

 Tags:

   Report

1 ANSWERS


  1. You need some time functions like those in <time.h>, although the time function only returns the time in seconds. Depending on your project, you might need something with more precision than that.

    Basically, when a switch is activated, you remember the time and then compare that to the last time each of the other two switches was activated.

    I'll use some made up functions to illustrate:

    int tm[3];

    tm[0] = tm[1] = tm[2] = 0;

    for (;;) {

    sw = WaitForSwitch();

    tm[sw] = CurrentTime();

    for (int i =0; i < 3; ++i) {

    if (tm[i] < tm[sw] - 3) {

    printf( "too slow\n" );

    break;

    }

    }

    if (i == 3) { /* all three tests passed */

    printf( "Click!\n" );

    break;

    }

    }

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.