Back to Parent

int photoCellPin = A0;

int photoCellReading = 0;

// Define a pin we'll place an LED on
int ledPin = D2;

// Create a variable to store the LED brightness.
int ledBrightness = 0;

// Our button wired to D0
int buttonPin = D3;

void setup() {
    
    pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
    
    // Set up the LED for output
    pinMode(ledPin, OUTPUT);
    
    // Create a cloud variable of type integer
  // called 'light' mapped to photoCellReading
  Particle.variable("light", &photoCellReading, INT);

}

void loop() {
    // find out if the button is pushed
   // or not by reading from it.
   int buttonState = digitalRead( buttonPin );
   
    // Use analogRead to read the photo cell reading
  // This gives us a value from 0 to 4095
  photoCellReading = analogRead(photoCellPin);

  // Map this value into the PWM range (0-255)
  // and store as the led brightness
  ledBrightness = map(photoCellReading, 2500, 4095, 0, 255);
  
  if( buttonState == LOW and photoCellReading>3000)
  {
    // fade the LED to the desired brightness
    analogWrite(ledPin, ledBrightness);
  }
  else if(buttonState == LOW and photoCellReading<=3000)
    {
        digitalWrite( ledPin, HIGH);
        delay(1000);
        digitalWrite( ledPin, LOW);
        delay(1000);
    }
  else if (buttonState == HIGH){
    // otherwise
    // turn the LED Off
    digitalWrite( ledPin, LOW);

  }

  

  // wait 1/10th of a second and then loop
  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