Back to Parent

Enlarge to full screen mode to watch the video.
//Photoresistor exercise 2: 
//Turn the light on and off when reaches certain levels.

int photoCellPin = A0;
int photoCellReading = 0; //create a variable to hold the light reading


int ledPin = D2;
int ledBrightness = 0; //create a variable to store the LED brightness

void setup() {

    //setup the LED for output
    pinMode(ledPin, OUTPUT);
    
    
    Particle.variable("light", &photoCellReading, INT); //create a cloud variable of type integer
}

void loop() {

    
        digitalWrite(ledPin, HIGH);
        photoCellReading = analogRead(photoCellPin); //read the photo cell reading, 0-4095
        int ledBrightness = map(photoCellReading, 0, 4095, 0, 255); //map this value into the PWM range (0-255) and store as led brightness
       
        int lightreading = map (600,0,4095,0,255);
        if(ledBrightness >= lightreading){
        
        analogWrite(ledPin, ledBrightness); //fade the led to the desired brightness
        }else{
            digitalWrite (ledPin, LOW);
        }
        delay(100); //wait 1/10th of a second and then loop
        
}
Click to Expand

Content Rating

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

0