0

I'm trying to create a program in Raspberry Pi. This program is in C language. In the project, I have two LEDs. The first for "EV1" and the second for a pomp "P1". When I click in the console the char "S", the project starts with EV1. When I click in the console "g", the EV1 stop and the pomp starts for 60s. If in the 60s you put in the console the "C" char, the pomp still works without stopping after 60s; else if you don't put any char in the console or something different to "C", the pomp doesn't continue working and stops after 60s.

The problem is if I put in my code the print() and the scanf() arguments, the console still waits for the user to put in a char so the pomp works after I put a char and in my case I need the pomp working such that at the same time I can put in a char.

So this my code:

#include <stdio.h>
#include <bcm2835.h>
#include <string.h>

#define FAN 16
#define P1 17
#define P2 27
#define GPP 22
#define GPN 20
#define EV1 21
#define EV2 13
#define LAN 19

void LampOn(int a)
{   
        bcm2835_gpio_set(a);    
}

void LampDel(int a,int b)
{
    unsigned int del=1000;

    bcm2835_gpio_set(a);
    bcm2835_delay(del * b);
    bcm2835_gpio_clr(a);
}       

void LampOff(int a)
{
    bcm2835_gpio_clr(a);
}   

void OUTPUT(int c)
{
    bcm2835_gpio_fsel(c, BCM2835_GPIO_FSEL_OUTP);
}

char Taper()
{
    char v;
    printf("put a char: ");
    scanf("%c",&v);
    return v;
}   

int main(int argc, char **argv)
{
    if(!bcm2835_init())
    return 1;

    OUTPUT(17);
    OUTPUT(27);
    OUTPUT(22);
    OUTPUT(13);
    OUTPUT(19);
    OUTPUT(20);
    OUTPUT(21);
    OUTPUT(16);
    LampOff(EV1);

    do
    {
        Taper();
    }while(Taper()!='s');

    while(1)
    {
        LampOn(EV1);
        Taper();

        if(Taper()=='g')
        {

            LampOff(EV1);
            LampDel(P1,60);
        }
    }

    while(1)
    {
        /* Infinite loop */
    }
}

I think to add a counter, but that's still a bad solution because if I use a counter, the program stop until the counter stops.

How can I resolve these problems?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sekmani52
  • 55
  • 8
  • I honestly have no idea what you're talking about... Please add some clarification. – ForceBru Jun 26 '17 at 20:37
  • Welcome to SO. This is a question and answer site, so what is your question? – Jens Gustedt Jun 26 '17 at 20:58
  • What is a 'pomp'? In English, it is an adjective ('Pomp and Circumstance March' by Elgar, f'rinstance), and that doesn't apply here where you're using it as a noun. Please check the translation from your language to English. – Jonathan Leffler Jun 26 '17 at 21:03
  • There is almost certainly an issue of some sort with line-buffered input. How you work around it on a Raspberry Pi, I don't know. But it is likely that there is a way to do it, that is documented and readily available. – Jonathan Leffler Jun 26 '17 at 21:04
  • guys thanks and i'm sorry for my bad english i will try to ask my question with other sence ,so in my program 1/i need a clock or timer work in the same time with other function ,in my case work with "printf" statement if i put in the console the "C" char ,program still working else stop working so my question how i can put this clock or counter or timer ? 2/i need 2 or multiples actions running in same time so how i can do that ? finally my program work without any problem but he don't work as i like so how i can fix it how i can put a clock or running multiple function in same time – Sekmani52 Jun 27 '17 at 00:26

1 Answers1

0

Sorry if I'be misinterpreted your question.

Basically you want to do two things simultaneously (concurrently):

  1. Control leds
  2. Wait for user interaction via scanf

I'd say there are two options here, either to separate your UI (scanf) into a separate thread or to work with stdio in non-blocking mode so instead of scanf you would be doing read.

tgregory
  • 554
  • 3
  • 9
  • Hi tgregory thanks for reply but how i can work with stidio in non-blocking mode ? – Sekmani52 Jun 28 '17 at 14:26
  • @Sekmani52 take a look at this [question](https://stackoverflow.com/questions/717572/how-do-you-do-non-blocking-console-i-o-on-linux-in-c) Read all the answers, those with O_NONBLOCK switch stdin into nonblocking mode. Keep in mind that this way other code expecting for it to be in the blocking mode may break. – tgregory Jun 28 '17 at 14:49