View difference between Paste ID: cGgCbcqe and rYjv3XW8
SHOW: | | - or go back to the newest paste.
1
#include<stdio.h>
2
#include<ctype.h>
3
# include <conio.h>
4
# define CLS system("cls")
5
6
void initialize(void);
7
void screen(void);
8
void input(void);
9
void OpenScreen(void);
10
11
int get_opt(int,int,char *,int,int);
12
int inv_opt(char *,int,int);
13
int get_cur(int,int,char *);
14
int inv_cur(char *);
15
16
int hilimit(int);
17
int lolimit(int);
18
19
int radon(void);
20
int radoff(void);
21
int sprnkon(char);
22
int sprnkoff(char);
23
int venthopen(char);
24
int ventfopen(char);
25
int venthclos(char);
26
int ventfclos(char);
27
28
void tmpcontrol(void);
29
void humcontrol(void);
30
void wincontrol(void);
31
32
void toohot(void);
33
void toocold(void);
34
void toohum(void);
35
void toodry(void);
36
void toowin(void);
37
void alarm(char);
38
39
int resetflag=0,quitflag=0;
40
int optmp=0,ophum=0,opwin=0;
41
int cutmp=0,cuhum=0,cuwin=0;
42
int max=0,min=0;
43
static int sprnk[2];
44
static int vents[5];
45
static int rads[4];
46
47
main()
48
{
49
while(quitflag==0)
50
     {
51
     initialize();
52
     screen();
53
     input();
54
55
     while((resetflag==0)&&(quitflag==0))
56
          {
57
          screen();
58
59
/* --------------------- temperature section -------------------------- */
60
              do
61
                {
62
                cutmp=get_cur(20,9,"Temperature");
63
                }
64
              while(cutmp==-1);
65
              max=min=0;
66
              max=hilimit(optmp);
67
              min=lolimit(optmp);
68
              tmpcontrol();
69
70
/* --------------------- humidity section --------------------------- */
71
72
              if((quitflag==0)&&(resetflag==0))
73
                 {
74
                 screen();
75
                  do
76
                    {
77
                    cuhum=get_cur(20,9,"Humidity");
78
                    }
79
                  while(cuhum==-1);
80
                  max=min=0;
81
                  max=hilimit(ophum);
82
                  min=lolimit(ophum);
83
                  humcontrol();
84
                 }
85
86
/*--------------------- draught section ----------------------------*/
87
88
              if((quitflag==0)&&(resetflag==0))
89
                 {
90
                  screen();
91
                  do
92
                    {
93
                    cuwin=get_cur(20,9,"Draught"); 
94
                    }
95
                  while(cuwin==-1);
96
                  max=min=0;
97
                  max=hilimit(opwin);
98
                  min=lolimit(opwin);
99
                  wincontrol();
100
                 }
101
102
/*-------------------------------------------------------------------*/
103
	}  /* end of quit & reset loop */
104
    }     /* end off quit loop */
105
}        /* end of main */
106
107
/*---------------- Clear screen and flush keyboard buffer -----------*/
108
109
void OpenScreen(void)
110
  {
111
  CLS;
112
  fflush(stdin);
113
  }
