aboutsummaryrefslogtreecommitdiffstats
path: root/lighting
diff options
context:
space:
mode:
authorClemens Fries <git-arduino@xenoworld.de>2018-09-19 09:33:15 +0200
committerClemens Fries <git-arduino@xenoworld.de>2018-09-19 09:33:15 +0200
commite7c98512a8ced2a88dbfa171b1c0b3a0ad212fa2 (patch)
tree8cbf2eeffaac0857c8385d7b48effa532ece9085 /lighting
parentef2a04cd46ee49c13b7d2af95e3d35fcc7c7d756 (diff)
tinyhouse: Use LDR to react to external lightHEADmaster
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.
Diffstat (limited to 'lighting')
-rw-r--r--lighting/tinyhouse.ino42
1 files 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;