Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[Moved] Debugging MikroC Code

Status
Not open for further replies.

thandana

Junior Member level 1
Joined
Nov 27, 2014
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
367
Hi All,

I have the below code and I'm testing these codes:

  • 20!4#d2 - turn OFF/ON light after 2minutes
  • 20!4#t30 - to set the threshold temperature to 30 degrees celcius.
The issue i have is that when i send these code i keep getting SMS "Incorrect Code recieved". Can someone please show me how i can debug this as i dont know why its sending this SMS, can someone point me in right direction as to why im getting this SMS.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
#define ON   1
#define OFF  0
 
#define SET 1
#define CLEAR 0
 
#define HIGH 1
#define LOW  0
 
#define OPEN 1
#define CLOSE 0
 
#define TRUE 1
#define FALSE 0
 
#define ENABLE 1
#define DISABLE 0
 
#define ENABLED 1
#define DISABLED 0
 
#define ACTIVE 1
#define INACTIVE 0
 
#define First_Time_Use_Address 0x01
#define Temperature_Threshold_Address 0x02
#define Event_Address 0x03
 
// GSM module connections
sbit gsmReset at LATB4_bit;
sbit gsmReset_Direction at TRISB4_bit;
// End of GSM module connections
 
sbit LIGHT at LATB7_bit;
sbit ALARM at LATC2_bit;
sbit GATE  at LATB6_bit;
 
sbit NORMAL_VOLTAGE_LED at LATC0_bit;
sbit UNDER_VOLTAGE_LED at LATC1_bit;
sbit OVER_VOLTAGE_LED at LATB2_bit;
 
char myFlagsA = 0, myFlagsB = 0;
 
sbit gateStatusFlag at myFlagsA.B0;
sbit mainsStatusFlag at myFlagsA.B1;
sbit alarmStatusFlag at myFlagsA.B2;
sbit doOnceFlag at myFlagsA.B3;
sbit doOnceFlag2 at myFlagsA.B4;
sbit turnOffLightsFlag at myFlagsA.B5;
sbit timerOnFlag at myFlagsA.B6;
sbit lightStatusFlag at myFlagsA.B7;
 
sbit sendSmsFlag at myFlagsB.B0;
sbit firstTimeUseFlag at myFlagsB.B1;
 
char gsmBuffer[270];
char message[50];
char buffer1[50];
char buffer2[50];
char sms[100];
char smsIndex[4];
char value[4];
char str[23];
char lightDelayCode[10];
char temperatureThresholdData[5];
char gsmAttempt = 0;
char port = 0;
unsigned int diff = 0;
 
unsigned int iTemperature = 0, iPreviousTemperature = 0;
unsigned int iTemperatureThreshold = 0;
unsigned int delayCounter = 0, lightBlinkCounter = 0;
unsigned long lightDelayValue = 0, noOf200MilliSecondsCounter = 0, gsmBufferIndex = 0;
unsigned int addr = 0, val = 0;
unsigned char iEvent = 15;
unsigned int mainsVoltage = 0, prevMainsVoltage = 0;
 
double fTemperature = 0.0, fPreviousTemperature = 0.0;
 
//GSM Constant Strings
const char atCommand1[] = "AT\r";
const char atCommand2[] = "ATE0\r";
const char atCommand3[] = "ATE1\r";
const char atCommand4[] = "AT+IPR=9600\r";
const char atCommand5[] = "AT+CMGF=1\r";
const char atCommand6[] = "AT+CPMS="SM","SM","SM"\r";
const char atCommand7[] = "AT+CNMI=2,1\r";
const char atCommand8[] = "AT+CMGR=";
const char atCommand9[] = "AT+CMGS="XXXXXX"\r";
const char atCommandA[] = "AT+CMGD=1,4\r";
 
const char atResponse1[] = "\r\nOK\r\n";
const char atResponse2[] = "ERROR";
const char atResponse3[] = "+CMTI: "SM",";
const char atResponse4[] = "> ";
const char atResponse5[] = "RING";
const char atResponse6[] = "NO CARRIER";
const char atResponse7[] = "NO ANSWER";
const char atResponse8[] = "+CFUN: 1";
const char atResponse9[] = "+CPIN: READY";
 
 
 
const char secretCode[]  = "20!4";
const char secretCode0[] = "20!4#l1";  //to turn lights ON
const char secretCode1[] = "20!4#l0";  //to turn lights OFF
const char secretCode2[] = "20!4#ls";  //to get STATUS of lights
const char secretCode3[] = "20!4#gt";  //to get temperature reading
const char secretCode4[] = "20!4#da";  //to de-activate the alarm
const char secretCode5[] = "20!4#aa";  //to activate the alarm
const char secretCode6[] = "20!4#as";  //to get the STATUS of the alarm
const char secretCode7[] = "20!4#cg";  //to close the electric gate
const char secretCode8[] = "20!4#og";  //to open the electric gate
const char secretCode9[] = "20!4#gs";  //to get the STATUS of the electric gate to see if its OPEN/CLOSED
const char secretCodeA[] = "20!4#ps";  //to get the STATUS of the power supply if its interrupted or not
const char secretCodeB[] = "20!4#rt";  //to get set temperature thtreshold value
const char secretCodeC[] = "20!4#d";   //to set Light On / OFF Delay
const char secretCodeD[] = "20!4#t";   //to set Light On / OFF Delay
 
