// 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;
int switchPin = D3;
void setup()
{
// Set up the LED for output
pinMode(ledPin, OUTPUT);
pinMode( switchPin , INPUT_PULLUP); // sets pin as input
// Create a cloud variable of type integer
// called 'light' mapped to photoCellReading
Particle.variable("light", &photoCellReading, INT);
}
void loop()
{
// find out if the button is pushed
// or not by reading from it.
int buttonState = digitalRead( switchPin );
// Using a pulldown resistor we get a LOW
// Signal when its on
if( buttonState == LOW )
{
// turn the LED On
// 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( photoCellReading > 3000 )
{
// fade the LED to the desired brightness
analogWrite(ledPin, 255);
}else{
analogWrite(ledPin, 0);
}
}else{
// otherwise
// turn the LED Off
digitalWrite( ledPin, LOW);
}
delay(100);
}
Click to Expand