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?