Back to Parent

//Photoresistor exercise 1: 
//Modify the program so that the LED shows the opposite. When there is little light, it will be bright and when there’s lots of light, it will be off.

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, 255, 0); //map this value into the PWM range (0-255) and store as led brightness
       
        int lightreading = map (1500,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