114
115
/*------------------------ display screen ----------------------------*/
116
117
void screen(void)
118
{
119
OpenScreen();
120
putchar(26);
121
gotoxy(23,1);
122
printf("***** Green House Status *****");
123
gotoxy(9,3);
124
printf("Sprinklers              Radiators                Vents");
125
gotoxy(9,5);
126
printf("SP1   SP2             R1  R2  R3  R4       V1  V2  V3  V4  V5");
127
gotoxy(9,6);
128
printf("---   ---             --  --  --  --       --  --  --  --  --");
129
gotoxy(9,7);
130
printf(" %d     %d               %d   %d   %d   %d        %d   %d   %d   %d   %d",
131
sprnk[0],sprnk[1],rads[0],rads[1],rads[2],rads[3],vents[0],vents[1],vents[2],vents[3],vents[4]);
132
133
gotoxy(9,11);
134
printf("  Optimum                Optimum                 Optimum");
135
gotoxy(9,12);
136
printf("Temperature              Humidity                Draught");
137
gotoxy(9,13);
138
printf("     %2d                    %2d                      %2d",
139
optmp,ophum,opwin); 
140
gotoxy(9,16);
141
printf("  Current                Current                 Current");
142
gotoxy(9,17);
143
printf("Temperature              Humidity                Draught");
144
gotoxy(9,18);
145
printf("     %2d                    %2d                      %2d",
146
cutmp,cuhum,cuwin); 
147
gotoxy(9,22);
148
printf("Key: Rads/Sprnk 0=Off,1=On. Vents 0=Shut,1/2=Half/Full Open.");
149
}
150
151
/*----------------------- get optimum settings ----------------------*/
152
   
153
int get_opt(int x,int y,char *strng,int min,int max)
154
    {
155
    char ip[10];
156
    int op; 
157
    gotoxy(x,y);
158
    printf("                                                  ");
159
    gotoxy(x,y); 
160
    printf("Enter Optimum %s  (%d-%d)  : ",strng,min,max);
161
    fflush(stdin);
162
    gets(ip);
163
    op=inv_opt(ip,min,max);
164
    return op;
165
    }
166
167
/*-------------- test for invalid optimum settings ------------------*/
168
169
int inv_opt(char *strng,int min,int max)
170
    {
171
    int op; 
172
    char *p;
173
    p=strng;
174
    while(*p!=NULL)
175
                  {
176
                  if(!(isdigit(*p))) return 0;
177
                  p++;
178
                  }
179
    op=atoi(strng);
180
    if((op<min)||(op>max))
181
                         {
182
                         return 0;
183
                         } 
184
    else return op;
185
    }
186
187
/*-------------------- get current settings ------------------------*/
188
189
int get_cur(int x,int y,char *strng)
190
{
191
char ip[10];
192
int op;
193
gotoxy(x,y);
194
printf("                                                    ");
195
gotoxy(x,y);
196
printf("Enter Current %s (Q)uit or (R)eset : ",strng);
197
fflush(stdin);
198
gets(ip);
199
op=inv_cur(ip);
200
return op;
201
}
202
203
/*-------------------- test for quit / reset or setting --------------*/
204
205
int inv_cur(char *strng)
206
    {
207
    int op; 
208
    char *p;
209
    p=strng;
210
    while(*p!=NULL)
211
                  {
212
                  if((*p=='q')||(*p=='Q'))
213
                     {
214
                     quitflag=1;
215
                     exit();
216
                     }
217
                  else if((*p=='r')||(*p=='R'))
218
                          {
219
                          resetflag=1;
220
                          }
221
                  else if(!(isdigit(*p))) return -1;
222
                  p++;
223
                  }
224
    op=atoi(strng);
225
    return op;
226
    }
