Back to Parent

//Flex Door Sensor

//Define led and flex sensor pins
int ledPin = D2;
int flexPin = A0;
int buzzPin = D3;
//Define flex sensor reading and led brightness
int flexRead = 0;
int flexInv = 4095;
int ledBrightness = 0;

//Define begining state
int state = 0;


void setup() {

//Set up the led output
pinMode(ledPin, OUTPUT);
Particle.variable("Flex", &flexInv, INT);
Particle.variable("State", state);
Particle.variable("led", ledPin);

}

void loop() {

  // Use analogRead to read from the sensor
  // This gives us a value from 0 to 4095
  //Inverse the flex reading
  flexRead = analogRead(flexPin);
  flexInv = 4095 - flexRead;
    
  // Map this value into the PWM range (0-255)
  // and store as the led brightness
  //ledBrightness = map(flexInv, 3000, 4095, 0, 255);

  //analogWrite(ledPin, ledBrightness);

  //delay(100);
  
  //if loop to trigger led and buzzer
  if (flexInv > 3500)
  {
      state = 1;
      analogWrite(ledPin, 255);
      delay(100);
      analogWrite(ledPin, 0);
      delay(100);
      analogWrite(ledPin, 255);
      tone(D3, 200, 1000);
  }
  else 
  {
      state = 0;
      analogWrite(ledPin, 0);
      digitalWrite(buzzPin, LOW);
     
  }

}
Click to Expand

Content Rating

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

0