-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTreppenlichtsteuerung_Teil5.ino
853 lines (819 loc) · 29.3 KB
/
Treppenlichtsteuerung_Teil5.ino
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
#include <Wire.h>
#include <EEPROM.h>
#define PWM_Module_Base_Addr 0x40 // 10000000b Das letzte Bit des Adressbytes definiert die auszuführende Operation. Bei Einstellung auf logisch 1 0x41 Modul 2 etc.. Adressbereich0x40 - 0x47
// wird ein Lesevorgang auswählt, während eine logische 0 eine Schreiboperation auswählt.
#define OE_Pin 8 // Pin für Output Enable
#define CPU_LED_Pin 13 // Interne Board LED an Pin 13 (zu Debuggingzwecken)
#define PIRA_Pin 2
#define PIRB_Pin 3
#define Num_Stages_per_Module 16
#define LDR_Pin A2 // Analog Pin, über den die Helligkeit gemessen werden soll. (LDR Wiederstand)
// #define DEBUG
#define L_Sens_Scope 50
#define MaxInputBufferSize 5 // maximal 255 Zeichen anpassen an vlcdr
struct WiFiEEPromData
{
// Anpassbare Betriebsparameter (Konstanten)
int Delay_ON_to_OFF = 10; // Minimum Wartezeit bis zur "Aus Sequenz" in Sekunden
int Overall_Stages = 8; // maximale Stufenanzahl: 62 x 16 = 992
int delay_per_Stage_in_ms = 100;
int DayLight_Brightness_Border = 600; // Helligkeitsgrenze Automatik - Höherer Wert - Höhere Helligkeit
byte Delay_Stages_ON = 20;
byte Delay_Stages_OFF = 20;
char ConfigValid[3]; //If Config is Vaild, Tag "TK" is required"
};
// Globale Variablen
int Pwm_Channel = 0;
int Pwm_Channel_Brightness = 0;
bool Motion_Trigger_Down_to_Up = false;
bool Motion_Trigger_Up_to_Down = false;
bool On_Delay = false;
bool DayLight_Status = true;
bool DLightCntrl = true;
byte PWMModules = 0;
byte StagesLeft = 0;
// interrupt Control
volatile byte A60telSeconds24 = 0;
volatile byte Seconds24;
//Serial Input Handling
char TBuffer;
char Cbuffer[MaxInputBufferSize+1]; //USB Code Input Buffer
String Sbuffer = ""; //USB String Input Buffer
int value; //USB Nummeric Input Buffer
byte Ccount { 0 }; //Number received Chars
byte Inptype = 0;
boolean StrInput = false;
boolean NumberInput = false;
boolean DataInput = false;
boolean EnterInput = false;
byte MenueSelection = 0;
byte MnuState = 0; // Maximale Menuetiefe 255 icl Sub
WiFiEEPromData MyConfig;
ISR(TIMER1_COMPA_vect)
{
A60telSeconds24++;
if (A60telSeconds24 > 59)
{
A60telSeconds24 = 0;
Seconds24++;
if (Seconds24 > 150)
{
Seconds24 = 0;
}
}
}
void ISR_PIR_A()
{
bool PinState = digitalRead(PIRA_Pin);
if (PinState)
{
if (!(Motion_Trigger_Up_to_Down) and !(Motion_Trigger_Down_to_Up))
{
digitalWrite(CPU_LED_Pin,HIGH);
Motion_Trigger_Down_to_Up = true;
} // PIR A ausgelöst
} else
{
digitalWrite(CPU_LED_Pin,LOW);
}
}
void ISR_PIR_B()
{
bool PinState = digitalRead(PIRB_Pin);
if (PinState)
{
if (!(Motion_Trigger_Down_to_Up) and !(Motion_Trigger_Up_to_Down))
{
digitalWrite(CPU_LED_Pin,HIGH);
Motion_Trigger_Up_to_Down = true;
} // PIR B ausgelöst
} else
{
digitalWrite(CPU_LED_Pin,LOW);
}
}
void Init_PWM_Module(byte PWM_ModuleAddr)
{
digitalWrite(OE_Pin,HIGH); // Active LOW-Ausgangsaktivierungs-Pin (OE).
Wire.beginTransmission(PWM_ModuleAddr); // Datentransfer initiieren
Wire.write(0x00); //
Wire.write(0x06); // Software Reset
Wire.endTransmission(); // Stoppe Kommunikation - Sende Stop Bit
delay(400);
Wire.beginTransmission(PWM_ModuleAddr); // Datentransfer initiieren
Wire.write(0x01); // Wähle Mode 2 Register (Command Register)
Wire.write(0x04); // Konfiguriere Chip: 0x04: totem pole Ausgang 0x00: Open drain Ausgang.
Wire.endTransmission(); // Stoppe Kommunikation - Sende Stop Bit
Wire.beginTransmission(PWM_ModuleAddr); // Datentransfer initiieren
Wire.write(0x00); // Wähle Mode 1 Register (Command Register)
Wire.write(0x10); // Konfiguriere SleepMode
Wire.endTransmission(); // Stoppe Kommunikation - Sende Stop Bit
Wire.beginTransmission(PWM_ModuleAddr); // Datentransfer initiieren
Wire.write(0xFE); // Wähle PRE_SCALE register (Command Register)
Wire.write(0x03); // Set Prescaler. Die maximale PWM Frequent ist 1526 Hz wenn das PRE_SCALEer Regsiter auf "0x03h" gesetzt wird. Standard : 200 Hz
Wire.endTransmission(); // Stoppe Kommunikation - Sende Stop Bit
Wire.beginTransmission(PWM_ModuleAddr); // Datentransfer initiieren
Wire.write(0x00); // Wähle Mode 1 Register (Command Register)
Wire.write(0xA1); // Konfiguriere Chip: ERrlaube All Call I2C Adressen, verwende interne Uhr, // Erlaube Auto Increment Feature
Wire.endTransmission(); // Stoppe Kommunikation - Sende Stop Bit
}
void Init_PWM_Outputs(byte PWM_ModuleAddr)
{
digitalWrite(OE_Pin,HIGH); // Active LOW-Ausgangsaktivierungs-Pin (OE).
for ( int z = 0;z < 16 + 1;z++)
{
Wire.beginTransmission(PWM_ModuleAddr);
Wire.write(z * 4 +6); // Wähle PWM_Channel_ON_L register
Wire.write(0x00); // Wert für o.g. Register
Wire.endTransmission();
Wire.beginTransmission(PWM_ModuleAddr);
Wire.write(z * 4 +7); // Wähle PWM_Channel_ON_H register
Wire.write(0x00); // Wert für o.g. Register
Wire.endTransmission();
Wire.beginTransmission(PWM_ModuleAddr);
Wire.write(z * 4 +8); // Wähle PWM_Channel_OFF_L register
Wire.write(0x00); // Wert für o.g. Register
Wire.endTransmission();
Wire.beginTransmission(PWM_ModuleAddr);
Wire.write(z * 4 +9); // Wähle PWM_Channel_OFF_H register
Wire.write(0x00); // Wert für o.g. Register
Wire.endTransmission();
}
digitalWrite(OE_Pin,LOW); // Active LOW-Ausgangsaktivierungs-Pin (OE).
}
void setup()
{
//Initalisierung
Serial.begin(9600);
pinMode(PIRA_Pin,INPUT);
pinMode(PIRB_Pin,INPUT);
pinMode(OE_Pin,OUTPUT);
pinMode(CPU_LED_Pin,OUTPUT);
pinMode(LDR_Pin,INPUT);
PWMModules = MyConfig.Overall_Stages / 16;
StagesLeft = ( MyConfig.Overall_Stages % 16) -1;
if (StagesLeft >= 1) {PWMModules++;}
Wire.begin(); // Initalisiere I2C Bus A4 (SDA), A5 (SCL)
for (byte ModuleCount=0;ModuleCount < PWMModules;ModuleCount++)
{
Init_PWM_Module(PWM_Module_Base_Addr + ModuleCount);
Init_PWM_Outputs(PWM_Module_Base_Addr + ModuleCount);
}
if (!(loadEEPROM_Config())) // Load Seetings from EEPROM
{
Serial.println(F("EEPROM Standard Settings saved."));
MyConfig.Delay_ON_to_OFF = 10; // Minimum Wartezeit bis zur "Aus Sequenz" in Sekunden
MyConfig.Overall_Stages = 8; // maximale Stufenanzahl: 62 x 16 = 992
MyConfig.delay_per_Stage_in_ms = 100;
MyConfig.DayLight_Brightness_Border = 600; // Helligkeitsgrenze Automatik - Höherer Wert - Höhere Helligkeit
MyConfig.Delay_Stages_ON = 20;
saveEEPROM_Config();
}
noInterrupts();
attachInterrupt(0, ISR_PIR_A, CHANGE);
attachInterrupt(1, ISR_PIR_B, CHANGE);
TCCR1A = 0x00;
TCCR1B = 0x02;
TCNT1 = 0; // Register mit 0 initialisieren
OCR1A = 33353; // Output Compare Register vorbelegen
TIMSK1 |= (1 << OCIE1A); // Timer Compare Interrupt aktivieren
interrupts();
Serial.println(F("Init_Complete"));
}
/** Save Config to EEPROM */
bool loadEEPROM_Config()
{
bool RetValue;
EEPROM.get(0, MyConfig);
EEPROM.end();
if (String(MyConfig.ConfigValid) = String("TK"))
{
RetValue = true;
} else
{
RetValue = false; // Settings not found.
}
return RetValue;
}
/** Store Config to EEPROM */
bool saveEEPROM_Config()
{
strncpy( MyConfig.ConfigValid , "TK", sizeof(MyConfig.ConfigValid) );
EEPROM.put(0, MyConfig);
EEPROM.end();
return true;
}
bool DayLightStatus ()
{
int SensorValue = 0;
bool ReturnValue = true;
SensorValue = analogRead(LDR_Pin);
#ifdef DEBUG
Serial.print(F("DayLightStatus: "));
Serial.print(SensorValue);
#endif
if (SensorValue > MyConfig.DayLight_Brightness_Border)
{
if ((DayLight_Status) and (SensorValue > MyConfig.DayLight_Brightness_Border + L_Sens_Scope))
{
ReturnValue = false;
DayLight_Status = false;
} else if (!(DayLight_Status))
{
ReturnValue = false;
DayLight_Status = false;
}
#ifdef DEBUG
Serial.println(F(" OFF"));
#endif
} else
{
if ((DayLight_Status) and (SensorValue > MyConfig.DayLight_Brightness_Border - L_Sens_Scope))
{
ReturnValue = true;
DayLight_Status = true;
} else if (!(DayLight_Status))
{
ReturnValue = true;
DayLight_Status = true;
}
#ifdef DEBUG
Serial.println(F(" ON"));
#endif
}
return ReturnValue;
}
void Down_to_Up_ON()
{
#ifdef DEBUG
Serial.println(F("Down_to_Up_ON"));
#endif
byte Calc_Num_Stages_per_Module = Num_Stages_per_Module;
for (byte ModuleCount=0;ModuleCount < PWMModules;ModuleCount++)
{
Pwm_Channel = 0;
Pwm_Channel_Brightness = 4095;
if ((StagesLeft >= 1) and (ModuleCount == PWMModules -1))
{
Calc_Num_Stages_per_Module = StagesLeft;
}
else
{
Calc_Num_Stages_per_Module = Num_Stages_per_Module;
}
Pwm_Channel = 0;
Pwm_Channel_Brightness = 0;
while (Pwm_Channel < Calc_Num_Stages_per_Module +1)
{
Wire.beginTransmission( PWM_Module_Base_Addr + ModuleCount);
Wire.write(Pwm_Channel * 4 +8); // Wähle PWM_Channel_0_OFF_L register
Wire.write((byte)Pwm_Channel_Brightness & 0xFF); // Wert für o.g. Register
Wire.endTransmission();
Wire.beginTransmission( PWM_Module_Base_Addr + ModuleCount);
Wire.write(Pwm_Channel * 4 +9); // Wähle PWM_Channel_0_OFF_H register
Wire.write((Pwm_Channel_Brightness >> 8)); // Wert für o.g. Register
Wire.endTransmission();
if (Pwm_Channel_Brightness < 4095)
{
Pwm_Channel_Brightness = Pwm_Channel_Brightness + MyConfig.Delay_Stages_ON;
if (Pwm_Channel_Brightness > 4095) {Pwm_Channel_Brightness = 4095;}
} else if ( Pwm_Channel < Num_Stages_per_Module +1)
{
Pwm_Channel_Brightness = 0;
delay(MyConfig.delay_per_Stage_in_ms);
Pwm_Channel++;
}
}
}
}
void Up_to_DOWN_ON()
{
#ifdef DEBUG
Serial.println(F("Up_to_DOWN_ON "));
#endif
byte Calc_Num_Stages_per_Module = Num_Stages_per_Module;
int ModuleCount = PWMModules - 1;
while (ModuleCount >= 0)
{
Pwm_Channel_Brightness = 0;
if ((StagesLeft >= 1) and (ModuleCount == PWMModules -1))
{
Calc_Num_Stages_per_Module = StagesLeft;
}
else
{
Calc_Num_Stages_per_Module = Num_Stages_per_Module;
}
Pwm_Channel = Calc_Num_Stages_per_Module;
while (Pwm_Channel > -1)
{
Wire.beginTransmission( PWM_Module_Base_Addr + ModuleCount);
Wire.write(Pwm_Channel * 4 +8); // Wähle PWM_Channel_0_OFF_L register
Wire.write((byte)Pwm_Channel_Brightness & 0xFF); // Wert für o.g. Register
Wire.endTransmission();
Wire.beginTransmission(PWM_Module_Base_Addr + ModuleCount);
Wire.write(Pwm_Channel * 4 +9); // Wähle PWM_Channel_0_OFF_H register
Wire.write((Pwm_Channel_Brightness >> 8)); // Wert für o.g. Register
Wire.endTransmission();
if (Pwm_Channel_Brightness < 4095)
{
Pwm_Channel_Brightness = Pwm_Channel_Brightness + MyConfig.Delay_Stages_ON;
if (Pwm_Channel_Brightness > 4095) {Pwm_Channel_Brightness = 4095;}
} else if ( Pwm_Channel >= 0)
{
Pwm_Channel_Brightness = 0;
delay(MyConfig.delay_per_Stage_in_ms);
Pwm_Channel--;
if ( Pwm_Channel < 0)
{
Pwm_Channel =0;
break;
}
}
}
ModuleCount = ModuleCount -1;
}
}
void Down_to_Up_OFF()
{
#ifdef DEBUG
Serial.println(F("Down_to_Up_OFF"));
#endif
byte Calc_Num_Stages_per_Module = Num_Stages_per_Module;
for (byte ModuleCount=0;ModuleCount < PWMModules;ModuleCount++)
{
Pwm_Channel = 0;
Pwm_Channel_Brightness = 4095;
if ((StagesLeft >= 1) and (ModuleCount == PWMModules -1))
{
Calc_Num_Stages_per_Module = StagesLeft;
}
else
{
Calc_Num_Stages_per_Module = Num_Stages_per_Module;
}
while (Pwm_Channel < Calc_Num_Stages_per_Module +1)
{
Wire.beginTransmission( PWM_Module_Base_Addr + ModuleCount);
Wire.write(Pwm_Channel * 4 +8); // Wähle PWM_Channel_0_OFF_L register
Wire.write((byte)Pwm_Channel_Brightness & 0xFF); // Wert für o.g. Register
Wire.endTransmission();
Wire.beginTransmission(PWM_Module_Base_Addr + ModuleCount);
Wire.write(Pwm_Channel * 4 +9); // Wähle PWM_Channel_0_OFF_H register
Wire.write((Pwm_Channel_Brightness >> 8)); // Wert für o.g. Register
Wire.endTransmission();
if (Pwm_Channel_Brightness > 0)
{
Pwm_Channel_Brightness = Pwm_Channel_Brightness - MyConfig.Delay_Stages_OFF;
if (Pwm_Channel_Brightness < 0) {Pwm_Channel_Brightness = 0;}
} else if ( Pwm_Channel < Num_Stages_per_Module +1)
{
Pwm_Channel_Brightness = 4095;
delay(MyConfig.delay_per_Stage_in_ms);
Pwm_Channel++;
}
}
}
}
void Up_to_DOWN_OFF()
{
#ifdef DEBUG
Serial.println(F("Up_to_DOWN_OFF"));
#endif
byte Calc_Num_Stages_per_Module = Num_Stages_per_Module;
int ModuleCount = PWMModules - 1;
while (ModuleCount >= 0)
{
Pwm_Channel_Brightness = 4095;
if ((StagesLeft >= 1) and (ModuleCount == PWMModules -1))
{
Calc_Num_Stages_per_Module = StagesLeft;
}
else
{
Calc_Num_Stages_per_Module = Num_Stages_per_Module;
}
Pwm_Channel = Calc_Num_Stages_per_Module;
while (Pwm_Channel > -1)
{
Wire.beginTransmission(PWM_Module_Base_Addr + ModuleCount);
Wire.write(Pwm_Channel * 4 +8); // Wähle PWM_Channel_0_OFF_L register
Wire.write((byte)Pwm_Channel_Brightness & 0xFF); // Wert für o.g. Register
Wire.endTransmission();
Wire.beginTransmission(PWM_Module_Base_Addr + ModuleCount);
Wire.write(Pwm_Channel * 4 +9); // Wähle PWM_Channel_0_OFF_H register
Wire.write((Pwm_Channel_Brightness >> 8)); // Wert für o.g. Register
Wire.endTransmission();
if (Pwm_Channel_Brightness > 0)
{
Pwm_Channel_Brightness = Pwm_Channel_Brightness - MyConfig.Delay_Stages_OFF;
if (Pwm_Channel_Brightness < 0) {Pwm_Channel_Brightness = 0;}
} else if ( Pwm_Channel >= 0)
{
Pwm_Channel_Brightness = 4095;
delay(MyConfig.delay_per_Stage_in_ms);
Pwm_Channel--;
if ( Pwm_Channel < 0)
{
Pwm_Channel =0;
break;
}
}
}
ModuleCount = ModuleCount -1;
}
}
void Stages_Light_Control ()
{
if ((Motion_Trigger_Down_to_Up) and !(On_Delay))
{
DLightCntrl = DayLightStatus();
if (DLightCntrl)
{
Seconds24 = 0;
On_Delay = true;
Down_to_Up_ON();
} else { Motion_Trigger_Down_to_Up = false; }
}
if ((On_Delay) and (Seconds24 > MyConfig.Delay_ON_to_OFF) and (Motion_Trigger_Down_to_Up) )
{
Down_to_Up_OFF();
Motion_Trigger_Down_to_Up = false;
On_Delay = false;
Seconds24 = 0;
}
if ((Motion_Trigger_Up_to_Down) and !(On_Delay))
{
DLightCntrl = DayLightStatus();
if (DLightCntrl)
{
Seconds24 = 0;
On_Delay = true;
Up_to_DOWN_ON();
} else { Motion_Trigger_Up_to_Down = false; }
}
if ((On_Delay) and (Seconds24 > MyConfig.Delay_ON_to_OFF) and (Motion_Trigger_Up_to_Down))
{
Up_to_DOWN_OFF();
Motion_Trigger_Up_to_Down = false;
On_Delay = false;
Seconds24 = 0;
}
}
//Serial Command Interpreter Functions -------------------------------
void ClearCBuffer ()
{
for (byte a= 0; MaxInputBufferSize -1;a++)
Cbuffer[a] = 0;
}
boolean CheckforserialEvent()
{
while (Serial.available()) {
// get the new byte:
TBuffer = Serial.read();
if (TBuffer > 9 && TBuffer < 14)
{
Cbuffer[Ccount] = 0;
TBuffer =0;
Serial.print(char(13));
Serial.flush();
Serial.println("");
Sbuffer = "";
value = 0;
EnterInput = true;
return true;
} else if (TBuffer > 47 && TBuffer <58 )
{
if ( Ccount < MaxInputBufferSize)
{
Cbuffer[Ccount] = TBuffer;
Ccount++;
} else {Serial.print("#"); }
//Number Input detected
NumberInput = true;
}
else if (TBuffer > 64 && TBuffer < 123 )
{
if ( Ccount < MaxInputBufferSize)
{
Cbuffer[Ccount] = TBuffer;
Ccount++;
Serial.print(char(TBuffer));
Serial.flush();
}
//Character Char Input detected
StrInput = true;
}
else if ( (TBuffer == 127 ) | (TBuffer == 8 ) )
{
if ( Ccount > 0)
{
Ccount--;
Cbuffer[Ccount] = 0;
Serial.print("-");
Serial.flush();
}
}
else
{
if ( Ccount < MaxInputBufferSize)
{
Cbuffer[Ccount] = TBuffer;
Ccount++;
Serial.print(char(TBuffer));
Serial.flush();
//Data Input detected
DataInput = true;
}
return false;
}
return false;
}
}
byte SerInputHandler()
{
byte result = 0;
int c;
int d;
int a;
int b;
result = 0;
if (CheckforserialEvent())
{
if ((NumberInput) and not (DataInput)and not (StrInput)) //Numbers only
{
Sbuffer = "";
value = 0;
StrInput = false;
NumberInput = false;
DataInput = false;
EnterInput = false;
a = 0;
b = 0;
c = 0;
d = 0;
Sbuffer = Cbuffer; // Zahl wird AUCH ! in SBUFFER übernommen, falls benötigt.
if (Ccount == 1) { value = Cbuffer[0]- 48 ; }
if (Ccount == 2) {
a = Cbuffer[0] - 48 ;
a = a * 10;
b = Cbuffer[1] - 48 ;
value = a + b;
}
if (Ccount == 3) {
a = Cbuffer[0] - 48 ;
a = a * 100;
b = Cbuffer[1] - 48 ;
b = b * 10;
c = Cbuffer[2] - 48 ;
value = a + b + c;
}
if (Ccount == 4) {
a = Cbuffer[0] - 48 ;
a = a * 1000;
b = Cbuffer[1] - 48 ;
b = b * 100;
c = Cbuffer[2] - 48 ;
c = c * 10;
d = Cbuffer[3] - 48 ;
value = a + b + c + d;
}
if (Ccount >= 5)
{
Sbuffer = "";
value = 0;
Sbuffer = Cbuffer;
ClearCBuffer;
result = 2;
} else
{
ClearCBuffer;
Ccount = 0;
result = 1; //Number Returncode
NumberInput = false;
StrInput = false;
DataInput = false;
EnterInput = false;
Ccount = 0;
return result;
}
}
if ((StrInput) and not (DataInput)) //String Input only
{
Sbuffer = "";
Sbuffer = Cbuffer;
value = 0;
StrInput = false;
NumberInput = false;
DataInput = false;
EnterInput = false;
Ccount = 0;
ClearCBuffer;
result = 2; //Number Returncode
}
if (DataInput) {
Sbuffer = "";
Sbuffer = Cbuffer;
value = 0;
StrInput = false;
NumberInput = false;
DataInput = false;
EnterInput = false;
Ccount = 0;
ClearCBuffer;
result = 3; //Number Returncode
}
if ((EnterInput) and not (StrInput) and not (NumberInput) and not (DataInput))
{
Sbuffer = "";
value = 0;
Ccount = 0;
ClearCBuffer;
result = 4; //Number Returncode
}
NumberInput = false;
StrInput = false;
DataInput = false;
EnterInput = false;
Ccount = 0;
return result;
}
return result;
//End CheckforSerialEvent
}
void SerialcommandProcessor()
{
int a;
Inptype = 0;
Inptype = SerInputHandler();
// 0 keine Rückgabe
// 1 Nummer
// 2 String
// 3 Data
if (Inptype > 0)
{
MenueSelection = 0;
if ((MnuState < 2) && (Inptype == 2)) {Sbuffer.toUpperCase(); } // For Easy Entering Commands
if ((Sbuffer == "D") && (MnuState == 0) && (Inptype == 2)) { MenueSelection = 1;}
if ((Sbuffer == "O")&& (MnuState == 0) && (Inptype == 2)) { MenueSelection = 2;}
if ((Sbuffer == "T") && (MnuState == 0) && (Inptype == 2)) { MenueSelection = 3;}
if ((Sbuffer == "B") && (MnuState == 0) && (Inptype == 2)) { MenueSelection = 4;}
if ((Sbuffer == "N") && (MnuState == 0) && (Inptype == 2)) { MenueSelection = 5;}
if ((Sbuffer == "F")&& (MnuState == 0) && (Inptype == 2)) { MenueSelection = 6;}
if ((MnuState == 2) && (Inptype == 1)) { MenueSelection = 8;}
if ((MnuState == 3) && (Inptype == 1)) { MenueSelection = 9;}
if ((MnuState == 4) && (Inptype == 1)) { MenueSelection = 10;}
if ((MnuState == 5) && (Inptype == 1)) { MenueSelection = 11;}
if ((MnuState == 6) && (Inptype == 1)) { MenueSelection = 12;}
if ((MnuState == 7) && (Inptype == 1)) { MenueSelection = 13;}
if (MnuState == 10) { MenueSelection = 21;} // Time Set
if (MnuState == 11) { MenueSelection = 24;} // Time Set
if (MnuState == 12) { MenueSelection = 25;} // Time Set
if (MnuState == 13) { MenueSelection = 27;} // Background Set
if (MnuState == 14) { MenueSelection = 29;} // ClockFace Set
switch (MenueSelection)
{
case 1:
{
Serial.println("Delay ON to OFF: (1-65000)");
MnuState = 2;
value = 0;
Sbuffer = "";
break;
}
case 2:
{
Serial.println("Overall Stages: (1-992)");
MnuState = 3;
value = 0;
Sbuffer = "";
break;
}
case 3:
{
Serial.println("Delay per Stage in ms: (1-65000)");
MnuState = 4;
value = 0;
Sbuffer = "";
break;
}
case 4:
{
Serial.println("DayLight Brightness Border: (0-65000)");
MnuState = 5;
value = 0;
Sbuffer = "";
break;
}
case 5:
{
Serial.println("Delay Stages ON: (1-254)");
MnuState = 6;
value = 0;
Sbuffer = "";
break;
}
case 6:
{
Serial.println("Delay Stages OFF: (1-254)");
MnuState = 7;
value = 0;
Sbuffer = "";
break;
}
case 8:
{
MyConfig.Delay_ON_to_OFF = value;
saveEEPROM_Config();
Serial.print(F("Delay_ON_to_OFF set to:"));
Serial.println(MyConfig.Delay_ON_to_OFF);
MnuState = 0;
Sbuffer = "";
value = 0;
break;
}
case 9:
{
MyConfig.Overall_Stages = value;
saveEEPROM_Config();
Serial.print(F("Overall Stages set to:"));
Serial.println(MyConfig.Overall_Stages);
MnuState = 0;
Sbuffer = "";
value = 0;
break;
}
case 10:
{
MyConfig.delay_per_Stage_in_ms = value;
saveEEPROM_Config();
Serial.print(F("Delay per Stage in ms set to:"));
Serial.println(MyConfig.delay_per_Stage_in_ms);
MnuState = 0;
Sbuffer = "";
value = 0;
break;
}
case 11:
{
MyConfig.DayLight_Brightness_Border = value;
saveEEPROM_Config();
Serial.print(F("DayLight Brightness Border set to:"));
Serial.println(MyConfig.DayLight_Brightness_Border);
MnuState = 0;
Sbuffer = "";
value = 0;
break;
}
case 12:
{
MyConfig.Delay_Stages_ON = value;
saveEEPROM_Config();
Serial.print(F("Delay Stages ON set to:"));
Serial.println(MyConfig.Delay_Stages_ON);
MnuState = 0;
Sbuffer = "";
value = 0;
break;
}
case 13:
{
MyConfig.Delay_Stages_OFF = value;
saveEEPROM_Config();
Serial.print(F("Delay Stages OFF set to:"));
Serial.println(MyConfig.Delay_Stages_OFF);
MnuState = 0;
Sbuffer = "";
value = 0;
break;
}
default:
{
MnuState = 0;
Serial.println(F("-Treppenlichtsteuerung by T.Kuch 2020-"));
Serial.print(F("D - Delay ON to OFF / Current Value:"));
Serial.println(MyConfig.Delay_ON_to_OFF);
Serial.print(F("O - Overall Stages / Current Value:"));
Serial.println(MyConfig.Overall_Stages);
Serial.print(F("T - Delay per Stage in ms / Current Value:"));
Serial.println(MyConfig.delay_per_Stage_in_ms);
Serial.print(F("B - DayLight Brightness Border / Current Value:"));
Serial.println(MyConfig.DayLight_Brightness_Border );
Serial.print(F("N - Delay Stages ON / Current Value:"));
Serial.println(MyConfig.Delay_Stages_ON);
Serial.print(F("F - Delay Stages OFF / Current Value:"));
Serial.println(MyConfig.Delay_Stages_OFF);
Serial.println(F("Type Cmd and press Enter"));
Serial.flush();
MnuState = 0;
value = 0;
Sbuffer = "";
}
}
} // Eingabe erkannt
}
void loop()
{
Stages_Light_Control();
SerialcommandProcessor();
}