Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
int pirPin = 2; // PIR Out pin
int pirStat = 0; // PIR status
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
//relay
int relay = 12;
void setup() {
pinMode(pirPin, INPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
Serial.begin(9600);
}
void bulb(){ //turn on the light bulb
digitalWrite(relay, HIGH);
}
void motors(){
// turn on motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 100); //speed
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 100); //speed
delay(4000); //time of moving in one direction
//changing direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void motors2(){
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop(){
pirStat = digitalRead(pirPin);
if (pirStat == HIGH) { // if motion detected
Serial.println("DETECTED");
bulb();
motors();
motors();
motors();
motors2();
}
else {
// no motion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.