227
228
/*---------------------- Initialization ----------------------------*/
229
230
void initialize(void)
231
{
232
min=max=0;
233
resetflag=quitflag=0;
234
optmp=ophum=opwin=0;
235
cutmp=cuhum=cuwin=0;
236
sprnk[0]=sprnk[1]=0;
237
rads[0]=rads[1]=rads[2]=rads[3]=0;
238
vents[0]=vents[1]=vents[2]=vents[3]=vents[4]=0;
239
}
240
241
/*---------------------- get user input ------------------------------*/
242
243
void input(void)
244
{
245
     do
246
       {
247
       optmp=get_opt(20,9,"Temperature",5,99);
248
       }
249
     while(optmp==0);
250
     do
251
       {
252
       ophum=get_opt(20,9,"Humidity",5,99);
253
       }
254
     while(ophum==0);
255
     do
256
       {
257
       opwin=get_opt(20,9,"Draught",5,20);
258
       }
259
     while(opwin==0);
260
}
261
262
/*--------------------------- hi-limit --------------------------------*/
263
264
int hilimit(int opt)
265
{
266
int op1,op2;
267
if(opt<10)
268
  {
269
  op1=opt+2;
270
  return op1;
271
  }
272
if(opt>=10)
273
  {
274
  op2=(opt+(opt*0.1)+1);
275
  return op2;
276
  }
277
}
278
279
/*--------------------------- lo-limit ------------------------------*/
280
281
int lolimit(int opt)
282
{
283
int op1,op2;
284
if(opt<10)
285
  {
286
  op1=opt-2;
287
  return op1;
288
  }
289
if(opt>=10)
290
  {
291
  op2=(opt-(opt*0.1));
292
  return op2;
293
  }
294
}
295
296
/*---------------------- attempt radiator on  --------------------------*/
297
298
int radon(void)
299
{
300
int i=0;
301
for(i=0;i<4;i++)
302
               {
303
               if(rads[i]==0)
304
                            {
305
                            rads[i]=1;
306
                            cutmp=cutmp+2;
307
                            return 1;
308
                            }
309
               }
310
return 0;
311
}
312
313
/*--------------------- attempt radiator off  -------------------------*/
314
315
int radoff(void)
316
{
317
int i=0;
318
for(i=0;i<4;i++)
319
               {
320
               if(rads[i]==1)
321
                            {
322
                            rads[i]=0;
323
                            cutmp=cutmp-2;
324
                            return 1;
325
                            }
326
               }
327
return 0;
328
}
329
330
/*------------------- attempt turn sprinkler on  -----------------------*/
331
332
int sprnkon(char mode)
333
{
334
int i=0;
335
for(i=0;i<2;i++)
336
               {
337
               if(sprnk[i]==0) 
338
                             {
339
                             sprnk[i]=1;
340
                             if(mode=='t') cutmp=cutmp-2;
341
                             if(mode=='h') cuhum=cuhum+2; 
342
                             return 1;
343
                             }
344
               }
345
return 0;
346
}
347
348
/*------------------- attempt turn sprinkler off ----------------------*/
349
350
int sprnkoff(char mode)
351
{
352
int i=0;
353
for(i=0;i<2;i++)
354
               {
355
               if(sprnk[i]==1) 
356
                             {
357
                             sprnk[i]=0;
358
                             if(mode=='t') cutmp=cutmp+2;
359
                             if(mode=='h') cuhum=cuhum-2; 
360
                             return 1;
361
                             }
362
               }
363
return 0;
364
}
365
366
/*---------------------- attempt 1/2 open vent -----------------------*/
367
368
int venthopen(char mode)
369
{
370
int i=0;
371
for(i=0;i<5;i++)
372
               {
373
               if(vents[i]==0)
374
                             {
375
                             vents[i]=1;
376
                             if(mode=='t') cutmp=cutmp-1; 
377
                             if(mode=='h') cuhum=cuhum-1; 
378
                             return 1;
379
                             }
380
               }
381
return 0;
382
}
383
384
/*-------------------- attempt fully open vent -----------------------*/
385
386
int ventfopen(char mode)
387
{
388
int i=0;
389
for(i=0;i<5;i++)
390
               {
391
               if(vents[i]==1)
392
                             {
393
                             vents[i]=2;
394
                             if(mode=='t') cutmp=cutmp-1; 
395
                             if(mode=='h') cuhum=cuhum-1; 
396
                             return 1;
397
                             }
398
               }
399
return 0;
400
}
401
402
/*-------------------- attempt 1/2 close vent -----------------------*/
403
404
int venthclos(char mode)
405
{
406
int i=0;
407
for(i=0;i<5;i++)
408
               {
409
               if(vents[i]==2)
410
                             {
411
                             vents[i]=1;
412
                             if(mode=='t') cutmp=cutmp+1; 
413
                             if(mode=='h') cuhum=cuhum+1; 
414
                             if(mode=='d') cuwin=cuwin-1; 
415
                             return 1;
416
                             }
417
               }
418
return 0;
419
}
420
421
/*-------------------- attempt fully close vent -----------------------*/
422
423
int ventfclos(char mode)
424
{
425
int i=0;
426
for(i=0;i<5;i++)
427
               {
428
               if(vents[i]==1)
429
                             {
430
                             vents[i]=0;
431
                             if(mode=='t') cutmp=cutmp+1; 
432
                             if(mode=='h') cuhum=cuhum+1; 
433
                             if(mode=='d') cuwin=cuwin-1; 
434
                             return 1;
435
                             }
436
               }
437
return 0;
438
}
439
440
/*--------------------- temperature control ------------------------*/
441
442
void tmpcontrol(void)
443
{
444
if(cutmp>=max) toohot();
445
else
446
if(cutmp<=min) toocold();
447
}
448
                            
