//define the warning led
int redLed = D0;
//define sensors sources
int lightPin = A0;
int lightReading = 0;
int fsrPin = A1;
int fsrReading = 0;
bool isSteal = false;
void setup()
{
pinMode(redLed, OUTPUT);
digitalWrite(redLed, LOW);
//show sensors variables on sparkcloud
Spark.variable("force", &fsrReading, INT);
Spark.variable("light", &lightReading, INT);
}
void loop()
{
//get fsr sensor readings
fsrReading = analogRead(fsrPin);
//get light sensor readings
lightReading = analogRead(lightPin);
bool notTouch = fsrReading <= 100;
bool lidOpen = lightReading >= 700;
Serial.print(lidOpen);
//light sensor on, fsr off, redled off
if (notTouch && lidOpen) {
//light sensor on, fsr on, redled on
} else if (!notTouch && lidOpen) {
//someone is stealing, led on!
digitalWrite(redLed, HIGH);
isSteal = true;
//the warning led will stay on for 10 seconds after the alert system has been activated
delay(10000);
//turn off the warning led after the 10 seconds is over
digitalWrite(redLed, LOW);
} else {
digitalWrite(redLed, LOW);
isSteal = false;
//no one is opening the jar
}
}
Click to Expand