Need a bit of guidance here. A simple program I wrote before I add in my complicated robot control command. I need to make this program keep repeating itself until I press a key to stop it. It's like keep commanding the robot to move until I want it to stop. Please do look at my simple program first. Thanks in advance.
Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch !=EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int main()
{
char ch;
int asc;
while(!kbhit())
scanf("%c",&ch);
asc=toascii(ch);
if (asc==119)
{
printf("\n move front \n");
}
else if (asc==97)
{
printf("\n move left \n");
}
else if (asc==115)
{
printf("\n move back \n");
}
else if (asc==100)
{
printf("\n move right \n");
}
else if (asc==104)
{
printf("\n H \n");
}
else
{
printf("\n stop \n");
}
return 0;
}