Skip to content

Instantly share code, notes, and snippets.

@zamfi

zamfi/stillness.ino

Created Mar 18, 2016
Embed
What would you like to do?
const int numPins = 12;
const int pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int motionSensePin = A0;
void setup() {
for (int i = 0; i < numPins; ++i) {
pinMode(pins[i], OUTPUT);
}
pinMode(motionSensePin, INPUT);
}
int litPin = 0;
unsigned long lastSecond = 0;
boolean systemOn = false;
unsigned long lastMotionDetected = 0;
void startClock() {
systemOn = true;
lastSecond = millis();
litPin = 0;
digitalWrite(pins[0], HIGH);
for (int i = 1; i < numPins; ++i) {
digitalWrite(pins[i], LOW);
}
}
void loop() {
if (digitalRead(motionSensePin) == LOW) {
lastMotionDetected = millis();
if (! systemOn) {
startClock();
}
}
if (millis() < lastMotionDetected ||
millis() - lastMotionDetected > 10 * 60 * 1000) { // 10 min * 60 s/min * 1000 ms/s
systemOn = false;
}
if (systemOn) {
if (millis() < lastSecond || millis() - lastSecond > 1000) {
digitalWrite(pins[litPin], LOW);
litPin = (litPin + 1) % numPins;
digitalWrite(pins[litPin], HIGH);
lastSecond = millis();
}
} else {
for (int i = 0; i < numPins; ++i) {
digitalWrite(pins[i], LOW);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.