Back to Parent

Detector Code
const int photoResistor = A0;
const int photoSampleRate = 100;
const int photoDiff = 200;
int light = 0;
int firstLight = 0;
int lightStatus = 0; // 0 = no vegetable, 1 = vegetable

void setup () {
  Serial.begin(9600);
  Particle.variable("light", &light, INT);

}

void loop () {
  light = analogRead(photoResistor);
  if(firstLight==0) {
    firstLight = light;
  }

  if(light < firstLight-photoDiff && lightStatus==0){
      Particle.publish("diotQTcarrot", "removeVegetable",1,PUBLIC);
      lightStatus=1;
  }

  if(light >=  firstLight-photoDiff && lightStatus==1){
      Particle.publish("diotQTcarrot", "foundVegetable",1,PUBLIC);
      lightStatus=0;
  }

  delay(photoSampleRate);
}
Click to Expand

Content Rating

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

0