#include <Servo.h>
#include <Arduino_APDS9960.h>
//PIR SENSOR
int PIRsensor = 2; // the pin that the sensor is atteched to
int PIRstate = LOW; // by default, no motion detected
int PIRval = 0; // variable to store the sensor status (value)
//FLEX SENSOR
int flexPin = A0;
int value;
//RELAY & SERVO
int relayPin = 3;
int servoPin = 9;
int pos = 0; // variable to store the servo position
Servo myservo; // create servo object to control a servo
void setup() {
pinMode(PIRsensor, INPUT);
pinMode(relayPin, OUTPUT);
myservo.attach(servoPin);
Serial.begin(9600); // initialize serial
while (!Serial);
if (!APDS.begin()) {
Serial.println("Error initializing APDS-9960 sensor!");
}
}
void loop(){
//FLEX SENSOR
value = analogRead(flexPin);
Serial.print("flex value: ");
Serial.println(value);
delay(500);
//value = map(value, 700, 900,0, 255);//Map value 0-1023 to 0-255 (PWM)
PIRval = digitalRead(PIRsensor); // read sensor value
if (PIRval == HIGH) { // check if the sensor is HIGH
delay(100); // delay 100 milliseconds
if (PIRstate == LOW) {
Serial.println("Motion detected!");
Serial.println(PIRval);
digitalWrite(LEDB,LOW);
do{
//SERVO
myservo.write(0);
delay(1000);
//RELAY
digitalWrite (relayPin,HIGH);
delay(100);
Serial.println("relay on");
}
while (value > 100);
PIRstate = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(LEDB,HIGH);
delay(200); // delay 200 milliseconds
if (PIRstate == HIGH){
Serial.println("Motion stopped!");
Serial.println(PIRval);
myservo.write(90);
digitalWrite (relayPin,LOW);
PIRstate = LOW; // update variable state to LOW
}
}
}
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. .