Back to Parent

const int sensorPin = A0; 
int sensorValue = 0; 
const int lightPin = D2;
int lightIntensity = 0;
const int pressPin = D4;

void setup() {
    pinMode(lightPin, OUTPUT);
    Particle.variable("brightness", &lightIntensity, INT);
    pinMode(pressPin, INPUT_PULLUP);
}

void loop() {
    int pressState = digitalRead(pressPin);

    if (pressState == LOW) {
        sensorValue = analogRead(sensorPin);
        lightIntensity = map(sensorValue, 3000, 4095, 0, 255);
        printf("%d\n", lightIntensity); 
        analogWrite(lightPin, lightIntensity);
        delay(100);
    } else {
        digitalWrite(lightPin, LOW);
    }

    Particle.publish("brightness"); 
}
Click to Expand

Content Rating

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

0