home Inhaltsverzeichnis
erste Version am 18.12.2017
letzte Änderung am 18.12.2017
LED-Streifen als Nachtlicht - Seite 3-sub
Auf dieser Seite ist das Ergebnis von sechs Messreihen dokumentiert.
Anhand dieser Daten hoffe ich eine Funktion ableiten zu können, die
zur gemessenen Gesamthelligkeit und der eingestellten LED-Helligkeit
die real vorhandene Umgebungshelligkeit liefert - also praktisch aus
der Gesamthelligkeit die LED-Helligkeit rausrechnet.
Die LEDs samt Sensor-Einheit befanden sich dabei am finalen
Installationsort.
Die relevanten Pins des ATmega328 waren auf einen ArduinoUNO
gepatched, auf dem das folgende Sketch lief:
#include <avr/sleep.h>
#define LED_POWER_PIN 11 // PWM-Pin zum Gate des n-Channel MOSFETs für die LEDs
#define STATUS_LED_PIN 5 // PWM-Pin zur Anode der Status-LED
#define MOTION_SENSOR_PIN 2 // Interrupt-Pin (0) für den Bewegungs-Sensor
#define BPW40_EMITTER_PIN 0 // Analog-Pin zur Abfrage des Helligkeits-Sensors
#define DEBUG 1
/* -------------------------------------------------------------------
* ADC wieder aktivieren.
* https://www.gammon.com.au/adc
*/
void restore_ADC(void) {
ADCSRA=bit(ADEN); // turn ADC on
ADCSRA|=bit(ADPS0)|bit(ADPS1)|bit(ADPS2); // Prescaler of 128
analogRead(BPW40_EMITTER_PIN); // Dummy-Read als Init
}
/* -------------------------------------------------------------------
* Wird bei LOW-HIGH-Flanke an Pin2 aufgerufen.
* Quelle: http://www.gammon.com.au/power ( Sketch J)
*/
void wake() {
sleep_disable();
detachInterrupt(0);
restore_ADC();
#if DEBUG==1
Serial.println("wake from deepSleep()");
#endif
}
/* -------------------------------------------------------------------
* CPU in den Tiefschlaf-Modus versetzen.
* Aufwachen durch LOW-HIGH-Flanke an Pin2 bzw. Int0 über wake().
*/
void deepSleep(void) {
#if DEBUG==1
Serial.println("Tiefschlaf");
delay(10); // dem println genug Zeit geben, seine Daten loszuwerden
#endif
analogWrite(LED_POWER_PIN, 0); // PWM-Signal auf "100% LOW" setzen
ADCSRA=0; // disable ADC
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Do not interrupt before we go to sleep, or the
// ISR will detach interrupts and we won't wake.
noInterrupts();
// will be called when pin D2 goes low
attachInterrupt(0, wake, RISING);
EIFR=bit(INTF0); // clear flag for interrupt 0
// turn off brown-out enable in software
// BODS must be set to one and BODSE must be set to zero within four clock cycles
MCUCR=bit(BODS)|bit(BODSE);
// The BODS bit is automatically cleared after three clock cycles
MCUCR=bit(BODS);
// We are guaranteed that the sleep_cpu call will be done
// as the processor executes the next instruction after
// interrupts are turned on.
interrupts(); // one cycle
sleep_cpu(); // one cycle
}
/* -------------------------------------------------------------------
* Liefert den Mittelwert von 10 Helligkeitswerten mit jeweils einer
* Pause von 100µS dazwischen. In Summe wird also 2mS lang gemessen.
*/
int getBPW40save(void) {
unsigned int tmp, avg, i;
for(avg=i=0; i<10; i++) {
tmp=analogRead(BPW40_EMITTER_PIN); // Doku sagt: "It takes about 100 microseconds (0.0001 s) to read an analog input"
avg+=tmp;
delayMicroseconds(100);
}
return(avg/10);
}
void setup() {
#if DEBUG==1
Serial.begin(115200);
#endif
//analogReference(INTERNAL); // die Auflösung im unteren Bereich erhöhen
pinMode(MOTION_SENSOR_PIN, INPUT); // Bewegungssensor
pinMode(LED_POWER_PIN, OUTPUT); // LED-Streifen
pinMode(STATUS_LED_PIN, OUTPUT); // Status-LED
analogWrite(LED_POWER_PIN, 0); // LED-Streifen aus
analogWrite(STATUS_LED_PIN, 0); // Status-LED aus
}
void loop() {
int cur_led, i, v, avg;
delay(3000);
Serial.println("---");
for(cur_led=0; cur_led<256; cur_led++) {
analogWrite(LED_POWER_PIN, cur_led);
delay(20);
Serial.print("cur_led="); Serial.print(cur_led); Serial.print(" ");
for(avg=i=0; i<10; i++) {
v=getBPW40save();
avg+=v;
Serial.print(v); Serial.print(", ");
delay(100);
}
Serial.println(avg/10);
analogWrite(LED_POWER_PIN, 0);
delay(500);
}
Serial.println("------------------- Neustart....");
}
Das Sketch steuert die LEDs mit einem PWM-Wert von 0 bis 255 an
und erfasst pro Wert die Helligkeit.
Beim ersten Durchlauf (dunkel) waren die LEDs die einzige
Lichtquelle, beim zweiten Durchlauf (Tr) war die
Treppenhausbeleuchtung eingeschaltet, beim dritten (Tr+Fo)
zusätzlich noch das Flurlicht/oben, beim vierten (Fo) war nur das
Flurlicht/oben eingeschaltet.
Den fünften und sechsten Durchlauf, bei dem die Referenz-Spannung des ADC auf
DEFAULT eingestellt war, habe ich laufen lassen, um darüber ggf. die Ausdimm-Funktion insofern verbessern zu können, dass sie die Helligkeit durchgängig gleichmäßig runterfährt.
Beim fünften Durchlauf (Fo) war das Flurlicht/oben war an, beim sechsten
Durchlauf (dunkel) war es aus.
Die Werte ab der zweiten Spalte stellen den Mittelwert von jeweils zehn Helligkeitsmessungen dar.
PWM-Wert
|
dunkel
|
Tr
|
Tr+Fo
|
Fo
|
Fo
|
dunkel
|
analogReference
|
INTERNAL
|
DEFAULT
|
0 |
0 |
129 |
359 |
229 |
47 |
0 |
1 |
5 |
138 |
374 |
240 |
48 |
0 |
2 |
20 |
150 |
382 |
253 |
51 |
3 |
3 |
30 |
161 |
397 |
269 |
53 |
4 |
4 |
40 |
174 |
406 |
275 |
56 |
7 |
5 |
50 |
188 |
417 |
292 |
60 |
9 |
6 |
66 |
198 |
428 |
299 |
61 |
11 |
7 |
84 |
214 |
440 |
315 |
65 |
15 |
8 |
87 |
218 |
451 |
324 |
66 |
16 |
9 |
96 |
231 |
463 |
334 |
69 |
20 |
10 |
108 |
243 |
475 |
346 |
72 |
22 |
11 |
120 |
253 |
493 |
358 |
74 |
24 |
12 |
136 |
268 |
497 |
370 |
77 |
27 |
13 |
144 |
281 |
512 |
382 |
80 |
29 |
14 |
156 |
290 |
523 |
395 |
81 |
32 |
15 |
167 |
298 |
535 |
404 |
84 |
34 |
16 |
180 |
311 |
547 |
417 |
88 |
37 |
17 |
190 |
323 |
556 |
430 |
89 |
39 |
18 |
204 |
335 |
570 |
439 |
92 |
42 |
19 |
215 |
350 |
579 |
449 |
94 |
44 |
20 |
227 |
359 |
593 |
462 |
98 |
47 |
21 |
246 |
376 |
604 |
475 |
100 |
49 |
22 |
251 |
381 |
615 |
486 |
103 |
53 |
23 |
263 |
394 |
630 |
501 |
105 |
54 |
24 |
272 |
401 |
639 |
510 |
108 |
58 |
25 |
286 |
416 |
652 |
525 |
110 |
60 |
26 |
298 |
430 |
658 |
533 |
113 |
62 |
27 |
310 |
440 |
669 |
546 |
115 |
66 |
28 |
323 |
454 |
676 |
557 |
118 |
68 |
29 |
334 |
464 |
686 |
569 |
121 |
71 |
30 |
351 |
479 |
695 |
579 |
123 |
74 |
31 |
356 |
487 |
704 |
589 |
127 |
75 |
32 |
371 |
500 |
715 |
602 |
128 |
78 |
33 |
379 |
508 |
719 |
609 |
132 |
81 |
34 |
393 |
522 |
729 |
621 |
134 |
84 |
35 |
405 |
532 |
735 |
628 |
136 |
87 |
36 |
417 |
542 |
744 |
639 |
139 |
88 |
37 |
430 |
552 |
750 |
645 |
142 |
92 |
38 |
440 |
561 |
756 |
653 |
144 |
94 |
39 |
454 |
573 |
762 |
662 |
147 |
96 |
40 |
462 |
579 |
768 |
670 |
150 |
99 |
41 |
476 |
593 |
777 |
680 |
151 |
101 |
42 |
482 |
599 |
781 |
684 |
155 |
105 |
43 |
495 |
609 |
791 |
695 |
158 |
107 |
44 |
504 |
614 |
796 |
701 |
160 |
110 |
45 |
514 |
623 |
802 |
710 |
164 |
111 |
46 |
524 |
631 |
807 |
715 |
165 |
115 |
47 |
533 |
639 |
812 |
722 |
168 |
118 |
48 |
545 |
649 |
816 |
727 |
170 |
120 |
49 |
550 |
655 |
821 |
734 |
173 |
123 |
50 |
563 |
666 |
828 |
744 |
175 |
124 |
51 |
568 |
670 |
832 |
746 |
174 |
129 |
52 |
579 |
679 |
839 |
756 |
180 |
130 |
53 |
585 |
683 |
843 |
760 |
183 |
133 |
54 |
595 |
690 |
849 |
768 |
186 |
135 |
55 |
601 |
697 |
853 |
773 |
188 |
139 |
56 |
610 |
705 |
857 |
779 |
192 |
141 |
57 |
620 |
715 |
862 |
784 |
193 |
144 |
58 |
624 |
716 |
865 |
789 |
196 |
146 |
59 |
635 |
726 |
871 |
796 |
198 |
148 |
60 |
641 |
729 |
872 |
798 |
201 |
151 |
61 |
649 |
738 |
878 |
806 |
204 |
153 |
62 |
655 |
744 |
882 |
810 |
206 |
157 |
63 |
663 |
750 |
888 |
817 |
209 |
158 |
64 |
669 |
754 |
892 |
822 |
211 |
161 |
65 |
676 |
761 |
896 |
827 |
214 |
164 |
66 |
685 |
768 |
899 |
833 |
216 |
167 |
67 |
688 |
771 |
902 |
835 |
219 |
170 |
68 |
699 |
780 |
906 |
841 |
218 |
171 |
69 |
701 |
783 |
908 |
844 |
223 |
175 |
70 |
711 |
791 |
913 |
850 |
226 |
176 |
71 |
716 |
795 |
914 |
853 |
230 |
180 |
72 |
724 |
801 |
920 |
860 |
232 |
181 |
73 |
728 |
806 |
921 |
862 |
235 |
185 |
74 |
735 |
809 |
928 |
869 |
237 |
187 |
75 |
742 |
817 |
931 |
873 |
239 |
190 |
76 |
744 |
819 |
933 |
876 |
242 |
192 |
77 |
755 |
828 |
937 |
881 |
244 |
194 |
78 |
759 |
828 |
937 |
882 |
248 |
198 |
79 |
766 |
835 |
942 |
888 |
244 |
199 |
80 |
769 |
839 |
942 |
890 |
249 |
204 |
81 |
777 |
845 |
947 |
896 |
252 |
204 |
82 |
781 |
852 |
948 |
898 |
254 |
208 |
83 |
786 |
853 |
953 |
904 |
258 |
210 |
84 |
794 |
858 |
957 |
909 |
259 |
212 |
85 |
795 |
860 |
958 |
911 |
263 |
215 |
86 |
804 |
867 |
962 |
916 |
265 |
217 |
87 |
805 |
868 |
963 |
917 |
266 |
222 |
88 |
813 |
875 |
967 |
921 |
271 |
223 |
89 |
817 |
877 |
968 |
923 |
277 |
226 |
90 |
822 |
884 |
971 |
928 |
277 |
228 |
91 |
830 |
889 |
971 |
929 |
280 |
231 |
92 |
833 |
891 |
975 |
934 |
283 |
233 |
93 |
838 |
896 |
978 |
937 |
285 |
236 |
94 |
841 |
897 |
980 |
939 |
289 |
239 |
95 |
848 |
903 |
984 |
944 |
290 |
241 |
96 |
849 |
904 |
985 |
948 |
294 |
245 |
97 |
856 |
911 |
987 |
951 |
295 |
246 |
98 |
859 |
911 |
987 |
951 |
298 |
250 |
99 |
866 |
917 |
989 |
955 |
300 |
251 |
100 |
870 |
920 |
991 |
957 |
303 |
254 |
101 |
873 |
923 |
993 |
961 |
306 |
257 |
102 |
879 |
928 |
995 |
964 |
307 |
259 |
103 |
881 |
929 |
997 |
965 |
311 |
262 |
104 |
887 |
935 |
999 |
969 |
313 |
264 |
105 |
889 |
938 |
1001 |
972 |
316 |
268 |
106 |
894 |
940 |
1002 |
974 |
318 |
268 |
107 |
899 |
945 |
1003 |
976 |
320 |
272 |
108 |
901 |
948 |
1005 |
979 |
324 |
275 |
109 |
907 |
951 |
1006 |
981 |
325 |
278 |
110 |
908 |
952 |
1008 |
984 |
329 |
280 |
111 |
914 |
956 |
1008 |
985 |
330 |
281 |
112 |
918 |
958 |
1009 |
987 |
333 |
285 |
113 |
921 |
959 |
1011 |
989 |
337 |
287 |
114 |
926 |
963 |
1011 |
990 |
338 |
289 |
115 |
929 |
964 |
1013 |
992 |
342 |
293 |
116 |
932 |
969 |
1014 |
995 |
343 |
294 |
117 |
933 |
973 |
1015 |
997 |
347 |
298 |
118 |
939 |
974 |
1016 |
998 |
349 |
300 |
119 |
943 |
977 |
1019 |
1001 |
352 |
303 |
120 |
943 |
979 |
1017 |
1001 |
355 |
305 |
121 |
948 |
982 |
1018 |
1002 |
357 |
308 |
122 |
949 |
985 |
1018 |
1006 |
360 |
311 |
123 |
955 |
986 |
1020 |
1005 |
361 |
312 |
124 |
958 |
989 |
1020 |
1008 |
364 |
316 |
125 |
962 |
991 |
1021 |
1009 |
366 |
317 |
126 |
965 |
992 |
1021 |
1011 |
369 |
321 |
127 |
966 |
994 |
1021 |
1012 |
372 |
323 |
128 |
969 |
996 |
1022 |
1013 |
373 |
325 |
129 |
970 |
998 |
1022 |
1012 |
377 |
329 |
130 |
974 |
999 |
1022 |
1015 |
378 |
330 |
131 |
977 |
1002 |
1023 |
1015 |
382 |
335 |
132 |
978 |
1005 |
1023 |
1017 |
385 |
336 |
133 |
982 |
1005 |
1023 |
1017 |
386 |
338 |
134 |
982 |
1007 |
1023 |
1017 |
390 |
341 |
135 |
987 |
1010 |
- |
1019 |
392 |
343 |
136 |
988 |
1010 |
- |
1021 |
395 |
347 |
137 |
992 |
1013 |
- |
1021 |
397 |
348 |
138 |
995 |
1012 |
- |
1020 |
399 |
352 |
139 |
995 |
1013 |
- |
1022 |
403 |
354 |
140 |
997 |
1013 |
- |
1021 |
404 |
356 |
141 |
1001 |
1013 |
- |
1022 |
408 |
359 |
142 |
1001 |
1016 |
- |
1022 |
409 |
360 |
143 |
1004 |
1018 |
- |
1022 |
412 |
364 |
144 |
1004 |
1018 |
- |
1022 |
415 |
366 |
145 |
1006 |
1018 |
- |
1023 |
416 |
369 |
146 |
1008 |
1019 |
- |
1023 |
420 |
372 |
147 |
1011 |
1020 |
- |
1023 |
422 |
373 |
148 |
1011 |
1021 |
- |
1023 |
425 |
377 |
149 |
1012 |
1021 |
- |
- |
427 |
378 |
150 |
1014 |
1022 |
- |
- |
429 |
382 |
151 |
1014 |
1022 |
- |
- |
432 |
384 |
152 |
1016 |
1022 |
- |
- |
434 |
386 |
153 |
1016 |
1023 |
- |
- |
438 |
390 |
154 |
1018 |
1023 |
- |
- |
439 |
391 |
155 |
1019 |
1023 |
- |
- |
443 |
395 |
156 |
1020 |
1023 |
- |
- |
444 |
396 |
157 |
1019 |
1023 |
- |
- |
446 |
400 |
158 |
1020 |
- |
- |
- |
450 |
402 |
159 |
1021 |
- |
- |
- |
451 |
404 |
160 |
1022 |
- |
- |
- |
455 |
407 |
161 |
1022 |
- |
- |
- |
457 |
409 |
162 |
1022 |
- |
- |
- |
460 |
412 |
163 |
1023 |
- |
- |
- |
462 |
415 |
164 |
1023 |
- |
- |
- |
464 |
417 |
165 |
1023 |
- |
- |
- |
468 |
420 |
166 |
1023 |
- |
- |
- |
468 |
421 |
167 |
- |
- |
- |
- |
472 |
425 |
168 |
- |
- |
- |
- |
474 |
426 |
169 |
- |
- |
- |
- |
476 |
429 |
170 |
- |
- |
- |
- |
479 |
432 |
171 |
- |
- |
- |
- |
481 |
434 |
172 |
- |
- |
- |
- |
485 |
437 |
173 |
- |
- |
- |
- |
486 |
439 |
174 |
- |
- |
- |
- |
490 |
443 |
175 |
- |
- |
- |
- |
492 |
444 |
176 |
- |
- |
- |
- |
494 |
447 |
177 |
- |
- |
- |
- |
497 |
449 |
178 |
- |
- |
- |
- |
498 |
451 |
179 |
- |
- |
- |
- |
502 |
455 |
180 |
- |
- |
- |
- |
504 |
456 |
181 |
- |
- |
- |
- |
506 |
460 |
182 |
- |
- |
- |
- |
509 |
461 |
183 |
- |
- |
- |
- |
511 |
464 |
184 |
- |
- |
- |
- |
515 |
467 |
185 |
- |
- |
- |
- |
516 |
468 |
186 |
- |
- |
- |
- |
519 |
472 |
187 |
- |
- |
- |
- |
522 |
474 |
188 |
- |
- |
- |
- |
523 |
477 |
189 |
- |
- |
- |
- |
526 |
480 |
190 |
- |
- |
- |
- |
527 |
481 |
191 |
- |
- |
- |
- |
531 |
484 |
192 |
- |
- |
- |
- |
533 |
485 |
193 |
- |
- |
- |
- |
536 |
489 |
194 |
- |
- |
- |
- |
539 |
491 |
195 |
- |
- |
- |
- |
540 |
495 |
196 |
- |
- |
- |
- |
544 |
497 |
197 |
- |
- |
- |
- |
545 |
498 |
198 |
- |
- |
- |
- |
549 |
502 |
199 |
- |
- |
- |
- |
550 |
504 |
200 |
- |
- |
- |
- |
553 |
507 |
201 |
- |
- |
- |
- |
556 |
509 |
202 |
- |
- |
- |
- |
558 |
511 |
203 |
- |
- |
- |
- |
561 |
514 |
204 |
- |
- |
- |
- |
562 |
516 |
205 |
- |
- |
- |
- |
566 |
519 |
206 |
- |
- |
- |
- |
568 |
521 |
207 |
- |
- |
- |
- |
570 |
524 |
208 |
- |
- |
- |
- |
573 |
526 |
209 |
- |
- |
- |
- |
574 |
528 |
210 |
- |
- |
- |
- |
578 |
531 |
211 |
- |
- |
- |
- |
580 |
533 |
212 |
- |
- |
- |
- |
583 |
536 |
213 |
- |
- |
- |
- |
585 |
538 |
214 |
- |
- |
- |
- |
587 |
541 |
215 |
- |
- |
- |
- |
590 |
544 |
216 |
- |
- |
- |
- |
591 |
545 |
217 |
- |
- |
- |
- |
595 |
549 |
218 |
- |
- |
- |
- |
597 |
551 |
219 |
- |
- |
- |
- |
599 |
554 |
220 |
- |
- |
- |
- |
602 |
555 |
221 |
- |
- |
- |
- |
604 |
558 |
222 |
- |
- |
- |
- |
607 |
560 |
223 |
- |
- |
- |
- |
609 |
562 |
224 |
- |
- |
- |
- |
612 |
565 |
225 |
- |
- |
- |
- |
614 |
568 |
226 |
- |
- |
- |
- |
616 |
570 |
227 |
- |
- |
- |
- |
619 |
572 |
228 |
- |
- |
- |
- |
620 |
575 |
229 |
- |
- |
- |
- |
624 |
577 |
230 |
- |
- |
- |
- |
625 |
580 |
231 |
- |
- |
- |
- |
628 |
582 |
232 |
- |
- |
- |
- |
630 |
584 |
233 |
- |
- |
- |
- |
633 |
586 |
234 |
- |
- |
- |
- |
635 |
589 |
235 |
- |
- |
- |
- |
637 |
591 |
236 |
- |
- |
- |
- |
640 |
594 |
237 |
- |
- |
- |
- |
642 |
596 |
238 |
- |
- |
- |
- |
645 |
598 |
239 |
- |
- |
- |
- |
647 |
601 |
240 |
- |
- |
- |
- |
649 |
604 |
241 |
- |
- |
- |
- |
652 |
606 |
242 |
- |
- |
- |
- |
654 |
609 |
243 |
- |
- |
- |
- |
657 |
612 |
244 |
- |
- |
- |
- |
659 |
613 |
245 |
- |
- |
- |
- |
661 |
616 |
246 |
- |
- |
- |
- |
664 |
618 |
247 |
- |
- |
- |
- |
666 |
621 |
248 |
- |
- |
- |
- |
668 |
623 |
249 |
- |
- |
- |
- |
671 |
626 |
250 |
- |
- |
- |
- |
673 |
628 |
251 |
- |
- |
- |
- |
675 |
630 |
252 |
- |
- |
- |
- |
678 |
632 |
253 |
- |
- |
- |
- |
680 |
634 |
254 |
- |
- |
- |
- |
682 |
637 |
255 |
- |
- |
- |
- |
686 |
640 |
Hier noch die Rohform, wie sie das Sketch im Terminal geliefert hat.
Zur Konvertierung der rohen Daten habe ich mir das folgende Python-Script zurechtgefuscht:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
fn="/home/dede/messung.txt"
# https://www.arduino.cc/reference/en/language/functions/math/map/
def mymap(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
d=dict()
for i in range(256):
d.update({i:["-", "-", "-", "-", "-", "-"]})
idx=-1
fobj=open(fn, "r")
lns=fobj.readlines()
for lnr in lns:
ln=lnr.strip()
if ln.startswith(";"):
idx+=1
if ln.startswith("cur_led="):
wrdlst=ln.split()
cur_led=int(wrdlst[0][len("cur_led="):])
avg=wrdlst[-1]
#print cur_led, avg, wrdlst
lv=d[cur_led]
lv[idx]=avg
d.update({cur_led:lv})
for i in range(256):
print i,
for j in d[i]:
print j,
print