Back to Parent

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;

int button = D4;
int buttonReading = digitalRead(button);

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

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(button, INPUT);
    
    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);

    // Map this value into the PWM range (0-255)
    // and store as the led brightness
    if (digitalRead(button) == HIGH && photoCellReading > 1500)  {
        ledBrightness = 255;
    } else if (digitalRead(button) == HIGH && photoCellReading < 2000){
        ledBrightness = 80;
    } else {
        ledBrightness = 0;
    }

    // fade the LED to the desired brightness
    analogWrite(ledPin, ledBrightness);
}
Click to Expand

Content Rating

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

0