Back to Parent

// version 6.0: servo + photoCell sensor (3 light intensity levels) + share more data with Particle cloud + publish data to IFTTT + button (on/off)

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position

// Photo cell pin
// Remember to add a 10K Ohm pull-down resistor
int photoCellPin = A0;

// Create a variable to hold the light reading
int photoCellReading = 0;

// Push-button wired to D0
int buttonPin = D0;

// device on/off status controlled by the push-button
int deviceOn = 1;
String deviceOnStr = "";

// Lighting status (Direct Sunlight; Low Sunlight; No Sunlight)
String sunlightStatus = "";

// store no. of hours of Direct Sunlight
double direct_Sunlight_Hours = 0;
String direct_Sunlight_Hours_Str = "";

// store no. of hours of Low Sunlight
double low_Sunlight_Hours = 0;
String low_Sunlight_Hours_Str = "";

void setup()
{
  myservo.attach(D2);  // attaches the servo on the D2 pin to the servo object
  // Only supported on pins that have PWM
  
  // For input, we define the
  // pushbutton as an input-pullup
  // this uses an internal pullup resistor
  // to manage consistent reads from the device

  // sets pin as input
  pinMode( buttonPin , INPUT_PULLUP);
  
  // Create a cloud variable of type String 
  Particle.variable("Device_Status", deviceOnStr );
  
  // Create a cloud variable of type integer
  Particle.variable("Sunlight_Intensity", &photoCellReading, INT);
  
  // Create a cloud variable of type String 
  Particle.variable("Sunlight_Status", sunlightStatus);
  
  // Create a cloud variable of type String 
  Particle.variable("Direct_Sunlight_Hours", direct_Sunlight_Hours_Str );
  
  // Create a cloud variable of type String 
  Particle.variable("Low_Sunlight_Hours", low_Sunlight_Hours_Str );
}

void loop()
{
  // Use analogRead to read the photo cell reading
  // This gives us a value from 0 to 4095
  photoCellReading = analogRead(photoCellPin);

   // find out if the button is pushed
   // or not by reading from it.
   int buttonState = digitalRead( buttonPin );
   
  // remember that we have wired the pushbutton to
  // ground and are using a pulldown resistor
  // that means, when the button is pushed,
  // we will get a LOW signal
  // when the button is not pushed we'll get a HIGH

  if( buttonState == LOW ){
      
      // toggle the on/off states of the device
      deviceOn = deviceOn * (-1);
  }
  
  if( deviceOn == 1 )
  {
      deviceOnStr = "On";
      delay(100);
  }
  else{
    
      deviceOnStr = "Off";
      delay(100);
  }
  
  if( deviceOn == 1 )  // if the device is turned on
  {
      if( photoCellReading < 2000 )  // No Sunlight
      {
          sunlightStatus = "No_Sunlight";
          
          while( pos > 0){                   // goes to 0 degree
              
              pos--;                         // in steps of 1 degree
              myservo.write(pos);            // tell servo to go to position in variable 'pos'
              delay(5);                      // waits 5ms for the servo to reach the position
          }
          
      }
      else if (photoCellReading > 3700){     // Direct Sunlight
      
          sunlightStatus = "Direct_Sunlight";
          
          // calculate the accumulated time of exposure to direct sunlight
          direct_Sunlight_Hours = direct_Sunlight_Hours + 0.1/60/60;
          direct_Sunlight_Hours_Str = String( direct_Sunlight_Hours, 3 );
          
          while( pos < 180){                 // goes to 180 degrees
              
              pos++;                         // in steps of 1 degree
              myservo.write(pos);            // tell servo to go to position in variable 'pos'
              delay(5);                      // waits 5ms for the servo to reach the position
          }
          
      }
      else{  // Low Sunlight
      
          sunlightStatus = "Low_Sunlight";
          
          // calculate the accumulated time of exposure to low sunlight
          low_Sunlight_Hours = low_Sunlight_Hours + 0.1/60/60;
          low_Sunlight_Hours_Str = String( low_Sunlight_Hours, 3 );
          
          while( pos < 90){                  // goes to 90 degrees
              
              pos++;                         // in steps of 1 degree
              myservo.write(pos);            // tell servo to go to position in variable 'pos'
              delay(5);                      // waits 5ms for the servo to reach the position
          }
          
          while( pos > 90){                  // goes to 90 degrees
              
              pos--;                         // in steps of 1 degree
              myservo.write(pos);            // tell servo to go to position in variable 'pos'
              delay(5);                      // waits 5ms for the servo to reach the position
          }
      }    
  }
  
  // send daily report to the user
  log_to_Notification();
  
  // wait 1/10th of a second and then loop
  delay(100);
}

// store the time when you last published to notification
int last_published_Notification = -1;

// store the notification message to the user
String message_To_User = "";
  
void log_to_Notification(){  
  
    // check if 24 hrs have elapsed 
    if( last_published_Notification + 60000*60*24 < millis() ){  
          
        if( direct_Sunlight_Hours < 4)
        {
            message_To_User = "Most mini-cacti require ~4 hrs of direct sunlight daily. Your cacus had direct sunlight of " + direct_Sunlight_Hours_Str
            + " hrs today. Consider moving it within 4 feet of a window :)";
        }
        else{
            
            message_To_User = "Most mini-cacti require ~4 hrs of direct sunlight daily. Your cacus had direct sunlight of " + direct_Sunlight_Hours_Str + " hrs today. Good job!"; 
        }
        
        Particle.publish( "log_to_Notification", message_To_User );
        
        last_published_Notification = millis();   
    }
}
Click to Expand

Content Rating

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

0