Back to Parent

// Defining pins
// Define a pin that we'll place the photo cell on
int photoCellPin = A0;
// Define a pin that we'll place the potentiometer on
int potPin = A5;
// Define a pin that we'll place the LED on
int ledPin = D2;

// Defining variables
// Create a variable to hold the light reading
int photoCellReading = 0;
// Create a variable to hold the pot reading
int potReading = 0;
// Create a variable to hold the LED reading
int ledBrightness = 0;


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

}

void loop() {
    // Use analogRead to read the photo cell reading, value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);
    // Use analogRead to read the potentiometer reading
    potReading = map(analogRead(potPin), 0,4095, 0, 255);
    // Map this value into the PWM range (0-255)
    ledBrightness = map(photoCellReading, 0, 4095, 0, 255);
    // Fade the LED to the desired brightness
        if(photoCellReading < 2000) {
            analogWrite(ledPin, potReading);
        } else {
            digitalWrite(ledPin, LOW);
        }
    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