1

I am writing a C program to blink LED for Raspberry Pi. Its like (i) Blink LED (2) Stop blinking it.

Now while the LED is blinking, if I press 2 it should stop blinking. How to do it ?

If I include scanf inside the code will stop blinking.

while(1)
{
 printf("Enter a command \n 1.Blink Led\n 2. Stop blinking\n");
 scanf("%d",&choice);
if(choice==1)
    for (;;)
    {
        digitalWrite (LED, HIGH) ;  // On
        delay (500) ;               // mS
        digitalWrite (LED, LOW) ;   // Off
        delay (500) ;
        // If i press 2 the led should stop blinking
  }

else if(choice==2){
 digitalWrite (LED, LOW) ;
}
Abhishek Ghosh
  • 1,161
  • 9
  • 19
Aditya Jha
  • 69
  • 1
  • 8
  • 1
    Two things: You need to check for the `2` key *in* the blinking loop. And you need to find a way to poll for keyboard input *without* blocking (which the `scanf` does by default). – Some programmer dude Jul 06 '17 at 05:30
  • 1
    Unclear question. You might want to use [poll(2)](http://man7.org/linux/man-pages/man2/poll.2.html) in some [*event loop*](https://en.wikipedia.org/wiki/Event_loop) – Basile Starynkevitch Jul 06 '17 at 05:32
  • @Someprogrammerdude ya that is what I m looking for. I was unaware of that polling. Thanks I will find out. – Aditya Jha Jul 06 '17 at 05:35
  • Here are some links you can check: https://stackoverflow.com/questions/717572/how-do-you-do-non-blocking-console-i-o-on-linux-in-c or https://stackoverflow.com/questions/37292474/how-to-do-nonblocking-input-from-stdin-in-c or https://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input – Support Ukraine Jul 06 '17 at 05:36
  • @AdityaJha this is not the correct way to take input sorry – Vishwajeet Vishu Jul 06 '17 at 06:32

2 Answers2

0

if you don't want to interrupt the Blinking you should try to run the blinking in a parallel process using pthread_t. i really want to give you the code but i'm not on my PC at the moment.

I.AMIR
  • 31
  • 1
0

Use poll:

#include <stdio.h>
#include <unistd.h>
#include <poll.h>
int main()
{
    struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
    unsigned char c, blink=1, on =1, q=1;
    while(q) {
      if( poll(&mypoll, 1, 500) ) {
        c=getchar();
        switch(c) {
          case '1':
              blink = 1;
              break;
          case '2':
              blink =0;
              break;
          case 'q':
              q= 0;
              break;
       }
   }
   else{
    if (blink) {
       if (on)
          on =0;
       else 
         on =1;
     }
     printf("%u\n",on);
    }
  } 
  return 0;
}

replace the on parameter with the led functions.

  • You can replace `if(on)on=0; else on=1;}` with `on=1-on` :-) – Mark Setchell Jul 07 '17 at 10:03
  • @Mark Setchell or with on = 1^on , but will any of these make the code easier to read ? –  Jul 07 '17 at 17:28
  • It was just a fun observation reminding me of a real-time system I once worked on which was double-buffered between buffer `0` and `1`, and the timing was so critical that avoiding the CPU-stall caused by the `if` test made all the difference :-) – Mark Setchell Jul 07 '17 at 17:59