C program for a counter

59.3k Views Asked by At

I want to create a counter. Every 10 seconds I want to check the state of a switch. For example if the switch is closed then the 10 sec counter increments. If it is open it goes back to sleep, wakes up again in 10 seconds and checks the state of switch. When the count reaches e.g 100 then do something. How would I go about doing this

My attempt is:

for(int i=0;i<100;i++){
if(SW=1) {
    i++;
}
else
    i=0;
}
3

There are 3 best solutions below

0
On BEST ANSWER

You can look at this code:

int sw = 0;
#define MAX 100
#define gotosleep sleep(10)

int main(void)
{
    int num = 0;
    while(1) {
        gotosleep;
        if(sw)
            num++;
        else
            num = 0;

        if(num == MAX) {
            //do something
            printf("Done\n");
            num = 0;
            break;
        }
    }

    return 0;
}
  1. Go to sleep for 10 seconds
  2. If switch is ON, the increment num, else reset num to 0 and go to sleep
  3. Check if num has reached MAX value set, if equal to MAX then do something and break. (To continue the cycle comment the break in the code).
  4. Wake up again after 10 seconds and check for the state of the switch -> step 2
0
On

I'm not very sure if I have understood your problem correctly, but you can check the following code for some ideas. It is self-explaining.

NOTE : to test the functionality locally, please enable the test codes

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MAXVAL 100
#define SLEEPTIME 10

//extern int switchSet; //if defined in some other file
int switchSet;

int main()
{
    int counter = 0;
    int toSleep = 0;
#if 0
        //for testing
    switchSet = MAXVAL; 
#endif

    while (1)
    {
        toSleep = switchSet? SLEEPTIME:0;   //check for the switch state, 0 = open, 1 = closed
        if (toSleep)
        {
            printf("Going to sleep for %d sec\n", SLEEPTIME); 
            sleep(toSleep);
        }
        else
        {
            counter++;
            printf ("No sleeping, counter is %d\n", counter);
        }
        if (counter == MAXVAL)
        break;
#if 0
        //for testing 
        switchSet--;
        if (switchSet < 0) switchSet = 0;
#endif
    }

    printf("Do Something... Did, Done !!\n");

    return 0;

}
0
On

I think you need to be more specific on the question. It seems like you want to reset the counter every time the switch is open. Are you sure you want that ?

Anyway, here is probably what you want

#include <stdio.h>
#include <time.h>
struct tm tm;   
time_t start,enxd;
double sec;
int counter;
int main(){
    int switchCounter = 0;
    int checkSwitch;

    checkSwitch = 1; // I put this in purpose since I have no idea how you're going to read the switch. 
                     // Thus, this assumes the switch is always closed.

    while(switchCounter != 100){
        // 1. Wait for 10 seconds
        sec = 0;
        time(&start);

        while(sec !=10){
            ++counter;
            time(&enxd);
            sec=difftime(enxd,start);
        }

        // 2. Read the state of the switch here.
        // ..............

        // 3. Simple if-else
        if (checkSwitch == 1){ //switch is closed
            switchCounter++;
            printf("Counter incremented. Current = %i \n", switchCounter);
        }
        else //if switch is open
        {
            switchCounter = 0 ;// Iam confused here, is this what you want ?
            printf("Switch is open \n");
        }
    }
    // 4. Finally when your counter reaches 100, you wanna do something here
    // ............................

    return 0;
}

Hope it helps :)