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