Back to Parent

int photoresistor = A1;
int lightState = 0;
int lightStatus = 0;
int previousLightStatus = 0;
int lightCheck = 0;
int ledPin = D0;
int switchPin = D1;

void setup() {
  pinMode(photoresistor, INPUT);
  pinMode(ledPin, OUTPUT);
  Particle.variable("lightState", lightState);
  Particle.variable("lightCheck", lightCheck);
  Particle.variable("lightStatus", lightStatus);
  pinMode( switchPin , INPUT_PULLUP); // sets pin as input
  pinMode( ledPin , OUTPUT ); // sets pin as output
}

void loop() {
  int buttonState = digitalRead( switchPin );
  if( buttonState == LOW ) {
    lightCheck = analogRead(photoresistor);
    if (lightCheck > 100) {
      lightStatus = 1;
      digitalWrite(ledPin,HIGH);
    }
    else {
    lightStatus = 0;
    digitalWrite(ledPin,LOW);
    }
    delay(100);
    if (previousLightStatus == 0 && lightStatus == 1) {
      Particle.publish("lightState","Light detected");

    }
    previousLightStatus = lightStatus;
  }
  else {
    digitalWrite(ledPin,HIGH);
    delay(100);
    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