mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 17:51:20 +00:00
64 lines
1.2 KiB
C++
64 lines
1.2 KiB
C++
|
|
int motion = 2;
|
|
int motionLed = 4;
|
|
const int relayPin = D1;
|
|
int LightSensor = A0;
|
|
int sensorValue = 0;
|
|
long TimeRequestMillis;
|
|
long waitTime = 20000;
|
|
bool activate = false;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
pinMode(motion, INPUT);
|
|
pinMode(motionLed, OUTPUT);
|
|
pinMode(relayPin, OUTPUT);
|
|
TimeRequestMillis = millis();
|
|
|
|
delay(2000);
|
|
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
sensorValue = analogRead(A0); // read the value from the sensor
|
|
|
|
unsigned long currentMillis = millis();
|
|
|
|
long sensor = digitalRead(motion);
|
|
|
|
Serial.println(currentMillis - TimeRequestMillis);
|
|
Serial.println(sensorValue);
|
|
|
|
|
|
if(sensor == HIGH && sensorValue<500){
|
|
Serial.println("J'ouvre !");
|
|
digitalWrite (motionLed, HIGH);
|
|
digitalWrite(D1, HIGH);
|
|
|
|
TimeRequestMillis = currentMillis;
|
|
|
|
activate = true;
|
|
|
|
delay(9000);
|
|
}
|
|
|
|
if(activate && sensor == HIGH) {
|
|
TimeRequestMillis = currentMillis;
|
|
Serial.println("mouvement, je laisse ouvert");
|
|
delay(2000);
|
|
}
|
|
|
|
if(activate && (currentMillis - TimeRequestMillis > waitTime)){
|
|
Serial.println("Je ferme");
|
|
digitalWrite (motionLed, LOW);
|
|
digitalWrite(D1, LOW);
|
|
activate = false;
|
|
}
|
|
|
|
delay(200);
|
|
|
|
|
|
}
|