Back to Parent

#include "Particle.h"

int photoPin = A0;
int photoReading = 0;
int ledPin = D1;
int ledBrightness = 0;
int switchPin = D2;


void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode( switchPin , INPUT_PULLUP);
    
    Particle.variable("light", &photoReading, INT);
    

}

void loop() {
    
    int buttonState = digitalRead( switchPin );
    
      // Using a pulldown resistor we get a LOW
  // Signal when its on
  
  if( buttonState == LOW )
  {

      // Use analogRead to read the photo cell reading
  // This gives us a value from 0 to 4095
        photoReading = analogRead(photoPin);

  // Map this value into the PWM range (0-255)
  // and store as the led brightness
        ledBrightness = map(photoReading, 3000, 4095, 0, 255);

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

  // wait 1/10th of a second and then loop
  }else{
    // otherwise
    // turn the LED Off
    digitalWrite( ledPin, LOW);
    
  }

}
Click to Expand

Content Rating

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

0