449
/*--------------------- humidity control ---------------------------*/
450
451
void humcontrol(void)
452
{
453
if(cuhum>=max) toohum(); 
454
else
455
if(cuhum<=min) toodry(); 
456
}
457
458
/*--------------------- draught control ----------------------------*/
459
460
void wincontrol(void)
461
{
462
if(cuwin>=max) toowin();
463
}
464
465
/*---------------------- too hot, Phew !!! ------------------------*/
466
467
void toohot(void)
468
{
469
while((cutmp>=max)&&(resetflag==0))
470
               {
471
               if(radoff()==0)
472
                 if(venthopen('t')==0)
473
                   if(ventfopen('t')==0)
474
                     if(sprnkon('t')==0) alarm('t');
475
}              }
476
477
/*-------------------- too cold, Brrrr !!! ---------------------------*/
478
479
void toocold(void)
480
{
481
while((cutmp<=min)&&(resetflag==0))
482
               {
483
               if(venthclos('t')==0)
484
                 if(ventfclos('t')==0)
485
                   if(sprnkoff('t')==0)
486
                     if(radon()==0) alarm('c');
487
}              }
488
489
/*------------------------ too humid -------------------------------*/
490
491
void toohum(void)
492
{
493
while((cuhum>=max)&&(resetflag==0))
494
               {
495
               if(sprnkoff('h')==0)
496
                 if(venthopen('h')==0)
497
                   if(ventfopen('h')==0) alarm('h');
498
}              }
499
500
/*----------------------- too dry ----------------------------------*/
501
502
void toodry(void)
503
{
504
while((cuhum<=min)&&(resetflag==0))
505
               {
506
               if(sprnkon('h')==0)
507
                 if(venthclos('h')==0)
508
                   if(ventfclos('h')==0) alarm('d');
509
}              }
510
511
/*-------------------------- too win --------------------------------*/
512
513
void toowin(void)
514
{
515
while((cuwin>=max)&&(resetflag==0))
516
               {
517
               if(venthclos('d')==0)
518
                 if(ventfclos('d')==0) alarm('w');
519
}              }
520
521
/*---------------------------- alarm -------------------------------*/
522
void alarm(char mode)
523
{
524
putchar(26);
525
gotoxy(30,11);
526
printf("*** Warning ! ***");
527
gotoxy(34,13);
528
if(mode=='t'){gotoxy(34,13);printf("Too Hot !");}
529
if(mode=='d'){gotoxy(34,13);printf("Too Dry !");}
530
if(mode=='c'){gotoxy(33,13);printf("Too Cold !");}
531
if(mode=='h'){gotoxy(33,13);printf("Too Humid !");}
532
if(mode=='w'){gotoxy(32,13);printf("Too Draughty !");}
533
gotoxy(25,15);
534
printf("Environment Limits Exceeded !");
535
gotoxy(28,17);
536
printf("Attend Fault and Reset !");
537
resetflag=1;
538
getch();
539
}
540