const char sms1[] = "Light is ON";
const char sms2[] = "Light is OFF";
const char sms3[] = "Gate is open";
const char sms4[] = "Gate is closed";
const char sms5[] = "Alarm is ON";
const char sms6[] = "Alarm is OFF";
const char sms7[] = "Temperature is high";
const char sms8[] = "Power supply is OK";
const char sms9[] = "Power supply is not OK";
const char smsA[] = "Temperature Threshold is set to ";
const char smsB[] = " degree C";
const char smsC[] = "Temperature is ";
const char smsD[] = "Cannot control Gate. Voltage is not Ok";
const char smsE[] = "Incorrect Code Received";
const char smsF[] = "Light Will Turn ON after ";
const char smsG[] = "Light will turn OFF after ";
const char smsH[] = " minutes";
const char smsI[] = "First time use. ";
const char smsJ[] = " Threshold is not set.";
 
//Function Prototypes
char *CopyConst2Ram(char *dest, const char *src);
 
void DelayXSec(unsigned long int sec);
 
char *CopyConst2Ram(char *dest, const char *src);
void GSM_RESET();
void Extract_SMS(char *s1, char *s2);
void Get_SMS_gsmBufferIndex(char *s1, char *s2);
void Get_GSM_Time(char *s1, char *s2);
void Get_Code(char *s1, char *s2);
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr);
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr);
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr);
 
#define SEND_AT_COMMAND GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand1, &gsmBufferIndex, &gsmAttempt, 1);
#define SET_GSM_BAUDRATE  GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand4, &gsmBufferIndex, &gsmAttempt, 1);
#define SET_GSM_TEXT_MODE GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand5, &gsmBufferIndex, &gsmAttempt, 1);
#define SET_SIM_MEMORY_FOR_SMS GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand6, &gsmBufferIndex, &gsmAttempt, 1);
#define ENABLE_NEW_SMS_NOTIFICATION GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommand7, &gsmBufferIndex, &gsmAttempt, 1);
#define DELETE_ALL_SMS GSM_Send_Const_Command(gsmBuffer, buffer1, atResponse1, atCommandA, &gsmBufferIndex, &gsmAttempt, 1);
#define READ_SMS GSM_Get_SMS(gsmBuffer, buffer1, atResponse1, atCommand8, smsIndex, &gsmBufferIndex, &gsmAttempt, sms, 1);
#define SEND_SMS Send_SMS(gsmBuffer, buffer1, atResponse1, atResponse4, atCommand1, atCommand5, atCommand9, sms, &gsmBufferIndex, &gsmAttempt, 1);
 
#define SECRET_CODE_RECEIVED (strstr(sms, CopyConst2Ram(buffer1, secretCode)) != 0)
 
#define SECRET_CODE_TO_TURN_ON_LIGHT_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode0)) == 0)
#define SECRET_CODE_TO_TURN_OFF_LIGHT_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode1)) == 0)
#define SECRET_CODE_TO_GET_STATUS_OF_LIGHT_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode2)) == 0)
#define SECRET_CODE_TO_GET_TEMPERATURE_READING_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode3)) == 0)
#define SECRET_CODE_TO_DE_ACTIVATE_THE_ALARM_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode4)) == 0)
#define SECRET_CODE_TO_ACTIVATE_THE_ALARM_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode5)) == 0)
#define SECRET_CODE_TO_GET_STATUS_OF_ALARM_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode6)) == 0)
#define SECRET_CODE_TO_CLOSE_GATE_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode7)) == 0)
#define SECRET_CODE_TO_OPEN_GATE_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode8)) == 0)
#define SECRET_CODE_TO_GET_STATUS_OF_GATE_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCode9)) == 0)
#define SECRET_CODE_TO_GET_STATUS_OF_POWER_SUPPLY_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCodeA)) == 0)
#define SECRET_CODE_TO_GET_TEMPERATURE_THRESHOLD_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCodeB)) == 0)
#define SECRET_CODE_TO_SET_LIGHT_ON_OFF_DELAY_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCodeC)) == 0)
#define SECRET_CODE_TO_SET_TEMPERATURE_THRESHOLD_RECEIVED (strcmp(sms, CopyConst2Ram(buffer1, secretCodeD)) == 0)
 
#define MAINS_VOLTAGE_IS_NORMAL (!mainsStatusFlag)
#define MAINS_VOLTAGE_IS_ABNORMAL (mainsStatusFlag == 1)
 
#define INITIALIZE_PWM_FOR_BUZZER_AT_2500_KHz { PWM1_Init(2500); }
#define SET_PWM_DUTY_TO_0 { PWM1_Set_Duty(0); }
 
#define ENABLE_SERIAL_INTERRUPT { RCIE_bit = 1; }
#define ENABLE_PERIPHERAL_INTERRUPTS { PEIE_bit = 1; }
#define ENABLE_GLOBAL_INTERRUPT { GIE_bit = 1; }
#define DISABLE_GLOBAL_INTERRUPT { GIE_bit = 0; }
 
#define INITIALIZE_UART_WITH_9600_BPS_BAUD { UART1_Init(9600);  Delay_ms(200); }
 
#define ENABLE_EXTERNAL_INTERRUPT_0 { INT0IE_bit = 1; }
#define CLEAR_EXTERNAL_INTERRUPT_FLAG { INT0IF_bit = 0; }
 
