breaking while loops on command

C, C++, and Visual C++
alame005

breaking while loops on command

Postby alame005 » Tue Apr 19, 2011 7:25 pm

Hey everyone,
so I'm trying to run the stepper motor in a clockwise anti-clockwise motion,
what I did was a simple while loop :

while(1)
{
CPhidgetStepper_setTargetPosition (stepper, 0, 1300);
Sleep(1500);
CPhidgetStepper_setTargetPosition (stepper, 0, -1300);
Sleep(1500);
}

but I want to break this while loop when I press any button, I can't wrap my head around where to use the getchar command, I tried putting it after the second sleep but the motor would stop running waiting for my command. I guess it's simple but I'm new to C++ any tips would be appreciated ! Thanks

erik
King of the Lab
Posts: 476
Joined: Fri Mar 06, 2009 12:42 pm
Location: Calgary, Canada
Contact:

Re: breaking while loops on command

Postby erik » Wed Apr 20, 2011 8:54 am

You could use pThreads to have a separate thread that is blocking on getchar() and then changes a global flag once you hit a key. Then in the original thread, instead of using while(1), you can use while(global flag).

alame005

Re: breaking while loops on command

Postby alame005 » Wed Apr 20, 2011 2:55 pm

Alright thanks, I'm reading up on pthreads to see how I can set it up :)


Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 2 guests