Back to Parent

int led1 = D2;
int button1 = D3;
int buttonState;
int lightSensor1 = A0;
int lightRead = 0;
int ledLighting = 0;

void setup() {
    
    pinMode(led1, OUTPUT);
    pinMode(button1, INPUT_PULLUP);
    Particle.variable("light", lightRead);
    Particle.variable("LEDbrightness", ledLighting);
    
}

void loop() {
    buttonState = digitalRead(button1);
    lightRead = analogRead(lightSensor1); //0-4095
    
    // if the button is pressed: the led will react with the lightness of outside world
    // the stronger the outside light is, the weaker the light intensity of LED will be 
    // if the button is not pressed, nothing will happen
    
    if (buttonState == LOW){
        ledLighting = 255 - map(lightRead, 0, 4095, 0, 255); //0-255
        analogWrite(led1, ledLighting);
    }else{
        digitalWrite(led1, LOW);
    }
    }
Click to Expand

Content Rating

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

0