#define READ_TEMPERATURE_THRESHOLD_VALUE_FROM_EEPROM { iTemperatureThreshold = EEPROM_Read(Temperature_Threshold_Address); Delay_ms(20); }
#define READ_PENDING_EVENT_VALUE_FROM_EEPROM { iEvent = EEPROM_Read(Event_Address); Delay_ms(20); }
#define READ_FIRST_TIME_USE_FLAG_FROM_EEPROM { firstTimeUseFlag = EEPROM_Read(First_Time_Use_Address); Delay_ms(20); }
 
#define SAVE_CURRENT_EVENT_VALUE_TO_EEPROM { EEPROM_Write(Event_Address, iEvent); Delay_ms(20); }
 
#define UART_OVERRUN_ERROR (OERR_bit == 1)
#define CLEAR_UART_OVERRUN_ERROR { OERR_bit = 0; }
#define DISABLE_UART_CONTINUOUS_RECEIVE { CREN_bit = 0; }
#define ENABLE_UART_CONTINUOUS_RECEIVE { CREN_bit = 1; }
 
#define READ_GSM_DATA_INTO_BUFFER { gsmBuffer[gsmBufferIndex++] = UART1_Read(); }
#define TERMINATE_GSM_BUFFER { gsmBuffer[gsmBufferIndex] = '\0'; }
 
#define CLEAR_UART_INTERRUPT_FLAG { RCIF_bit = 0; }
 
#define SERIAL_DATA_RECEIVED (RCIF_bit == 1)
 
#define WAIT_A_WHILE_TO_RECEIVE_THE_SMS Delay_ms(3000);
#define GET_SMS_INDEX Get_SMS_Index(gsmBuffer, smsIndex);
 
#define FIRST_TIME_USE (firstTimeUseFlag == 1)
#define CLEAR_FIRST_TIME_USE_FLAG { firstTimeUseFlag = 0; }
 
#define SET_SEND_SMS_FLAG { sendSmsFlag = TRUE; }
#define CLEAR_SEND_SMS_FLAG { sendSmsFlag = FALSE; }
 
#define SET_GATE_STATUS_FLAG { gateStatusFlag = TRUE; }
#define CLEAR_GATE_STATUS_FLAG { gateStatusFlag = FALSE; }
 
#define SET_LIGHT_STATUS_FLAG { lightStatusFlag = TRUE; }
#define CLEAR_LIGHT_STATUS_FLAG { lightStatusFlag = FALSE; }
 
#define SET_GATE_STATUS_FLAG { sendSmsFlag = TRUE; }
#define CLEAR_SEND_SMS_FLAG { sendSmsFlag = FALSE; }
 
#define SET_MAINS_STATUS_FLAG { mainsStatusFlag = TRUE; }
#define CLEAR_MAINS_STATUS_FLAG { mainsStatusFlag = FALSE; }
 
#define CLEAR_EVENT_COUNTER { iEvent = 0; }
 
#define EVENT_IS_0 {iEvent = 0; }
#define EVENT_IS_1 {iEvent = 1; }
#define EVENT_IS_2 {iEvent = 2; }
#define EVENT_IS_3 {iEvent = 3; }
#define EVENT_IS_4 {iEvent = 4; }
#define EVENT_IS_5 {iEvent = 5; }
#define EVENT_IS_6 {iEvent = 6; }
#define EVENT_IS_7 {iEvent = 7; }
#define EVENT_IS_8 {iEvent = 8; }
#define EVENT_IS_9 {iEvent = 9; }
#define EVENT_IS_10 {iEvent = 10; }
#define EVENT_IS_11 {iEvent = 11; }
#define EVENT_IS_12 {iEvent = 12; }
#define EVENT_IS_13 {iEvent = 13; }
#define EVENT_IS_14 {iEvent = 14; }
#define EVENT_IS_15 {iEvent = 15; }
#define EVENT_IS_16 {iEvent = 16; }
#define EVENT_IS_17 {iEvent = 17; }
 
#define SEND_PULSE_MOTOR_TO_OPEN_GATE { GATE = 1; Delay_ms(2000); GATE = 0; }
#define SEND_PULSE_MOTOR_TO_CLOSE_GATE { GATE = 1; Delay_ms(2000); GATE = 0; }
 
#define NEW_SMS_RECEIVED (strstr(gsmBuffer, CopyConst2Ram(buffer1, atResponse3)))
 
#define TURN_ON_LIGHT { LIGHT = 1; }
#define TURN_OFF_LIGHT { LIGHT = 0; }
 
#define LIGHT_IS_ON (lightStatusFlag == 1)
#define LIGHT_IS_OFF (lightStatusFlag == 0)
 
#define TURN_ON_NORMAL_VOLTAGE_LED { NORMAL_VOLTAGE_LED = 1; UNDER_VOLTAGE_LED = 0; OVER_VOLTAGE_LED = 0; }
#define TURN_ON_UNDER_VOLTAGE_LED { NORMAL_VOLTAGE_LED = 0; UNDER_VOLTAGE_LED = 1; OVER_VOLTAGE_LED = 0; }
#define TURN_ON_OVER_VOLTAGE_LED { NORMAL_VOLTAGE_LED = 0; UNDER_VOLTAGE_LED = 0; OVER_VOLTAGE_LED = 1; }
 
#define READ_TEMPERATURE { fTemperature = iTemperature = ADC_Read(0); Delay_ms(20); }
 
