Back to Parent

/*
 * Project Project1_RME_Pain
 * Description: Zen Pain Garden
 * Author: Laura Rodriguez
 * Date: 2/7/19
 */
 // variables
 int ledPin = D0;  // LED Pin mapping

 int photoPin = A0; // Photocell Pin Mapping

 int photoReading = 0; // Reading from the Photocell

 bool isBoxOpen = false; // A value to store if the box is open or closed

// setup() runs once, when the device is first turned on.
void setup() {
  // Put initialization like pinMode and begin functions here.

    pinMode(ledPin, OUTPUT);

    Particle.variable( "light", photoReading );

    Particle.function("activate", handleActivation );

    Particle.publish( "eventName" );

    for( int i = 0; i < 3; i++ ){
      digitalWrite(ledPin, HIGH);
      delay(2000);
      digitalWrite(ledPin, LOW);
      delay(2000);
    }

}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  // The core of your code will likely live here.

  photoReading = analogRead(photoPin);

  // our box is open...
  if( photoReading >= 2500 ){

	 // if the box just opened
	 // then signal it's changed
	 if( isBoxOpen == false ){
		 isBoxOpen = true;
		 Particle.publish( "box-opened" );

     for( int i = 0; i < 3; i++ ){
       digitalWrite(ledPin, HIGH);
       delay(2000);
       digitalWrite(ledPin, LOW);
       delay(2000);
     }
	 }

  }else{

	 if( isBoxOpen == true ){
		 Particle.publish( "box-closed" );
	 }
	 isBoxOpen = false;
  }

  delay( 100 );

}

int handleActivation( String command ){

  for( int i = 0; i < 3; i++ ){
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }

  return 1;

}
Click to Expand

Content Rating

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

0