I am making a servo project that moves according to the input from a PIR motion sensor. Currently, when the PIR sensor detects a motion, the servo will move.
But, when there is no motion detection, the server will still move.
It is now, the servo will move to 60 when it is HIGH and move to 0 when it is LOW. does that make sense ?
But the problem now, my PIR is always detecting a motion, even when there is no movement. Meanwhile the servo, it is still moving despite being the PIR being HIGH or LOW.
I have also attached the screenshot of the value 'val', as attached.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
#include <Servo.h>
int ledPin = 13;
int inputPin = 9;
int pirState = LOW;
int val = 0;
Servo myservo;
int pos = 0;
//long interval = 1000;
//unsigned long lastSpuit = 0;
void setup() {
myservo.attach(8);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED 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;
myservo.write(90);
delay(1000);
myservo.write(pos);
delay(1000);
}
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
myservo.write(pos);
}
}
}
The code in Reply #4 does not print the value of the variable val. In fact the code is almost identical to the code in your Original Post.
Write a simple program that gets a value from the PIR sensor and just prints the value to the Serial Monitor. There is no point worrying about other parts of the program until you can read the sensor reliably.
Post a link to the datasheet for the PIR sensor.
Also post a diagram showing clearly how you have connected the sensor to your Arduino.
What, exactly, do you mean by
Meanwhile the servo, it is still moving despite being the PIR being HIGH or LOW.
Robin2:
Write a simple program that gets a value from the PIR sensor and just prints the value to the Serial Monitor. There is no point worrying about other parts of the program until you can read the sensor reliably.
Sage advice.
@OP your code looks in fact like adafruit's from here: does it work properly in its virgin state with your actual sensor?
@OP your code looks in fact like adafruit's from here: does it work properly in its virgin state with your actual sensor?
yes, the code works fine in its original state. the problem rises when I start adding the code for servo motor. the servo does not stop when the PIR is at LOW state. it keeps moving.
sarahisml:
yes, the code works fine in its original state. the problem rises when I start adding the code for servo motor. the servo does not stop when the PIR is at LOW state. it keeps moving.
Post the working "original state" program and the code that represents your best attempt to add the servo.
Robin2:
Post the working "original state" program and the code that represents your best attempt to add the servo.
...R
this is the code for the PIR alone that is working
/*
* PIR sensor tester
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 9; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED 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;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
and this is the code that I have added commands for servo
#include <Servo.h>
int ledPin = 13;
int inputPin = 9;
int pirState = LOW;
int val = 0;
Servo myservo;
int pos = 0;
//long interval = 1000;
//unsigned long lastSpuit = 0;
void setup() {
myservo.attach(8);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED 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;
myservo.write(90);
delay(1000);
myservo.write(pos);
delay(1000);
}
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
myservo.write(pos);
}
}
}
sarahisml:
this is the code for the PIR alone that is working
Your two programs are very similar. However you have a large number of tiny (and irrelevant) differences like extra blank lines and braces on different lines, or in different places on the line which made it very difficult to compare the two codes side by side.
You still have no code in the programs to print the values from the variable val. If the problem is caused by the current draw from the servo preventing the other things working that would be very obvious if you were monitoring the value of the variable.
It is also wise to put a Serial.println() statement into setup() so you can see if the program is continually re-starting. that current draw by the servo can also cause that.
And, even though I asked twice, you have not yet described the errant servo movement in detail. If you had (or do) it might immediately point to the behaviour one would expect if the Arduino is resetting.