#define START_ALARM { PWM1_Set_Duty(127); PWM1_Start(); }
#define STOP_ALARM { PWM1_Stop(); }
#define SET_ALARM_STATUS_FLAG { alarmStatusFlag = 1; }
#define CLEAR_ALARM_STATUS_FLAG { alarmStatusFlag = 0; }
#define ALARM_IS_ON (alarmStatusFlag == 1)
#define ALARM_IS_OFF (alarmStatusFlag == 0)
 
#define MAINS_VOLTAGE_IS_NORMAL (mainsStatusFlag == 0)
 
#define GATE_IS_OPEN (gateStatusFlag == 1)
#define GATE_IS_CLOSED (gateStatusFlag == 0)
#define EVENT iEvent
 
#define CLEAR_WATCH_DOG_TIMER asm { clrwdt }
 
//Timer1
//Prescaler 1:8; TMR1 Preload = 15536; Actual Interrupt Time : 200 ms
//Place/Copy this part in declaration section
void InitTimer1(){
    T1CON = 0x31;
    TMR1IF_bit = 0;
    TMR1H = 0x3C;
    TMR1L = 0xB0;
    TMR1IE_bit = 1;
    INTCON = 0xC0;
}
 
void interrupt() {
 
    if(INT0IF_bit) {
        INT0IF_bit = 0;
        iEvent = 7;
        sendSmsFlag = 1;
    }
 
    if SERIAL_DATA_RECEIVED {
        if UART_OVERRUN_ERROR {
              CLEAR_UART_OVERRUN_ERROR
              DISABLE_UART_CONTINUOUS_RECEIVE
              ENABLE_UART_CONTINUOUS_RECEIVE
        }
 
        READ_GSM_DATA_INTO_BUFFER
        TERMINATE_GSM_BUFFER
 
        CLEAR_UART_INTERRUPT_FLAG
    }
 
     if(TMR1IF_bit) {
        TMR1IF_bit = 0;
        TMR1H = 0x3C;
        TMR1L = 0xB0;
        //Enter your code here
 
        if(lightDelayValue) {
             if(++noOf200MilliSecondsCounter == (lightDelayValue * 300)) {
                   LIGHT = 0;
                   lightDelayValue = 0;
                   TMR1ON_bit = 0;
                   lightStatusFlag = 0;
             }
        }
    }
}
 
char *CopyConst2Ram(char *dest, const char *src){
    char *d ;
    CLEAR_WATCH_DOG_TIMER
    d = dest;
    for(;*dest++ = *src++;)CLEAR_WATCH_DOG_TIMER;
 
    return d;
}
 
void DelayXSec(unsigned long int sec) {
    while(sec != 0) {
        Delay_ms(1000);
        CLEAR_WATCH_DOG_TIMER
        --sec;
    }
}
 
void GSM_RESET() {
    gsmReset = 1;
    CLEAR_WATCH_DOG_TIMER
    Delay_ms(20);
    gsmReset = 0;
    CLEAR_WATCH_DOG_TIMER
}
 
void Extract_SMS(char *s1, char *s2) {
    unsigned int i = 0;
    CLEAR_WATCH_DOG_TIMER
    while(*s1) {
        if(*s1 == '\n')
            ++i;
        CLEAR_WATCH_DOG_TIMER
        *s1++;
 
        if(i == 2)break;
    }
    CLEAR_WATCH_DOG_TIMER
    while((*s1) && (*s1 != '\r')) {
            *s2++ = *s1++;
            CLEAR_WATCH_DOG_TIMER
    }
 
    *s2 = '\0';                         //Terminate the string2 with null character
    CLEAR_WATCH_DOG_TIMER
}
 
void Get_SMS_Index(char *s1, char *s2) {
 
    while(*s1 != 'C') {
           *s1++;
           CLEAR_WATCH_DOG_TIMER
    }
 
    while(*s1 != 'M') {
           *s1++;
           CLEAR_WATCH_DOG_TIMER
    }
 
    while(*s1 != 'T') {
           *s1++;
           CLEAR_WATCH_DOG_TIMER
    }
 
    while(*s1 != 'I') {
           *s1++;
           CLEAR_WATCH_DOG_TIMER
    }
 
    while(*s1 != ',') {
           *s1++;
           CLEAR_WATCH_DOG_TIMER
    }
 
    *s1++;
    while(*s1 != '\r') {
        *s2++ = *s1++;
        CLEAR_WATCH_DOG_TIMER
    }
 
    *s2 = '\0';
    CLEAR_WATCH_DOG_TIMER
}
 
void Get_Value(char *s1, char *s2) {
    while(*s1 != '#') {
          *s1++;
          CLEAR_WATCH_DOG_TIMER
    }
 
    while((*s1) && ((*s1 >= '0') && (*s1 <= '9'))) {
         *s2++ = *s1++;
         CLEAR_WATCH_DOG_TIMER
    }
 
    *s2 = '\0';
    CLEAR_WATCH_DOG_TIMER
}
 
void Get_Light_Delay_Value(char *s1, char *s2) {
    while(*s1 != 'd') {
          *s1++;
          CLEAR_WATCH_DOG_TIMER
    }
    *s1++;
    while((*s1) && ((*s1 >= '0') && (*s1 <= '9'))) {
         *s2++ = *s1++;
         CLEAR_WATCH_DOG_TIMER
    }
 
    *s2 = '\0';
    CLEAR_WATCH_DOG_TIMER
}
 
