Control loop with scanf

This is a discussion on Control loop with scanf within the C Programming forums, part of the General Programming Boards category; Hi, Basically I'm using C to control an external board. (interfacing) So, what I need the LEDs on the board ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Control loop with scanf

    Hi,
    Basically I'm using C to control an external board. (interfacing)
    So, what I need the LEDs on the board is to blink until a user inputs char 'a'.
    The thing is, if I used scanf, the blinking would pause and wait until the user inputs char 'a'.

    Is there any way to avoid pausing the program, but at the same time end the blink loop when the user inputs char 'a'??

    Its not necessary to use scanf here.. Any solution will do.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You either need to use a non-blocking read from the keyboard (I think there is some mention of that in the FAQ in the section called "How do I read from console without enter").

    Or you can use threads to read the keyboard in one thread, and blink the LED in another.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    So by using these threads, I can use the output of one thread to control the other?
    N can both threads run simultaneously?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Two threads will run "simultaneously". If you have a dual core processor and an OS with support for MultiThreading, you can even run both threads REALLY at the same time. Otherwise it will appear to humans to run simultaneously, but it will actually run for a few tens of milliseconds at a time (or in the case of waiting for input, the other thread will run, and the one waiting for input will simply sit there and wait for the keyboard entry).

    It's not so much that you can use the "output" of one thread to control another, but that you can use one thread to CONTROL another thread.

    What OS are you using?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    1
    well, we are using Ubuntu-Linux OS, but I think we gonna use comp with a single-cored processor.
    the problem is that we have not covered Threads yet... so, do you know any source that could teach in a short period of time how to program and use Threads??

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,171
    Single core is fine. Won't be any different from human's point of view.

    https://computing.llnl.gov/tutorials/pthreads/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-23-2009, 03:51 AM
  2. scanf in a loop
    By santos288 in forum C Programming
    Replies: 3
    Last Post: 10-12-2005, 08:58 PM
  3. Getting the loop control form the user
    By Extropian in forum C Programming
    Replies: 6
    Last Post: 08-11-2005, 09:50 AM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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