Back to Parent

// Arduino code

/*
 * Room status reader.
 *
 * If lights are on and it's loud, room is social.
 * If lights are on and it's quiet, room is study.
 * If lights are off and it's quiet, room is empty.
 * If lights are off and it's loud, room is creepy.
 *
 * Prints 0, 1, 2, 3 for room status for easy reading in Processing sketch.
 *
 * 9/29/15, Robert Zacharias, rz@rzach.me
 * released to the public domain
 */


int soundPin = A5;
int lightPin = A4;
int x= 0;
int runningSoundAvg, runningLightAvg;

int soundThreshold = 250;
int lightThreshold = 200;

float weightOfNewData = 0.99; // closer to 1 biases towards old data, closer to 0 biases towards new data

unsigned long countdown;

void setup() {
  pinMode(soundPin, INPUT);
  pinMode(lightPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  runningSoundAvg = (analogRead(soundPin) * (1.0 - weightOfNewData)) + (runningSoundAvg * weightOfNewData);
  runningLightAvg = (analogRead(lightPin) * (1.0 - weightOfNewData)) + (runningLightAvg * weightOfNewData);

  if (runningSoundAvg > soundThreshold && runningLightAvg > lightThreshold) x = 4; // social
  else if (runningSoundAvg > soundThreshold && runningLightAvg <= lightThreshold) x = 1; // creepy
  else if (runningSoundAvg <= soundThreshold && runningLightAvg > lightThreshold) x = 2; // study
  else if (runningSoundAvg <= soundThreshold && runningLightAvg <= lightThreshold) x = 3; // empty

  // send data every half second
  if (millis() - countdown > 500) {
    Serial.write(x); // to talk to Processing
    countdown = millis();
  }
  
  delay(10);

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0