void GSM_Send_Const_Command(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *gsmAttempt, char clr) {
    CLEAR_WATCH_DOG_TIMER
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        UART1_Write_Text(CopyConst2Ram(s2, s4));
        CLEAR_WATCH_DOG_TIMER
        DelayXSec(2);
        CLEAR_WATCH_DOG_TIMER
        if((*gsmAttempt)++ == 3) {
            GSM_RESET();
            CLEAR_WATCH_DOG_TIMER
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
            (*gsmAttempt) = 0;
            (*idx) = 0;
        }
    }
 
    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    CLEAR_WATCH_DOG_TIMER
    (*gsmAttempt) = 0;
    (*idx) = 0;
}
 
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *gsmAttempt, char *s6, char clr) {
    CLEAR_WATCH_DOG_TIMER
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        CopyConst2Ram(s2, s4);
        CLEAR_WATCH_DOG_TIMER
        strcat(s2, s5);
        strcat(s2, "\r");
        UART1_Write_Text(s2);
        CLEAR_WATCH_DOG_TIMER
        DelayXSec(2);
        CLEAR_WATCH_DOG_TIMER
        if((*gsmAttempt)++ == 3) {
            GSM_RESET() ;
            CLEAR_WATCH_DOG_TIMER
            (*gsmAttempt) = 0;
            (*idx) = 0;
            memset(gsmBuffer, '\0', sizeof(gsmBuffer));
        }
    }
 
    (*idx) = 0;
 
    Extract_SMS(s1, s6);
    CLEAR_WATCH_DOG_TIMER
 
    if(clr)memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    CLEAR_WATCH_DOG_TIMER
}
 
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *gsmAttempt, char clr) {
 
    while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
        CLEAR_WATCH_DOG_TIMER
        GSM_Send_Const_Command(s1, s2, s3, s5, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s3, s6, idx, gsmAttempt, 1);
        GSM_Send_Const_Command(s1, s2, s4, s7, idx, gsmAttempt, 1);
        CLEAR_WATCH_DOG_TIMER
        UART1_Write_Text(s8);
        Delay_ms(500);
        UART1_Write(0x1A);
        CLEAR_WATCH_DOG_TIMER
        DelayXSec(6);
    }
 
    memset(gsmBuffer, '\0', sizeof(gsmBuffer));
    (*idx) = 0;
    CLEAR_WATCH_DOG_TIMER
}
 
