Christine Sunu
Published

Connected Shadowboxes

Motion-detecting frames that signal each other from across the country.

Full instructions provided1,829
Connected Shadowboxes

Story

Read more

Code

shadowbox-basic.ino

C/C++
#define EVENT_PERIOD_SECONDS 60
#define MAX_MISSED_EVENTS 3
 
int inputPin = D0;              // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int calibrateTime = 10000;      // wait for the thingy to calibrate
int onTime = -10000;

void setup() {
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(D7, OUTPUT);  // d7 LED is an output
  pinMode(A4, OUTPUT);  // set A4-A7 as output
  pinMode(A5, OUTPUT);
  pinMode(A6, OUTPUT);
  pinMode(A7, OUTPUT);
  Spark.function("circleblink", circleblink);   // define blinking function to be called when motion detected anywhere
  Spark.subscribe("motion-MYNAME-shadowbox", lightresponse); // define event to subscribe to. replace MYNAME with your name!
  Serial.begin(9600);
}

int circleblink(String command) {
    if (command.toInt()>0 && command.toInt()<15) { // blinks 4 leds in sequence starting with whichever pin is indicated. for A4-A7, this is 14.
        int startpin=command.toInt();
        for (int r = 0; r<3; r++) {
            digitalWrite(startpin,HIGH);
            for (int x = startpin; x<(startpin+4); x++) {
                delay(500);
                digitalWrite(x, LOW);
                digitalWrite(x+1, HIGH);
            }
            digitalWrite(startpin+4,LOW);
        }
        return 1;   // return 1 to show that this worked.
    }
    else {
        return -1; // return -1 to show that it did not work.
    }
}

bool calibrated() { // don't start anything until the sensor is ready
  return millis() - calibrateTime > 0;
}

void readTheSensor() { // function to read motion detector
  val = digitalRead(inputPin);
}

void reportTheData() { // function to report motion detection
  if (val == HIGH) {
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("motion");
      Spark.publish("motion-MYNAME-shadowbox","motion"); // remember to replace MYNAME with your name
      digitalWrite(D7,HIGH);
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    if (pirState == HIGH) {
      // we have just turned off
      Serial.println("quiet");
      // We only want to print on the output change, not state
      digitalWrite(D7,LOW);
      pirState = LOW;
    }
  }
}

void lightresponse(const char *event, const char *data) {
  onTime = millis();    //reset timer
}

void loop() {
  if (calibrated()) {
    readTheSensor();
    reportTheData();
  }
  if (millis() - onTime < 500) {    //only blink if spark.subscribe found something in the last 500 ms
      circleblink("14");
  }
  else {}
  double elapsedSeconds = (millis() - onTime) / 1000.0;
  if (elapsedSeconds > (MAX_MISSED_EVENTS * EVENT_PERIOD_SECONDS)) {
    Serial.println("Subscribe is dead, long live subscribe!");
    delay(500);
    System.reset();
  }
  else {
    Serial.println("things are okay... but it's been " + String(elapsedSeconds) + " since last event");
    delay(1000);
  }
}

Credits

Christine Sunu

Christine Sunu

1 project • 6 followers

Comments

Add projectSign up / Login