0
I am developing the following code and I am using global variables that can be changed in different parts of the code, but when I change for example the state variable in the menu() function its value is not changed in the other functions that use it, that is, the variables are global but not updated. Is it because I’m running multiple processes to run all the functions simultaneously or I’m doing something wrong?
/* menu para acesso as funcionalidades */
#include <wiringPi.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdbool.h>
#include <signal.h>
#define sensoralarme 3
#define saidaalarme 9
#define saidaalarme2 26
#define sensoralarme2 11
#define botaoreset 31
#define saidaclimatizacao 12
#define botaoaumenta 0
#define botaodiminui 2
#define saidajanela1 27
#define saidajanela2 23
#define abrirjanela 5
#define fecharjanela 4
#define ldr 29
void menu();
void iluminacacao();
void intrusao();
void janelas();
void toggle();
void disparo();
void reset();
void climatizacao();
void toggleclimatizacao();
//int disparo = (digitalRead(sensoralarme));
int estadoalarme;
int estadojanela;
int estadojanela2 = 3 ;
//int *ptr_int = &estadojanela2;
int rr;
int bright=0 ;
int state=0;
int statedisparo=0;
int statedisparo2;
int toggleCount = 0; // used to count number of falling edges received.
int estadoclimatizacao;
int estadoclimatizacao2;
int main()
{
int i,r, n=5;
wiringPiSetup ();
pinMode (1, PWM_OUTPUT); /* pwm iluminação*/
pinMode (0, INPUT);/*aumenta luminusidade*/
pinMode (2, INPUT); /* diminui luminusidade*/
pinMode (9, OUTPUT); /* saída disparo alarme*/
pinMode (3, INPUT); /*sensor alarme*/
pinMode (12, OUTPUT); /*saída climatização */
pinMode (27, OUTPUT); /*saída janela abrir */
pinMode (23, OUTPUT); /*saída janela fechar */
pinMode (29, INPUT);/*ldr*/
pinMode (5, INPUT);
pinMode (4, INPUT);
pinMode (6, INPUT);
pinMode (25, OUTPUT);/*sensir janela aberta */
pinMode (saidaalarme, OUTPUT);
pinMode (saidaalarme2, OUTPUT);
pinMode (sensoralarme, INPUT);
pinMode (sensoralarme2, INPUT);
pinMode (botaoreset, INPUT);
digitalWrite(saidaalarme2, LOW);
digitalWrite(saidaalarme, LOW);
digitalWrite(saidaalarme,0);
digitalWrite(saidaclimatizacao,0);
digitalWrite(saidajanela1,0);
digitalWrite(saidajanela2,0);
//pwmWrite (1, 0);
printf("Bem Vindo.\n");
printf("Pressione OK para continuar\n");
getchar();
r=fork();
if(r==0)
{
printf(" \n.....A iniciar sistemas iluminacao\n \n ");
iluminacao();
exit(0);
}
r=fork();
if(r==0)
{
printf(" \n.....A iniciar sistemas intrusao \n \n ");
intrusao();
exit(0);
}
r=fork();
if(r==0)
{
printf(" \n.....A iniciar sistemas climatizacao\n \n ");
climatizacao();
exit(0);
}
r=fork();
if(r==0)
{
printf(" \n.....A iniciar sistemas janelas \n \n ");
janelas();
exit(0);
}
delay (500);
system( "clear" );
menu();
/*for(i=0; i<n; i++)
{
if(fork()) break;
}
if(i==n)
{
menu();
}
else
{
printf(" \n.....A iniciar sistemas \n \n ");
}
switch(i)
{
case 1:
{
iluminacao();
break;
}
case 2:
{
intrusao();
break;
}
case 3:
{
climatizacao();
break;
}
case 4:
{
janelas();
break;
}
}*/
/*int r;
r=fork();
if (r==0)
{iluminacao();intrusao();climatizacao();janelas();}
else
{menu();}
return 0;*/
}
void menu()
{
int escolha1;
int r;
int estadoclimatizacao;
int lum;
int estadointrusao;
//printf(" estado janela2:%d\n", *ptr_int);
printf("SISTEMAS \n");
printf("\n");
printf("Escolha umas das seguintes opções:\n");
printf("1. iluminação.\n");
printf("2. Intrusão.\n");
printf("3. Climatização.\n");
printf("4. Janelas.\n");
printf("0. Sair.\n");
scanf("%d", &escolha1);
switch ( escolha1 )
{
case 1:
{
printf("intruduza o nivel de luminusidade desejado de 0 a 10 \n");
scanf("%d", &lum);
bright=lum*102.4;
printf("%d: lum\n",bright);
pwmWrite (1, bright);
menu();
break;
}
case 2:
{
printf(" Para ligar prima 1\n Para desligar prima 0\n");
scanf("%d", &estadointrusao);
if (estadointrusao==1 && state ==0)
{
digitalWrite(saidaalarme, HIGH);
state=1;
menu();
break;
}
else if (estadointrusao==1 && state ==1)
{
printf(" Sistema já se encontra ligado!\n");
menu();
}
if (estadointrusao==0 && state == 1 && statedisparo == 0)
{
digitalWrite(saidaalarme, LOW);
state=0;
menu();
break;
}
else if (estadointrusao==0 && state ==0)
{
printf(" Sistema já se encontra desligado!\n");
menu();
}
else if (estadointrusao==0 && state ==1 && statedisparo == 0)
{
printf(" Sistema terá de ser desligado localmente!\n DISPARO\n");
menu();
}
}
case 3:
{
printf(" Para ligar prima 1\n Para desligar prima 0\n");
scanf("%d", &estadoclimatizacao);
if ( estadoclimatizacao == 1 )
{
digitalWrite(saidaclimatizacao,1);
menu();
}
if ( estadoclimatizacao == 0 )
{
digitalWrite(saidaclimatizacao,0);
menu();
}
}
case 4:
{
printf(" Para abrir prima 1\n Para fechar prima 0\n Para verificar o estado prima 2\n");
scanf("%d", &estadojanela);
if (estadojanela ==2 && estadojanela2 ==1)
{
printf("janela ABERTa \n");
menu();
}
if (estadojanela==2 && estadojanela2 == 0)
{
printf("janela fechada \n");
menu();
}
if (estadojanela !=2 && rr==0)
{
menu();
//sleep(600);
}
else
{
printf("passar para processo ja aberto \n");
menu();
}
}
case 0:
{
kill(0, SIGTERM);
/*comando para matar todos os processos criados*/
digitalWrite(saidaalarme2, LOW);
digitalWrite(saidaalarme, LOW);
digitalWrite(saidaalarme,0);
digitalWrite(saidaclimatizacao,0);
digitalWrite(saidajanela1,0);
digitalWrite(saidajanela2,0);
pwmWrite (1, 0);
return (0);
break;
}
}
}
void iluminacao()
{
//int buttonmais = (digitalRead(0) );
int buttonmenos = (digitalRead(2) );
printf ("dimmer\n") ;
if (wiringPiSetup () == -1)
exit (1) ;
pwmWrite (1, bright);
while (1)
{
while (digitalRead(0) != LOW && digitalRead(2) == LOW && digitalRead(29) !=0)
{
if (bright < 1024 && bright >= 0)
{
bright = bright+1;
pwmWrite (1, bright) ;
delay (4) ;
}
}
while ( digitalRead(2) != LOW && digitalRead(0) == LOW && digitalRead(29) !=0)
{
if (bright <= 1024 && bright > 0)
{
bright = bright-1;
pwmWrite (1, bright) ;
delay (4) ;
}
}
if (digitalRead(0) != LOW && digitalRead(2) != LOW)
{
bright = 0;
pwmWrite (1, bright);
}
}
}
void janelas()
{
while (1)
{
if ( estadojanela == 1 && digitalRead(25)==0 )/*abre janela via linha de comandos*/
{
digitalWrite(saidajanela1,1);
digitalWrite(saidajanela2,0);
delay(500);
estadojanela2 = 1;
estadojanela=3;
printf("ABERTO \n");
digitalWrite(saidajanela1,0);
digitalWrite(25,1);
}
if ( estadojanela == 0 && digitalRead(25)==1)/* fecha janela via linha de comandos*/
{
digitalWrite(saidajanela1,0);
digitalWrite(saidajanela2,1);
delay(500);
estadojanela2=0;
estadojanela=3;
printf("Fechado \n");
digitalWrite(saidajanela2,0);
digitalWrite(25,0);
}
if ( estadojanela == 1 && (digitalRead(saidajanela1)==1) )/*para abertura de janela*/
{
digitalWrite(saidajanela1,0);
digitalWrite(saidajanela2,0);
printf("ABERTura parada \n");
}
if ( estadojanela == 0 && (digitalRead(saidajanela2)==1) )/*para fecho da janela*/
{
digitalWrite(saidajanela1,0);
digitalWrite(saidajanela2,0);
printf("fecho parada \n");
}
if ( digitalRead(5)!=0 && digitalRead(25)==0 )/*abre janela via linha de comandos*/
{
digitalWrite(saidajanela1,1);
digitalWrite(saidajanela2,0);
delay(500);
estadojanela2 = 1;
estadojanela=3;
printf("ABERTO \n");
digitalWrite(saidajanela1,0);
digitalWrite(25,1);
}
if ( digitalRead(4)!=0 && digitalRead(25)==1)
{
digitalWrite(saidajanela1,0);
digitalWrite(saidajanela2,1);
delay(500);
estadojanela2=0;
estadojanela=3;
printf("Fechado \n");
digitalWrite(saidajanela2,0);
digitalWrite(25,0);
}
}
}
void intrusao()
{
if (wiringPiISR(sensoralarme,INT_EDGE_FALLING,&toggle) != 0)
{
printf("Registering ISR failed\n");
return 2;
}
if (wiringPiISR(sensoralarme2,INT_EDGE_FALLING,&disparo) != 0)
{
printf("Registering ISR failed\n");
return 2;
}
if (wiringPiISR(botaoreset,INT_EDGE_FALLING,&reset) != 0)
{
printf("Registering ISR failed\n");
return 2;
}
if ( saidaalarme == 1 && sensoralarme2 == 1)
{
digitalWrite(saidaalarme2, HIGH);
delay(10);
}
// wait for ever so the program doesn't terminate.
while(1)
{
// Yield the thread so CPU doesn't run 100%
delay(5000);
}
}
void toggle()
{
// NOTE: LED is connected to +3.3V and to the PIN.
// So configuration is active low.
if(state==1 && statedisparo ==0)
{
// Set -ve lead of LED to ground.
digitalWrite(saidaalarme,LOW);
state = 0;
printf("%d: ALARME OFF\n", toggleCount++);
}
else
{
// Set -ve lead of LED to +3.3V.
digitalWrite(saidaalarme, HIGH);
state = 1;
printf("%d: ALARME ON\n", toggleCount++);
}
}
void disparo()
{
//statedisparo = 0;
if(state==1 && statedisparo==0)
{
// Set -ve lead of LED to ground.
digitalWrite(saidaalarme2,LOW);
statedisparo = 1;
statedisparo2 = 1;
// printf("%d: disparo off\n", toggleCount++);
/* do
{
digitalWrite (LedPin2, HIGH) ; delay (300) ;
digitalWrite (LedPin2, LOW) ; delay(300) ;
}
while (state==1);*/
while(statedisparo2==1)
{
digitalWrite (saidaalarme2, HIGH) ; delay (300) ;
digitalWrite (saidaalarme2, LOW) ; delay(300) ;
}
}
else if(state==0 && /*ButtonPin2 ==1*/ statedisparo == 0)
{
// Set -ve lead of LED to +3.3V.
digitalWrite(saidaalarme2, HIGH);
//statedisparo = 1;
printf("%d: ALARME OFF\n", toggleCount++);
printf("%d: disparo ON\n", toggleCount++);
digitalWrite (saidaalarme2, HIGH) ; delay (500) ;
pwmWrite (1, 256);
digitalWrite (saidaalarme2, LOW) ;
}
}
void reset()
{
if(state == 1)
{
// Set -ve lead of LED to +3.3V.
digitalWrite(saidaalarme2, LOW);
digitalWrite(saidaalarme, LOW);
//statedisparo = 1;
printf("%d: RESET\n", toggleCount++);
state=0;
statedisparo=0;
statedisparo2=0;
return;
}
}
void climatizacao()
{
//int flag=0;
printf("climatização\n");
if (wiringPiISR(digitalRead(6),INT_EDGE_FALLING,&toggleclimatizacao) != 0)
{
printf("Registering ISR failed\n");
return 2;
}
while (1)
{
//if (digitalRead(6)!=0 && estadoclimatizacao2==0 && flag==0)
//{
//digitalWrite(saidaclimatizacao,1);
//flag=1;
// estadoclimatizacao2=1;
//}
//if (digitalRead(6)!=0 && estadoclimatizacao2==1 && flag ==1)
//{
//digitalWrite(saidaclimatizacao,0);
//flag=0;
// estadoclimatizacao2=0;
//}
delay(5000);
}
}
void toggleclimatizacao()
{
if(estadoclimatizacao==1 && statedisparo ==0)
{
digitalWrite(saidaclimatizacao,LOW);
estadoclimatizacao = 0;
printf(" climatizacao OFF\n");
}
else
{
// Set -ve lead of LED to +3.3V.
digitalWrite(saidaclimatizacao, HIGH);
estadoclimatizacao = 1;
printf("climatizacao ON\n");
}
}
Can I post the chunk of code that has the problem? It’s hard to try to help when I have 17 screens of code to read
– Jefferson Quesado
I won’t even try to help for one reason: this code has so many problems that solving it won’t change anything. I’m sorry for the honesty, but just starting over, this time the right way.
– Maniero
I believe that the code has many problems, but for those who have very little knowledge in the area it is not easy to recognize certain problems, and it would be better to start again in the correct way, but it would be necessary to know which is the correct way. But thanks for the help and advice.
– Tiago Meireles