Back to Parent

int ledPin = D1; //"You've got mail" LED
int sendPin = A0; //Send gift
int presPin = A1; //Presence sensor
int presThresh = 1000; //Presence sensor threshold
int sendThresh = 1000; //Send sensor threshold

bool mailIntrpt = false; //Got mail interrupt
bool presIntrpt = false; //Is present interrupt

int brightness = 0; //Stuff for fading LED
int fadeAmount = 5; //Stuff for fading LED

int servoPin = A7; //Servo pin
Servo myServo; //Servo name

int presRead = 0; //Initialize presence reading
int sendRead = 0; //Initialize send reading

void setup() {
  pinMode (ledPin, OUTPUT); //Pin Modes
  pinMode (sendPin, INPUT);
  pinMode (presPin, INPUT);
  pinMode (servoPin, OUTPUT);
  myServo.attach(servoPin);  //Attach servo motor to appropriate pin
  Spark.subscribe("askjdfhkas1", gotMail); //Did I get mail?
  Spark.variable("Presence", &presRead, INT); //Check if presence sensor is working
  Spark.variable("Send", &sendRead, INT); //Check if send sensor is working
}

void loop() {
  myServo.write(10); //Start servo motor at 0 position

  //SEND GIFT code
  sendRead = analogRead(sendPin); //Read send pin input
  if (sendRead > sendThresh) //If send button is pressed,
  {
    sendMail(); //Send notification
  }
  else if (sendRead <= sendThresh) //If send button is not pressed, do nothing
  {}

  //CHECKING PRESENCE code
  presRead = analogRead (presPin); //Read presence pin input
  if (presRead > presThresh) //If presence read shows that I am present
  {
    presIntrpt = true; //Change my presence status
  }
  else if (presRead <= presThresh) //If presence read shows that I am NOT present
  {
    presIntrpt = false; //Keep my presence status false
  }

  //RECIEVE GIFT CODE
  if (mailIntrpt == true) //If I have recieved mail
  {
     analogWrite(ledPin, brightness); //Fade LED (source: http://arduino.cc/en/Tutorial/Fade)
     brightness = brightness + fadeAmount;
     if (brightness == 0 || brightness == 255) {
       fadeAmount = -fadeAmount ;
       }
       delay(30);

    if (presIntrpt == true) // If I am present
    {
      digitalWrite(ledPin, LOW); //Turn indicator LED off
      myServo.write(170); //Open door
      delay(10000); //Wait
      myServo.write(0); //Close door
      presIntrpt = true; //Reset presence input
      mailIntrpt = true; //Reset message input
      //Ready for new message
    }
    else if (presIntrpt == false) //If I am not present, do nothing
    {}
  }
  else if (mailIntrpt == false) //If I don't have a gift message,
  {
    digitalWrite(ledPin,LOW); //Turn indicator LED off
  }

}

void sendMail() //If send button is pressed
{
  Spark.publish("askjdfhkas2"); //Publish to other Spark core
}

void gotMail(const char *event, const char *data) //If a message is recieved from other Spark
{
  mailIntrpt = true; //Change my message inbox status to true
}
Click to Expand

Content Rating

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

0