/* For controlling a RUN display based on Stranger Things Using a motion detector (PIR sensor) is optional. Detects infrared, so is not fooled by shadows, but it does not work through windows. I'm using an HC-SR501. With the HC-SR501 facing away from you, and the 3 pins at the bottom... left pin --> 5V middle pin --> A0 right pin --> GND HC-SR501 requires nearly a minute to initialize. During this period, it can and often will output false detection signals. Perhaps using a photocell (LDR) is better? Sees through glass, so you can have a sign having people put their finger over the LDR as if it were a button that works through glass! Use a resistor and LDR to create a "potentiometer"... Photocell -> 5V Photocell and resistor -> A0 resistor -> GND Using a larger fixed resistor uses less power and is necessary if very dark, but it gives less resolution for measuring bright things. Using a smaller fixed resistor gives less resolution for measuring dark things, but is needed for very bright situations. 10,000 ohms is reasonable. For safety, do not use less than 220 ohms. In a very dark room, use at least 100,000 ohms. My LDR is model KLS6-3537. (c) 2018 Bradley Knockel */ // Are you using a motion detector? int motionDetector = false; // Are you using a light detector? // This will override motionDetector if true. int const lightDetector = true; // set pins int const pinR = 2; int const pinU = 3; int const pinN = 4; int const pinRemaining = 5; int const pirPin = A0; // for motion detector int const analogPin = A1; // for setting random seed void turnOff() { digitalWrite(pinR, HIGH); digitalWrite(pinU, HIGH); digitalWrite(pinN, HIGH); digitalWrite(pinRemaining, LOW); } void turnOn() { digitalWrite(pinR, LOW); digitalWrite(pinU, LOW); digitalWrite(pinN, LOW); digitalWrite(pinRemaining, HIGH); } void getAttention() { turnOn(); delay(100); turnOff(); delay(100); turnOn(); delay(100); turnOff(); } unsigned long t; void go() { digitalWrite(pinR, LOW); delay(1500); digitalWrite(pinR, HIGH); delay(500); digitalWrite(pinU, LOW); delay(2000); digitalWrite(pinU, HIGH); delay(500); digitalWrite(pinN, LOW); delay(3000); digitalWrite(pinN, HIGH); delay(500); // flicker all the lights t = millis(); while (millis() < t + 5000) { //Serial.println(random(2)); if (random(2) == 1) {turnOff();} else {turnOn();} delay(40); } turnOff(); } /* The following chunk of code is for the LDR. Nothing here matters if you are not using an LDR! */ // set these to your liking int const waitTime = 400; // ms between sampling brightness float const thresholdFraction = 0.1; // higher is harder to activate // for the fixed resistor //int voltsRint; float voltsR; // in volts / (supply voltage) float getBrightness() { //voltsRint = analogRead(0); //Serial.println(voltsRint); //voltsR = (voltsRint+0.5)/1024.0; // takes time; A0; add 0.5 to prevent getting 0 voltsR = (analogRead(0)+0.5)/1024.0; // takes time; A0; add 0.5 to prevent getting 0 return voltsR/(1-voltsR); } float val1; float val2; void setup() { pinMode(pinR, OUTPUT); pinMode(pinU, OUTPUT); pinMode(pinN, OUTPUT); pinMode(pinRemaining, OUTPUT); if (lightDetector) {motionDetector = false;} if (motionDetector) {pinMode(pirPin, INPUT);} randomSeed(analogRead(analogPin)); Serial.begin(9600); turnOff(); } bool justWent = false; void loop() { if (motionDetector) { if (digitalRead(pirPin) == HIGH) { if (!justWent) { getAttention(); delay(1000); } justWent = true; go(); delay(1000); } else { justWent = false; } } else if (lightDetector) { val1 = getBrightness(); delay(waitTime); val2 = getBrightness(); if ( abs(val1-val2)*2/(val1+val2) > thresholdFraction ) { //Serial.println(" start!"); if (!justWent) { getAttention(); delay(1000); } justWent = true; go(); delay( max(1000 - waitTime,1) ); } else { justWent = false; } } else { go(); delay(5000); } }