Day_8
1)the servo homework was pretty straight forward , so completing it wasn’t too much of a challenge. The biggest problem for me was not initially assigning the servo position, so it never returned back to the original position. And I also did a for loop telling it to return to initial position once the servo reached it’s specified angle.Thankfully Mathew was able to help me figure out why my servo was acting weird. I also wanted to add a buzzer when the motion read HIGH but I didn’t had time.
WORKING CODE:
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int PIR = 2; //PIR in Pin 2
Servo myServo;
const int Servo_Position_1 = 90;
void setup()
{
myServo.attach(SERVO);
pinMode (PIR, INPUT);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(PIR) == HIGH)
{
Serial.println("MOTION DETECTED");
myServo.write(180);
delay(15);
}
if(digitalRead(PIR) == LOW)
{
Serial.println("NO MOTION");
myServo.write(Servo_Position_1);
delay(40);
}
}
WORKING CODE:
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int PIR = 2; //PIR in Pin 2
Servo myServo;
const int Servo_Position_1 = 90;
void setup()
{
myServo.attach(SERVO);
pinMode (PIR, INPUT);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(PIR) == HIGH)
{
Serial.println("MOTION DETECTED");
myServo.write(180);
delay(15);
}
if(digitalRead(PIR) == LOW)
{
Serial.println("NO MOTION");
myServo.write(Servo_Position_1);
delay(40);
}
}
Comments
Post a Comment