void main() {
 
    CMCON = 0x07;
    ADCON1 = 0x0D;
 
    TRISA = 0xC3;
    TRISB = 0x09;
    TRISC = 0x80;
    CLEAR_WATCH_DOG_TIMER
 
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    CLEAR_WATCH_DOG_TIMER
 
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
    CLEAR_WATCH_DOG_TIMER
 
    INTEDG0_bit = 1;
    ENABLE_EXTERNAL_INTERRUPT_0
    CLEAR_EXTERNAL_INTERRUPT_FLAG
 
    INITIALIZE_UART_WITH_9600_BPS_BAUD
    CLEAR_WATCH_DOG_TIMER
 
    DISABLE_GLOBAL_INTERRUPT
    READ_FIRST_TIME_USE_FLAG_FROM_EEPROM
    READ_TEMPERATURE_THRESHOLD_VALUE_FROM_EEPROM
    READ_PENDING_EVENT_VALUE_FROM_EEPROM
 
    CLEAR_WATCH_DOG_TIMER
    ENABLE_SERIAL_INTERRUPT
    ENABLE_PERIPHERAL_INTERRUPTS
    ENABLE_GLOBAL_INTERRUPT
 
    CLEAR_WATCH_DOG_TIMER
    INITIALIZE_PWM_FOR_BUZZER_AT_2500_KHz
    SET_PWM_DUTY_TO_0
    CLEAR_WATCH_DOG_TIMER
 
    SEND_AT_COMMAND
    SET_GSM_BAUDRATE
    SET_GSM_TEXT_MODE
    SET_SIM_MEMORY_FOR_SMS
    ENABLE_NEW_SMS_NOTIFICATION
    DELETE_ALL_SMS
 
    CLEAR_WATCH_DOG_TIMER
 
    doOnceFlag = 0;
 
    if(iEvent != 0) {
        sendSmsFlag = 1;
    }
 
    while(1) {
 
        CLEAR_WATCH_DOG_TIMER
        mainsVoltage = ADC_Read(1) * 229.0 / 1024.0;
        CLEAR_WATCH_DOG_TIMER
        //5V dc adc input is equal to 250.0 V AC
        Delay_ms(20);
        CLEAR_WATCH_DOG_TIMER
 
        if(prevMainsVoltage != mainsVoltage) {
            CLEAR_WATCH_DOG_TIMER
            if((mainsVoltage >= 180.0) && (mainsVoltage <= 240.0)) {
                 TURN_ON_NORMAL_VOLTAGE_LED
                 mainsStatusFlag = 0;
            }
            else if(mainsVoltage < 180.0) {
                 TURN_ON_UNDER_VOLTAGE_LED
                 mainsStatusFlag = 1;
            }
            else if(mainsVoltage > 240.0) {
                 TURN_ON_OVER_VOLTAGE_LED
                 mainsStatusFlag = 1;
            }
 
            CLEAR_WATCH_DOG_TIMER
 
            prevMainsVoltage = mainsVoltage;
        }
 
        CLEAR_WATCH_DOG_TIMER
        READ_TEMPERATURE
        fTemperature /= 2.046;
        iTemperature /= 2;
 
        if(iPreviousTemperature != iTemperature) {
             if(iTemperature > iTemperatureThreshold) {
                    EVENT_IS_1;
                    SET_SEND_SMS_FLAG
             }
             else if(iTemperature <= iTemperatureThreshold) {
                    EVENT_IS_0;
                    doOnceFlag = 0;
             }
 
             FloatToStr(fTemperature, message);
 
             iPreviousTemperature = iTemperature;
        }
 
        if FIRST_TIME_USE {
            EVENT_IS_17;
            SET_SEND_SMS_FLAG
            CLEAR_FIRST_TIME_USE_FLAG
        }
 
        CLEAR_WATCH_DOG_TIMER
 
        if NEW_SMS_RECEIVED {
 
             WAIT_A_WHILE_TO_RECEIVE_THE_SMS
             GET_SMS_INDEX
             CLEAR_WATCH_DOG_TIMER
             SEND_AT_COMMAND
             SET_GSM_TEXT_MODE
             READ_SMS
 
             CLEAR_WATCH_DOG_TIMER
 
             if SECRET_CODE_RECEIVED {
                   CLEAR_WATCH_DOG_TIMER
                   if SECRET_CODE_TO_TURN_ON_LIGHT_RECEIVED {
                        EVENT_IS_2
                   }
                   else if SECRET_CODE_TO_TURN_OFF_LIGHT_RECEIVED {
                        EVENT_IS_3
                   }
                   else if SECRET_CODE_TO_GET_STATUS_OF_LIGHT_RECEIVED {
                        EVENT_IS_4
                   }
                   else if SECRET_CODE_TO_GET_TEMPERATURE_READING_RECEIVED {
                        EVENT_IS_5
                   }
                   else if SECRET_CODE_TO_DE_ACTIVATE_THE_ALARM_RECEIVED {
                        EVENT_IS_6
                   }
                   else if SECRET_CODE_TO_ACTIVATE_THE_ALARM_RECEIVED {
                        EVENT_IS_7
                   }
                   else if SECRET_CODE_TO_GET_STATUS_OF_ALARM_RECEIVED {
                        EVENT_IS_8
                   }
                   else if SECRET_CODE_TO_CLOSE_GATE_RECEIVED {
                        EVENT_IS_9
                   }
                   else if SECRET_CODE_TO_OPEN_GATE_RECEIVED {
                        EVENT_IS_10
                   }
                   else if SECRET_CODE_TO_GET_STATUS_OF_GATE_RECEIVED {
                        EVENT_IS_11
                   }
                   else if SECRET_CODE_TO_GET_STATUS_OF_POWER_SUPPLY_RECEIVED {
                        EVENT_IS_12;
                   }
                   else if SECRET_CODE_TO_SET_TEMPERATURE_THRESHOLD_RECEIVED  {
                        EVENT_IS_13;
                   }
                   else if SECRET_CODE_TO_GET_TEMPERATURE_THRESHOLD_RECEIVED {
                        EVENT_IS_14;
                   }
                   else if SECRET_CODE_TO_SET_LIGHT_ON_OFF_DELAY_RECEIVED {
                        EVENT_IS_15;
                   }
                   else {
                       EVENT_IS_16;
                   }
 
                   CLEAR_WATCH_DOG_TIMER
                   SAVE_CURRENT_EVENT_VALUE_TO_EEPROM
                   CLEAR_WATCH_DOG_TIMER
                   memset(sms, '\0', sizeof(sms));
                   CLEAR_WATCH_DOG_TIMER
                   SET_SEND_SMS_FLAG
             }
             else {
                 EVENT_IS_16;
                 SET_SEND_SMS_FLAG
             }
        }
 
        CLEAR_WATCH_DOG_TIMER
 
        if(sendSmsFlag == TRUE) {
 
             CLEAR_WATCH_DOG_TIMER
 
             switch(EVENT) {
                    case 1:
                          CopyConst2Ram(sms, sms7);
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 2:
                          TURN_ON_LIGHT
                          CopyConst2Ram(sms, sms1);
                          CLEAR_WATCH_DOG_TIMER
                          lightStatusFlag = 1;
                          InitTimer1();
                          break;
                    case 3:
                          TURN_OFF_LIGHT
                          CopyConst2Ram(sms, sms2);
                          CLEAR_WATCH_DOG_TIMER
                          lightStatusFlag = 0;
                          break;
                    case 4:
                          if LIGHT_IS_ON {
                               CopyConst2Ram(sms, sms1);
                          }
                          else if LIGHT_IS_OFF {
                               CopyConst2Ram(sms, sms2);
                          }
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 5:
                          CopyConst2Ram(sms, smsC);
                          CLEAR_WATCH_DOG_TIMER
                          FloatToStr(fTemperature, buffer1);
                          Ltrim(buffer1);
                          Rtrim(buffer1);
                          strcat(sms, buffer1);
                          strcat(sms, " degree C");
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 6:
                          STOP_ALARM
                          CLEAR_ALARM_STATUS_FLAG
                          CopyConst2Ram(sms, sms6);
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 7:
                          START_ALARM
                          SET_ALARM_STATUS_FLAG
                          CopyConst2Ram(sms, sms5);
                          CLEAR_WATCH_DOG_TIMER
                          INT0IF_bit = 0;
                          break;
                    case 8:
                          if ALARM_IS_ON {
                               CopyConst2Ram(sms, sms5);
                          }
                          else if ALARM_IS_OFF {
                               CopyConst2Ram(sms, sms6);
                          }
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 9:
                          if MAINS_VOLTAGE_IS_NORMAL {
                              GATE = 1;
                              Delay_ms(2000);
                              GATE = 0;
                              CopyConst2Ram(sms, sms4);
                              CLEAR_GATE_STATUS_FLAG
                          }
                          else {
                              CopyConst2Ram(sms, smsD);
                              CLEAR_GATE_STATUS_FLAG
                          }
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 10:
                          if MAINS_VOLTAGE_IS_NORMAL {
                              SEND_PULSE_MOTOR_TO_OPEN_GATE
                              CopyConst2Ram(sms, sms3);
                              SET_GATE_STATUS_FLAG
                          }
                          else if MAINS_VOLTAGE_IS_ABNORMAL {
                              CopyConst2Ram(sms, smsD);
                              CLEAR_GATE_STATUS_FLAG
                          }
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 11:
                          if GATE_IS_OPEN {
                                CopyConst2Ram(sms, sms3);
                          }
                          else if GATE_IS_CLOSED {
                                CopyConst2Ram(sms, sms4);
                          }
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 12:
                          if MAINS_VOLTAGE_IS_NORMAL  {
                              CopyConst2Ram(sms, sms8);
                              gateStatusFlag = 1;
                          }
                          else if MAINS_VOLTAGE_IS_ABNORMAL  {
                              CopyConst2Ram(sms, sms9);
                              gateStatusFlag = 0;
                          }
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 13:
                          iTemperatureThreshold = atol(temperatureThresholdData);
                          GIE_bit = 0;
                          EEPROM_Write(Temperature_Threshold_Address, iTemperatureThreshold);
                          Delay_ms(50);
 
                          GIE_bit = 1;
                          IntToStr(iTemperatureThreshold, sms);
 
                          CLEAR_WATCH_DOG_TIMER
                          Ltrim(sms);
                          Rtrim(sms);
                          CopyConst2Ram(buffer1, smsA);
                          strcat(buffer1, sms);
                          CopyConst2Ram(buffer2, smsB);
                          strcat(buffer1, buffer2);
 
                          strcpy(sms, buffer1);
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 14:
                          GIE_bit = 0;
                          iTemperatureThreshold = EEPROM_Read(Temperature_Threshold_Address);
                          Delay_ms(50);
                          GIE_bit = 1;
                          IntToStr(iTemperatureThreshold, sms);
                          CLEAR_WATCH_DOG_TIMER
                          Ltrim(sms);
                          Rtrim(sms);
                          CopyConst2Ram(buffer1, smsA);
                          strcat(buffer1, sms);
                          CopyConst2Ram(buffer2, smsB);
                          strcat(buffer1, buffer2);
                          strcpy(sms, buffer1);
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 15:
                          Get_Light_Delay_Value(sms, lightDelayCode);
                          lightDelayValue = atol(lightDelayCode);
 
                          if(TMR1ON_bit == 0) {
                              InitTimer1();
                          }
 
                          if(lightStatusFlag == 0) {
                              CopyConst2Ram(sms, smsF);
                              IntToStr(lightDelayValue, str);
                              Ltrim(str);
                              Rtrim(str);
                              strcat(sms, str);
                              CopyConst2Ram(str, smsH);
                              strcat(sms, str);
                          }
                          else if(lightStatusFlag == 1) {
                              CopyConst2Ram(sms, smsG);
                              IntToStr(lightDelayValue, str);
                              Ltrim(str);
                              Rtrim(str);
                              strcat(sms, str);
                              CopyConst2Ram(str, smsH);
                              strcat(sms, str);
                          }
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 16:
                          CopyConst2Ram(sms, smsE);
                          CLEAR_WATCH_DOG_TIMER
                          break;
                    case 17:
                          CopyConst2Ram(sms, smsI);
                          CopyConst2Ram(buffer1, smsC);
                          strcat(sms, buffer1);
                          strcat(sms, message);
                          CopyConst2Ram(buffer1, smsB);
                          strcat(sms, buffer1);
                          CopyConst2Ram(buffer1, smsJ);
                          strcat(sms, buffer1);
                          CLEAR_WATCH_DOG_TIMER
                          break;
             };
 
             CLEAR_WATCH_DOG_TIMER
 
             if((iEvent != 0) && (iEvent != 1)) {
                  SEND_SMS
             }
             else if((iEvent == 1) && (doOnceFlag == 0)) {
                  SEND_SMS
                  doOnceFlag = 1;
             }
 
             DELETE_ALL_SMS
 
             memset(sms, '\0', sizeof(sms));
 
             CLEAR_WATCH_DOG_TIMER
             CLEAR_EVENT_COUNTER
             CLEAR_SEND_SMS_FLAG
             CLEAR_WATCH_DOG_TIMER
        }
    }
}

 

