
1 minute read
Code
myservo.attach(4); // declare servo as output Serial.begin(9600); // Serial communication for debugging purposes
void loop(){ val = digitalRead(inputPin); // read input value switch (mode) { case 0: {
Advertisement
if (val == HIGH) { // check if the input is HIGH digitalWrite(relay, HIGH); // turn relay ON if (pirState == LOW) { // we have just turned on Serial.println(“Motion detected!”); // We only want to print on the output change, not state pirState = HIGH; mode = 1; } } else { digitalWrite(relay, LOW); // turn relay OFF if (pirState == HIGH){ // we have just turned off Serial.println(“Motion ended!”); // We only want to print on the output change, not state pirState = LOW; mode = 2; } break;
case 1: { delay(30000); digitalWrite(relay, LOW); mode = 0; } break;
case 2: {
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable ‘pos’ delay(15); // waits 15ms for the servo to reach the position }
delay(30000); } for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable ‘pos’ delay(15); // waits 15ms for the servo to reach the position } mode = 0; } } } }