Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<ctype.h>
- # include <conio.h>
- # define CLS system("cls")
- void initialize(void);
- void screen(void);
- void input(void);
- void OpenScreen(void);
- int get_opt(int,int,char *,int,int);
- int inv_opt(char *,int,int);
- int get_cur(int,int,char *);
- int inv_cur(char *);
- int hilimit(int);
- int lolimit(int);
- int radon(void);
- int radoff(void);
- int sprnkon(char);
- int sprnkoff(char);
- int venthopen(char);
- int ventfopen(char);
- int venthclos(char);
- int ventfclos(char);
- void tmpcontrol(void);
- void humcontrol(void);
- void wincontrol(void);
- void toohot(void);
- void toocold(void);
- void toohum(void);
- void toodry(void);
- void toowin(void);
- void alarm(char);
- int resetflag=0,quitflag=0;
- int optmp=0,ophum=0,opwin=0;
- int cutmp=0,cuhum=0,cuwin=0;
- int max=0,min=0;
- static int sprnk[2];
- static int vents[5];
- static int rads[4];
- main()
- {
- while(quitflag==0)
- {
- initialize();
- screen();
- input();
- while((resetflag==0)&&(quitflag==0))
- {
- screen();
- /* --------------------- temperature section -------------------------- */
- do
- {
- cutmp=get_cur(20,9,"Temperature");
- }
- while(cutmp==-1);
- max=min=0;
- max=hilimit(optmp);
- min=lolimit(optmp);
- tmpcontrol();
- /* --------------------- humidity section --------------------------- */
- if((quitflag==0)&&(resetflag==0))
- {
- screen();
- do
- {
- cuhum=get_cur(20,9,"Humidity");
- }
- while(cuhum==-1);
- max=min=0;
- max=hilimit(ophum);
- min=lolimit(ophum);
- humcontrol();
- }
- /*--------------------- draught section ----------------------------*/
- if((quitflag==0)&&(resetflag==0))
- {
- screen();
- do
- {
- cuwin=get_cur(20,9,"Draught");
- }
- while(cuwin==-1);
- max=min=0;
- max=hilimit(opwin);
- min=lolimit(opwin);
- wincontrol();
- }
- /*-------------------------------------------------------------------*/
- } /* end of quit & reset loop */
- } /* end off quit loop */
- } /* end of main */
- /*---------------- Clear screen and flush keyboard buffer -----------*/
- void OpenScreen(void)
- {
- CLS;
- fflush(stdin);
- }
- /*------------------------ display screen ----------------------------*/
- void screen(void)
- {
- OpenScreen();
- putchar(26);
- gotoxy(23,1);
- printf("***** Green House Status *****");
- gotoxy(9,3);
- printf("Sprinklers Radiators Vents");
- gotoxy(9,5);
- printf("SP1 SP2 R1 R2 R3 R4 V1 V2 V3 V4 V5");
- gotoxy(9,6);
- printf("--- --- -- -- -- -- -- -- -- -- --");
- gotoxy(9,7);
- printf(" %d %d %d %d %d %d %d %d %d %d %d",
- sprnk[0],sprnk[1],rads[0],rads[1],rads[2],rads[3],vents[0],vents[1],vents[2],vents[3],vents[4]);
- gotoxy(9,11);
- printf(" Optimum Optimum Optimum");
- gotoxy(9,12);
- printf("Temperature Humidity Draught");
- gotoxy(9,13);
- printf(" %2d %2d %2d",
- optmp,ophum,opwin);
- gotoxy(9,16);
- printf(" Current Current Current");
- gotoxy(9,17);
- printf("Temperature Humidity Draught");
- gotoxy(9,18);
- printf(" %2d %2d %2d",
- cutmp,cuhum,cuwin);
- gotoxy(9,22);
- printf("Key: Rads/Sprnk 0=Off,1=On. Vents 0=Shut,1/2=Half/Full Open.");
- }
- /*----------------------- get optimum settings ----------------------*/
- int get_opt(int x,int y,char *strng,int min,int max)
- {
- char ip[10];
- int op;
- gotoxy(x,y);
- printf(" ");
- gotoxy(x,y);
- printf("Enter Optimum %s (%d-%d) : ",strng,min,max);
- fflush(stdin);
- gets(ip);
- op=inv_opt(ip,min,max);
- return op;
- }
- /*-------------- test for invalid optimum settings ------------------*/
- int inv_opt(char *strng,int min,int max)
- {
- int op;
- char *p;
- p=strng;
- while(*p!=NULL)
- {
- if(!(isdigit(*p))) return 0;
- p++;
- }
- op=atoi(strng);
- if((op<min)||(op>max))
- {
- return 0;
- }
- else return op;
- }
- /*-------------------- get current settings ------------------------*/
- int get_cur(int x,int y,char *strng)
- {
- char ip[10];
- int op;
- gotoxy(x,y);
- printf(" ");
- gotoxy(x,y);
- printf("Enter Current %s (Q)uit or (R)eset : ",strng);
- fflush(stdin);
- gets(ip);
- op=inv_cur(ip);
- return op;
- }
- /*-------------------- test for quit / reset or setting --------------*/
- int inv_cur(char *strng)
- {
- int op;
- char *p;
- p=strng;
- while(*p!=NULL)
- {
- if((*p=='q')||(*p=='Q'))
- {
- quitflag=1;
- exit();
- }
- else if((*p=='r')||(*p=='R'))
- {
- resetflag=1;
- }
- else if(!(isdigit(*p))) return -1;
- p++;
- }
- op=atoi(strng);
- return op;
- }
- /*---------------------- Initialization ----------------------------*/
- void initialize(void)
- {
- min=max=0;
- resetflag=quitflag=0;
- optmp=ophum=opwin=0;
- cutmp=cuhum=cuwin=0;
- sprnk[0]=sprnk[1]=0;
- rads[0]=rads[1]=rads[2]=rads[3]=0;
- vents[0]=vents[1]=vents[2]=vents[3]=vents[4]=0;
- }
- /*---------------------- get user input ------------------------------*/
- void input(void)
- {
- do
- {
- optmp=get_opt(20,9,"Temperature",5,99);
- }
- while(optmp==0);
- do
- {
- ophum=get_opt(20,9,"Humidity",5,99);
- }
- while(ophum==0);
- do
- {
- opwin=get_opt(20,9,"Draught",5,20);
- }
- while(opwin==0);
- }
- /*--------------------------- hi-limit --------------------------------*/
- int hilimit(int opt)
- {
- int op1,op2;
- if(opt<10)
- {
- op1=opt+2;
- return op1;
- }
- if(opt>=10)
- {
- op2=(opt+(opt*0.1)+1);
- return op2;
- }
- }
- /*--------------------------- lo-limit ------------------------------*/
- int lolimit(int opt)
- {
- int op1,op2;
- if(opt<10)
- {
- op1=opt-2;
- return op1;
- }
- if(opt>=10)
- {
- op2=(opt-(opt*0.1));
- return op2;
- }
- }
- /*---------------------- attempt radiator on --------------------------*/
- int radon(void)
- {
- int i=0;
- for(i=0;i<4;i++)
- {
- if(rads[i]==0)
- {
- rads[i]=1;
- cutmp=cutmp+2;
- return 1;
- }
- }
- return 0;
- }
- /*--------------------- attempt radiator off -------------------------*/
- int radoff(void)
- {
- int i=0;
- for(i=0;i<4;i++)
- {
- if(rads[i]==1)
- {
- rads[i]=0;
- cutmp=cutmp-2;
- return 1;
- }
- }
- return 0;
- }
- /*------------------- attempt turn sprinkler on -----------------------*/
- int sprnkon(char mode)
- {
- int i=0;
- for(i=0;i<2;i++)
- {
- if(sprnk[i]==0)
- {
- sprnk[i]=1;
- if(mode=='t') cutmp=cutmp-2;
- if(mode=='h') cuhum=cuhum+2;
- return 1;
- }
- }
- return 0;
- }
- /*------------------- attempt turn sprinkler off ----------------------*/
- int sprnkoff(char mode)
- {
- int i=0;
- for(i=0;i<2;i++)
- {
- if(sprnk[i]==1)
- {
- sprnk[i]=0;
- if(mode=='t') cutmp=cutmp+2;
- if(mode=='h') cuhum=cuhum-2;
- return 1;
- }
- }
- return 0;
- }
- /*---------------------- attempt 1/2 open vent -----------------------*/
- int venthopen(char mode)
- {
- int i=0;
- for(i=0;i<5;i++)
- {
- if(vents[i]==0)
- {
- vents[i]=1;
- if(mode=='t') cutmp=cutmp-1;
- if(mode=='h') cuhum=cuhum-1;
- return 1;
- }
- }
- return 0;
- }
- /*-------------------- attempt fully open vent -----------------------*/
- int ventfopen(char mode)
- {
- int i=0;
- for(i=0;i<5;i++)
- {
- if(vents[i]==1)
- {
- vents[i]=2;
- if(mode=='t') cutmp=cutmp-1;
- if(mode=='h') cuhum=cuhum-1;
- return 1;
- }
- }
- return 0;
- }
- /*-------------------- attempt 1/2 close vent -----------------------*/
- int venthclos(char mode)
- {
- int i=0;
- for(i=0;i<5;i++)
- {
- if(vents[i]==2)
- {
- vents[i]=1;
- if(mode=='t') cutmp=cutmp+1;
- if(mode=='h') cuhum=cuhum+1;
- if(mode=='d') cuwin=cuwin-1;
- return 1;
- }
- }
- return 0;
- }
- /*-------------------- attempt fully close vent -----------------------*/
- int ventfclos(char mode)
- {
- int i=0;
- for(i=0;i<5;i++)
- {
- if(vents[i]==1)
- {
- vents[i]=0;
- if(mode=='t') cutmp=cutmp+1;
- if(mode=='h') cuhum=cuhum+1;
- if(mode=='d') cuwin=cuwin-1;
- return 1;
- }
- }
- return 0;
- }
- /*--------------------- temperature control ------------------------*/
- void tmpcontrol(void)
- {
- if(cutmp>=max) toohot();
- else
- if(cutmp<=min) toocold();
- }
- /*--------------------- humidity control ---------------------------*/
- void humcontrol(void)
- {
- if(cuhum>=max) toohum();
- else
- if(cuhum<=min) toodry();
- }
- /*--------------------- draught control ----------------------------*/
- void wincontrol(void)
- {
- if(cuwin>=max) toowin();
- }
- /*---------------------- too hot, Phew !!! ------------------------*/
- void toohot(void)
- {
- while((cutmp>=max)&&(resetflag==0))
- {
- if(radoff()==0)
- if(venthopen('t')==0)
- if(ventfopen('t')==0)
- if(sprnkon('t')==0) alarm('t');
- } }
- /*-------------------- too cold, Brrrr !!! ---------------------------*/
- void toocold(void)
- {
- while((cutmp<=min)&&(resetflag==0))
- {
- if(venthclos('t')==0)
- if(ventfclos('t')==0)
- if(sprnkoff('t')==0)
- if(radon()==0) alarm('c');
- } }
- /*------------------------ too humid -------------------------------*/
- void toohum(void)
- {
- while((cuhum>=max)&&(resetflag==0))
- {
- if(sprnkoff('h')==0)
- if(venthopen('h')==0)
- if(ventfopen('h')==0) alarm('h');
- } }
- /*----------------------- too dry ----------------------------------*/
- void toodry(void)
- {
- while((cuhum<=min)&&(resetflag==0))
- {
- if(sprnkon('h')==0)
- if(venthclos('h')==0)
- if(ventfclos('h')==0) alarm('d');
- } }
- /*-------------------------- too win --------------------------------*/
- void toowin(void)
- {
- while((cuwin>=max)&&(resetflag==0))
- {
- if(venthclos('d')==0)
- if(ventfclos('d')==0) alarm('w');
- } }
- /*---------------------------- alarm -------------------------------*/
- void alarm(char mode)
- {
- putchar(26);
- gotoxy(30,11);
- printf("*** Warning ! ***");
- gotoxy(34,13);
- if(mode=='t'){gotoxy(34,13);printf("Too Hot !");}
- if(mode=='d'){gotoxy(34,13);printf("Too Dry !");}
- if(mode=='c'){gotoxy(33,13);printf("Too Cold !");}
- if(mode=='h'){gotoxy(33,13);printf("Too Humid !");}
- if(mode=='w'){gotoxy(32,13);printf("Too Draughty !");}
- gotoxy(25,15);
- printf("Environment Limits Exceeded !");
- gotoxy(28,17);
- printf("Attend Fault and Reset !");
- resetflag=1;
- getch();
- }
Advertisement
Comments
-
- What a mess.
- I used structs and switch statements to clean this up considerably. I don't have all of your program, so I cannot test it. Please let me know if it works out!
- https://pastebin.com/dcikSLfr
-
- It was a beginners course mate :)
- And, that is all of it :)
Add Comment
Please, Sign In to add comment
Advertisement