From e7c98512a8ced2a88dbfa171b1c0b3a0ad212fa2 Mon Sep 17 00:00:00 2001 From: Clemens Fries Date: Wed, 19 Sep 2018 09:33:15 +0200 Subject: tinyhouse: Use LDR to react to external light This does three things: * When it is too dark, the lights will go into a shut down mode, where the maximum amount of brightness is graudally reduced to zero. This will allow me to let the project run all the time, while shutting down when I go to bed. * When it is dim (again), the process is reversed (but faster) * When there is too much light, the lights will be directly set to zero. This means that the house won't be lit if I switch on the lights in my room. --- lighting/tinyhouse.ino | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/lighting/tinyhouse.ino b/lighting/tinyhouse.ino index f2bf4f8..b77e2fb 100644 --- a/lighting/tinyhouse.ino +++ b/lighting/tinyhouse.ino @@ -5,23 +5,55 @@ void setup() { int i = 127; int j = 127; +int globalLimit = 255; +int ldr = 0; +int itercnt = 0; +boolean powerDown = false; +boolean powerUp = false; void loop() { + itercnt++; + ldr = analogRead(A0); + + if (ldr >= 1000 && globalLimit == 255) { + powerDown = true; + powerUp = false; + } else if (ldr < 1000 && globalLimit == 0) { + powerDown = false; + powerUp = true; + } + + if (powerDown && globalLimit > 0) { + // slow shutdown + itercnt % 8 == 0 && globalLimit--; + } else if( powerUp && globalLimit < 255 ) { + // fast start + globalLimit++; + } + delay(50); i = limit(i + (random(2) == 0 ? 8 : -8), 16); j = limit(j + (random(2) == 0 ? 2 : -2), 2); - analogWrite(3, j); - analogWrite(5, i); + if (ldr > 600) { + analogWrite(3, j); + analogWrite(5, i); + } else { // Simply go dark + analogWrite(3, 0); + analogWrite(5, 0); + } } int limit(int i, int within) { - if( i > (255 - within) ) - return 255 - within; + if (globalLimit < within) + return 0; + + if (i > (globalLimit - within)) + return globalLimit - within; - if( i < within ) + if (i < within) return within; return i; -- cgit