Back to Parent

//Practice Exercise: 
//Combining Sensors and Inputs

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


int switchPin = D3;
int switchState;


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

void setup() {

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

void loop() {
        
        switchState = digitalRead(switchPin);
        photoCellReading = analogRead(photoCellPin); //read the photo cell reading, 0-4095
        
        ledBrightness = map(photoCellReading, 0, 2500, 255, 0); //map this value into the PWM range (0-255) and store as led brightness
        //int lightreading = map (500,0,4095,255,0);
        
    if(switchState == LOW)
    {
        //digitalWrite (ledPin, HIGH);
        
        analogWrite(ledPin, ledBrightness); //fade the led to the desired brightness
        
        }else if(switchState == HIGH){
            digitalWrite (ledPin, LOW);
        }else {digitalWrite (ledPin, LOW);
        }
        delay(100);
        
}
Click to Expand

Content Rating

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

0