Back to Parent

// Define a pin that we'll place the photo cell on
// Remember to add a 10K Ohm pull-down resistor too.
int photoCellPin = A0;
int bendPin = A1;
int buzzerPin = D3;
// Create a variable to hold the light reading
int photoCellReading = 0;
int bendReading = 0;

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

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

void setup() {
    pinMode(bendPin, INPUT);
    pinMode(photoCellPin, INPUT);
    pinMode(ledPin, OUTPUT);
    pinMode(buzzerPin, OUTPUT);
    Particle.variable("light", &photoCellReading, INT);
    Particle.variable("bend", &bendReading, INT);

}

void loop() {
  // Use analogRead to read the photo cell reading
  // This gives us a value from 0 to 4095
  photoCellReading = analogRead(photoCellPin);
  bendReading = analogRead(bendPin);
  // Map this value into the PWM range (0-255)
  // and store as the led brightness
  ledBrightness = map(photoCellReading, 2800, 4095, 0, 255);
  
  //light the led when Bend Sensor is bent
  if(bendReading < 250 && ledBrightness < 100){
      // both sensors show bad readings at the same time, play a sound alert
      tone(buzzerPin, 1000, 500);
      delay(500);
  }else{
      if(bendReading < 250){
          // only the first sensor has poor readings, blink the LED to display the led for 1.5 seconds and off for 0.5 seconds
          analogWrite(ledPin, 255);
          delay(1500);
          analogWrite(ledPin, 0);
          delay(500);
      }else{
          if(ledBrightness < 100){
              // only the second sensor has poor readings, blink the LED such that it is on for 0.5seconds and off for 1.5 seconds
              analogWrite(ledPin, 255);
              delay(500);
              analogWrite(ledPin, 0);
              delay(1500);
          }
      }
  }

  // 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