Back to Parent

// STEP 1 Define pins
// Photocell pin definition
int photoCellPin = A0;
// Potentiometer pin definition
int potPin = A5;
// LED pin definition
int ledPin = D2;

// STEP 2 Define variables 
// Light reading variable
int photoCellReading = 0;
// potentiometer reading variable
int potReading = 0;
// LED brightness reading variable
int ledBrightness = 0;	

// STEP 3 Setup
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);
}

// STEP 4 Creating a loop()
void loop()
{
  // Use analogRead to read the photo cell reading w value 0 to 4095
  photoCellReading = analogRead(photoCellPin);
  // Use analogRead to read the pentiometer reading
  potReading = map(analogRead(potPin), 0,4095, 0, 255);
  // Map this value into the PWM range (0-255) and store as the led brightness
  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