int servoPin = D3;
int motionPin = D6;
int ledPin = D0;
Servo servoSwitch;
int lowServo = 175;
int highServo = 5;
int checkTime = 200;
int blinkTime = 30;
int noMove = 1;
int lights = 1;
int count = 0;
int count2 = 0;
void setup() {
servoSwitch.attach(servoPin);
pinMode(motionPin,INPUT);
pinMode(ledPin,OUTPUT);
Particle.variable("count", count);
Particle.variable("count2", count2);
Particle.variable("noMove", noMove);
servoSwitch.write(lowServo);
blinkLED();
}
void loop() {
noMove = 1;
// If the lights are on, look for periods of no movement
if (lights) {
// Look for a long initial period of no movement
count = 0;
while (count <= checkTime){
if (digitalRead(motionPin) == 1) {
Particle.publish("motionDetect","HIGH");
noMove = 0;
break;
}
delay(100);
count++;
}
// if long no movement period, blink the LED 3 times while checking
// for movement
if (noMove == 1){
count2 = 0;
while (count2 < 3) {
noMove = blinkCheck();
if (noMove == 0) {
Particle.publish("motionDetect","HIGH");
break;
}
count2++;
}
}
// no movement has been detected, turn off lights
if (noMove == 1) {
servoControl();
lights = 0;
}
} else {
// The lights are on, if there is movement switch back to searching mode
if (digitalRead(motionPin) == 1){
Particle.publish("motionDetect","HIGH");
blinkLED();
lights = 1;
}
}
}
int blinkCheck() {
int resultMove = 1;
digitalWrite(ledPin,HIGH);
int i = 0;
while (i < blinkTime) {
if (digitalRead(motionPin) == 1) {
digitalWrite(ledPin,LOW);
return 0;
}
delay(100);
i++;
}
digitalWrite(ledPin,LOW);
i = 0;
while (i < blinkTime) {
if (digitalRead(motionPin) == 1) {
return 0;
}
delay(100);
i++;
}
return 1;
}
void blinkLED() {
digitalWrite(ledPin,HIGH);
delay(200);
digitalWrite(ledPin,LOW);
delay(200);
}
void servoControl() {
servoSwitch.write(highServo);
delay(500);
servoSwitch.write(lowServo);
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .