Thread: getch() in while(TRUE) and with statemachine

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    5

    getch() in while(TRUE) and with statemachine

    Hi C-Forum,

    i have a question... i try to program a robot spider. therefore i use an endless-loop with while(true) and a switch(case=state) statemachine to switch in the different movement patterns. My problem is that a movement pattern case needs ~2 seconds time to move the spiders legs and if i for example hold the button "w" (forward) a long time, i see "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwww" in the console and the spider walks forward for 5 minutes without any option to stop (okay ctrl + z works). i want my program not to read the next "w" but i want it to read when the next loop begins.
    do you know how to do that?

    This is my main-program:

    Code:
    void main() {
    
    void statemachine(int);
    
    while (1){
      if(kbhit())
      {
      in = getch(); //catching w,a,s,d, ESC
      }else{
      in = '#'; // in = # if nothing is pushed
      }
      statemachine(in);
      
    }//End while
    }//End main
    this version of my statemachine has the following cases:

    w -> going forward
    a -> turning left
    s -> going backwards
    d -> turning right
    n -> neutral: waiting for a command
    esc -> close program and roll to init-position.

    i see the following on the console in the worst case:

    wwwwwwwwwww
    state forward (needs 2 seconds...)
    state forward (needs 2 seconds...)
    state forward (needs 2 seconds...)
    d
    state forward (needs 2 seconds...)
    state forward (needs 2 seconds...)
    state forward (needs 2 seconds...)
    ...
    state turning right (needs 2 seconds...)
    state neutral
    ...

    BUT i want it like this:

    wwwwwwwwwwwwwwwwww
    state forward (needs 2 seconds...)
    state neutral
    state neutral
    state neutral
    w
    state forward (needs 2 seconds...)
    w
    state forward (needs 2 seconds...)
    state neutral
    d
    state turning right (needs 2 seconds...)
    state neutral
    ...

    thank you
    Last edited by smolnar; 07-31-2016 at 11:29 AM.

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Start a timer and ignore input for 2 seconds or only take the last one entered? The code you have it a bit hard to follow what it is you want.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    >void main

    Main returns int, not void.

    void statemachine(int);
    that should of caused an error - you cannot declare a function
    within a function.

    CodeSlapper mentioned a timer, one idea is to look into the basic
    Windows API (if your using Windows) and check up on the Sleep()
    function. 1000 mils = 1 second in time format.
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    5
    @CodeSlapper how can i do this? how can i ignore input for a couple of seconds?
    now when i push "w" 2 seconds: i see "wwwwwwwwwwwwwwwwwww" in the console and then the state "forward" is used many times and a long time. Thats why there are "sleep(2)"s in all my states yet in ordner to simulate the states.

    wwwwwwwwwww //Now i push w 2 seconds
    state forward (4 seconds...)
    state forward (4 seconds...)
    state forward (4 seconds...)
    //we see forward states more then 2 seconds (example 12 seconds)
    d //d is ingored because too much w are saved.
    state forward (4 seconds...)
    state forward (4 seconds...)
    state forward (4 seconds...)
    ...
    state turning right (4 seconds...)
    state neutral

    my spider would crash the wall
    Last edited by smolnar; 08-01-2016 at 07:44 AM.

  5. #5
    Registered User
    Join Date
    Jul 2016
    Posts
    5
    A small while(kbhit()){return(0);}-loop does all i want. It solved my problem (!):

    Code:
     
    int getinput(void){ 
      int x=0; 
    if(kbhit()) 
      { 
      x = getch(); 
      while(kbhit()){ // (!)
        return(0);}    // (!) 
      }else{ 
      x = '#'; 
      } 
      return x; 
    }//End getinput

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getch Refresh? Simple Problems with Getch()
    By justin777 in forum C Programming
    Replies: 7
    Last Post: 10-26-2011, 01:21 AM
  2. for(;;) vs. while(true)
    By Hunter2 in forum C++ Programming
    Replies: 30
    Last Post: 09-06-2003, 04:18 PM
  3. Is this true about Dev-C++?
    By RealityFusion in forum Tech Board
    Replies: 7
    Last Post: 08-22-2003, 03:40 PM
  4. Is this true?
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-18-2002, 04:11 PM

Tags for this Thread