Re: Debugging MikroC Code

I can't be bothered trying to understand all of that code (anytime I see that many #define directives, I know the rest of the code is going to be poorly structured written; also much of it is probably irrelevant to the probelm you are asking about) but you ask how to debug: either use a debugger and check that the characters that are being sent to the UART are exactly what you expect; or replace the device on the UART with a device a terminal emulator and look at the actual character stream that is being sent.
Susan
 

Re: Debugging MikroC Code

Hi Susan,

the UART is working perfectly as seen on my realterm that im using, so im sending correct SMS to SIM900. Can you please have a look for me at case13 as when i send 20!4#d2, it should be setting my delay to 2min.

May you kindly check this for me.

- - - Updated - - -

Hi Susan,

Sorry i meant when i send 20!4#d5 it should execute event15 and event13 when i send 20!4#t20.
When i send the above SMS's, it executes event16
 

Re: Debugging MikroC Code

Looking at what would cause 'case 16' near the end of the file to be executed, we see that it is when "EVENT" would equal 16. Going back through all of the macro definitions (especially where EVENT is defined as 'iEvent' but is not used consistently) we need to look at the places where iEvent is begin set which is in the large "if NEW_SMS_RECEIVED" statement.
Here there are 2 places where 'EVENT_IS_16" (which expands to "{ iEvent = 16; }") is used: one is if SECRECT_CODE_RECEIVED (which expands to "(strstr(sms, CopyConst2Ram(buffer1, secretCode)) != 0)" which checks to see if 'sms' contains the "20!4" character sequence) is false and the other is when it is true but none of the other nested "if then else" statements are true.
Looking at the last situation, you say you are receiving "20!4#d5" and so I assume that you are expecting this to match the 'else if SECRECT_CODE_TO_SET_LIGHT_ON_OFF_DELAY_RECEIVED' matches.
That #define expands to "(strcmp(sms, CopyConst2Ram(buffer1, secretCodeC)) == 0)" where secretCodeC is set to "20!4#d".
Therefore this all comes down to the use of the 'strcmp' function call to compare the value of 'sms (which you say is "20!4#d5") to a string that is "20!4#d".
The problem here is that the strings are of different length. The 'strcmp' function works by comparing the equivalent characters in each location within the strings and this works for the first 6 characters in the 2 strings. However, when it looks to the 7th character position it finds that the "secretCodeC" string has ended but the "sms" string has not. Therefore it concludes that the strings are NOT the same and so does not return 0 and therefore the 'if' statement is false.
There are several solutions: one is to only match the prefix characters (i.e. stop the comparison when one string ends but return the status of the match to that point) or to use something like 'strncmp' and tell it that you only want the first 'n' characters compared. In this case you could use 'strlen(secretCodeC)' to give you that length.
All of this would have been MUCH simpler to write and debug if you have used a simpler overall code structure and also NOT used all of those #defines that only serve to hide the important information.
Susan
 

