Trying to create a non-blocking timer, that counts time in between keypresses

This is a discussion on Trying to create a non-blocking timer, that counts time in between keypresses within the C Programming forums, part of the General Programming Boards category; For the life of me I can't get this to go together, any help would be appreciated. One very important ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    1

    Trying to create a non-blocking timer, that counts time in between keypresses

    For the life of me I can't get this to go together, any help would be appreciated. One very important note, the below case statement can not block the user from pressing other keys on the keyboard and and being able to manipulate their functions (e.g. press R increases temperature by 1)

    User presses 'z', this triggers the "silence" mode of my alarm. The looping sound I have in my program stops (that works). Then what I need from this case statement is to:

    1: start a timer, so that if the user does not press 'z' again within 5 seconds it will automatically break out of the 'silence' condition and start the looping sound again

    2: the user can manually pause the silence condition, by pressing 'z' before the 5 seconds is up.

    Here's what I have, and the functions below that are called

    Code:
    case 'Z':                
    case 'z':
    
    time(&timeOfMute);
    
    if(isSilenceValid() == TRUE)
             {
    
    
              if((currentTime - timeOfMute) >= 1)
                {
                   setSilencedState(FALSE);
                   printf("this works");
                   break;
                }
              else
                   setSilencedState(FALSE);
              }
          else setSilencedState(TRUE);
    
           break;


    here are the two functions called above

    Code:
    char isSilenceValid(){
        
    return silenceIsValid;
    
    }


    Code:
    char isAverageValid(){
        
    return averageIsValid;
    
    }



  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    i would use a state machine structure and run it outside of the switch statement so it gets executed on every loop. use keystrokes and timers as inputs
    Code:
    state = 0;
    ...
    loop
    ....
        switch() {
         ...
        case 'z':
            z_pressed = 1;
            break;
        ...
        } // end of switch
    
        // alarm handler
        switch(state) {
        case 0:
        	// waiting for input, alarm is active
        	setSilencedState(FALSE);
        	if (z_pressed) {
        		setSilencedState(TRUE);
        		time(&timeOfMute);
        		state = 1;
        	}
        	break;
        case 1:
        	// silenced, watch for 'z' again or timeout
        	time(&current_time);
        	if (z_pressed) {
        		// manually pause (not sure what that means)
        		// restart timer?
        		time(&timeOfMute);
        		// or cancel the silence?
        		state = 0;
        	}
        	else if ((current_time - timeOfMute) > timeout_value) {
        		// reactivate the alarm
        		state = 0;
        	}
        	break;
        default:
        	// error
        	break;
        }
        z_pressed = 0;
        
    ...
    end of loop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timer using time.h
    By ADHDadditiv in forum C Programming
    Replies: 4
    Last Post: 10-05-2011, 05:26 PM
  2. Non blocking thread TIMER
    By evariste in forum C Programming
    Replies: 10
    Last Post: 01-14-2011, 11:56 AM
  3. Can anyone create a timer for WinLIRC?
    By richard23 in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2008, 07:57 AM
  4. How to build a timer that counts the seconds elapsed?
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-28-2007, 01:26 PM
  5. How do I create an accurate timer?
    By Quin in forum C Programming
    Replies: 1
    Last Post: 01-06-2002, 02:28 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21