-
Notifications
You must be signed in to change notification settings - Fork 2
/
Treppenlichtsteuerung_Teil4.ino
436 lines (413 loc) · 16.7 KB
/
Treppenlichtsteuerung_Teil4.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
#include <Wire.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
// 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;
// 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;
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 = Overall_Stages / 16;
StagesLeft = (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);
}
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"));
}
bool DayLightStatus ()
{
int SensorValue = 0;
bool ReturnValue = true;
SensorValue = analogRead(LDR_Pin);
#ifdef DEBUG
Serial.print(F("DayLightStatus: "));
Serial.print(SensorValue);
#endif
if (SensorValue > DayLight_Brightness_Border)
{
if ((DayLight_Status) and (SensorValue > 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 > 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 + 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(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 + Delay_Stages_ON;
if (Pwm_Channel_Brightness > 4095) {Pwm_Channel_Brightness = 4095;}
} else if ( Pwm_Channel >= 0)
{
Pwm_Channel_Brightness = 0;
delay(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 - 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(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 - Delay_Stages_OFF;
if (Pwm_Channel_Brightness < 0) {Pwm_Channel_Brightness = 0;}
} else if ( Pwm_Channel >= 0)
{
Pwm_Channel_Brightness = 4095;
delay(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 > 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 > 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;
}
}
void loop()
{
Stages_Light_Control ();
}