Re: Debugging MikroC Code

Hi Susan,

Thanks for the explaination, what happens when i send event13 when i send 20!4#t20? When i use the logic that you have use above, im not sure why event13 is not being used here too when i send 20!4#t20.

I have defined
Code:
char temperatureTresholdData[5]
, could this be an issue? Can i change this to
Code:
char temperatureTresholdData[100]
or something
 

Re: Debugging MikroC Code

If you use the same steps as I did you will come to the same conclusion for "event13" as for "event16".
You have a couple of advantages over me:
- you wrote the code and have a good understanding of what it is supposed to do and how it is supposed to work
- you have the ability to compile and debug the code while it is running so you can see which code paths you expect to be used and which are not
Of course I have a few advantages of my own:
- over 40 years of experience in writing code and debugging it
- having NOT written the code so I need to look at what it is actually doing, not what it is supposed to do.
You have used a particular 'pattern' to do your checking for a string prefix that is used in all of the "SECRET_CODE_TO_xxxx" macros (and why don't you use a single parameterised macro instead of all of these?). If the pattern has been shown to cause problems for one situation, then it may well be a problem for all of them.
While I'm talking about that pattern, what is the purpose of the 'CopyConst2Ram' function? The result is a pointer to a character array that is passed to the 'strcmp' function. In some old compilers they needed a different way to access ROM based variables as opposed to RAM based ones, but the internal operation of the function does not show that being used. If you are trying to simply remove the 'const' decoration from the string then why bother - the strcmp function takes const strings as variables. As you are calling this function for each of the nested 'if-then-else' chain items, all you are really doing is a whole lot of needless copying of strings.
I'm not sure what 'temperatureThresholdData' has to do with the issue. The name is only used twice: when you declare the array; and as a parameter to the 'atol' function. I cannot see where the array value is set to be of use in the 'atol' parameter.
While I'm making general comments on your code, what is the watchdog timeout set to? You have a huge number of calls to reset it, and some of these are within loops that execute in microseconds. However you also have places where you call 'delay' functions that will return in periods of up to 6 seconds. Therefore you can set the WDT to something like 10 seconds and only reset it once in the main loop. That means you can get rid of 84 instances that are simply cluttering up your code.
Also, many of your #defines are only used once or twice. Therefore you are not gaining anything by using them but you are making the code harder to write and debug - especially by someone else. I would suggest that you write the code once (or even twice) and use a comment if necessary to explain what it is there for instead of trying to use a long name for the same purpose.
At the heart of your main loop is the use of the 'iEvent' variable and either setting the variable value or using it in a 'switch' statement. The switch statement then has a number of 'case' statements that use "magic numbers" which are integers without any explanation of what they mean. A far better approach is to declare 'iEvent' to be an enum that has values with meaningful names. That way you could set the 'iEvent' value with statements such as 'iEvent = TurnOnLight;' and case statements such as 'case TurnOnLight:' which say exactly what you are trying to do at those parts of the code.
Susan
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top