aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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;