Back to Parent

// define a pin that we'll place the photo cell on
// remember to add a 10K Ohm pull-down resistor too
int photoCellPin = A0;

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

// define a pin we'll place an LED on
int ledPin = D2;

// create a variable to store the LED brightness
int ledBrightness = 0;

void setup() {
    
    // set up the LED for output
    pinMode( ledPin, OUTPUT );
    
    // create a cloud variable of type interger called 'light' mapped to photoCellReading
    Particle.variable("light", &photoCellReading, INT);
}

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 light levels are below 1000
    // set our LED on or off
    if ( photoCellReading < 1000){
        ledBrightness = 255;
    }else {
        ledBrightness = 0;
    }
    
    // fade the LED to the desired brightness
    analogWrite( ledPin, ledBrightness );
    
    // 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