Back to Parent

// -----------------------------------
// Smart Mailbox
// -----------------------------------

// Name the pins
int boxLED = D0; // box indicator LED connected to D0
int inPin = D2; // tilt sensor connected to D2
int fsrPin = A0; // FSR connected to A0
int mailLED = D4; // mail indicator LED connected to D4
int TiltStatus = 0; // variable to store the tilt sensor status
char BoxStatus[128]; // char to describe box door status on cloud
int fsrReading = 0; // variable to store FSR reading
char fsrStatus[128]; // char to describe mail status on cloud
int mailLEDBrightness = 0; // variable to store mail LED brightness

void setup()
{
  // Configure the pins to be outputs
  pinMode(boxLED, OUTPUT); // sets D0 as output
  pinMode(inPin, INPUT); // sets D2 as input
  pinMode(mailLED, OUTPUT); // sets D4 as output

  strcpy(BoxStatus, "OFF"); // initialize char BoxStatus to "off"
  strcpy(fsrStatus, "OFF"); // initialize char fsrStatus to "off"

  // Create a cloud variable of type char
  // called 'BoxStatus' mapped to BoxStatus
 Spark.variable("BoxStatus", BoxStatus, STRING);

 // Create a cloud variable of type char
 // called 'MailStatus' mapped to fsrStatus
 Spark.variable("MailStatus", fsrStatus, STRING);
}

void loop()
{
  TiltStatus = digitalRead(inPin); // read the input pin D2
  // check to see if tilt sensor is tilted on
  // if it is, TiltStatus is HIGH
  if (TiltStatus == HIGH) {
    // turn boxLED on
    digitalWrite(boxLED, HIGH);
    // return "Open Sesame!" as cloud variable status
    strcpy(BoxStatus, "Open Sesame!");
  }
  else {
    // turn boxLED off
    digitalWrite(boxLED, LOW);
    // return "Sorry, We're Closed" as cloud variable status
    strcpy(BoxStatus, "Sorry, We're Closed");
  }
  // Use analogRead to read the photo cell reading
  // This gives us a value from 0 to 4095
  fsrReading = analogRead(fsrPin);
  // Map this value into the PWM range (0-255)
  // and store as the led brightness
  mailLEDBrightness = map(fsrReading, 0, 4095, 0, 255);
  // create force threshold for presence of mail
  if (mailLEDBrightness >= 30) // 30 is arbitrary threshold # based on
                              // weight of mail
  {
    // turn mailLED on
    digitalWrite(mailLED, HIGH);
    // return "You've Got Mail" as cloud variable status
    strcpy(fsrStatus, "You've Got Mail");

  }
  else {
    // turn mailLED off
    digitalWrite(mailLED, LOW);
    // return "Nothing Here" as cloud variable status
    strcpy(fsrStatus, "Nothing here");
  }
  // wait 1/10th of a second and then loop
  delay(100);
}
Click to Expand

Content Rating

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

0