Étape 8: Code
#include < Servo.h >Servo myservo ;
int pos = 0 ;
int calibrationTime = 30 ;
unsigned long int lowIn ;
unsigned long int pause = 5000 ;
Boolean lockLow = true ;
Boolean takeLowTime ;
int pirPin = 12 ;
int pirPos = 13 ;
void setup() {}
myservo.Attach(4) ;
Serial.Begin(9600) ;
pinMode (pirPin, entrée) ;
pinMode (pirPos, sortie) ;
digitalWrite (pirPos, HIGH) ;
Serial.println ("calibrage capteur") ;
pour (int i = 0; i < calibrationTime; i ++) {}
Serial.Print(calibrationTime-i) ;
Serial.Print("-") ;
Delay(1000) ;
}
Serial.println() ;
Serial.println("Done") ;
même si (digitalRead(pirPin) == HIGH) {}
Delay(500) ;
Serial.Print(".") ;
}
Serial.Print ("capteur actif") ;
Delay(1000) ;
}
void loop() {}
if(digitalRead(pirPin) == HIGH) {}
pour (pos = 0; pos < 180; pos += 1)
{
myservo.Write(POS) ;
Delay(5) ;
}
pour (pos = 180; pos > = 1; pos-= 1)
{
myservo.Write(POS) ;
Delay(5) ;
}
{if(lockLow)}
lockLow = false ;
Serial.println("---") ;
Serial.Print ("mouvement détecté à") ;
Serial.Print(Millis()/1000) ;
Serial.println (« sec ») ;
Delay(50) ;
}
takeLowTime = true ;
}
if(digitalRead(pirPin) == faible) {}
{if(takeLowTime)}
lowIn = millis() ;
takeLowTime = false ;
}
Si (! lockLow & & millis() - lowIn > pause) {}
lockLow = true ;
Serial.Print ("requête s’est terminée à ») ;
Serial.Print((Millis() - pause) / 1000) ;
Serial.println (« sec ») ;
Delay(50) ;
}
}
}