Servo Motor And PIR

There are 4 pir and 1 servo motor and each pir has its own degree. when a pir detect the motion then servo motor rotates to degree of motion but this pir returns to its degree as triggered after the servo motor return to starting point. servo motor makes this situation 2 or 3 times.

is the problem about connection or codes ?

#include <Servo.h>

Servo myservo;
int pin1 = 2;
int pin2 = 4;
int pin3 = 11;
int pos = 0;
int pin4 = 6;
int wait = 1000;

void setup(){
Serial.begin(9600);
pinMode (pin1,INPUT);
pinMode (pin2,INPUT);
pinMode (pin3,INPUT);
pinMode (pin4,INPUT);
myservo.attach(13);
myservo.write(90);
}

void loop (){

int sensor_1 = digitalRead(pin1);
int sensor_2 = digitalRead(pin2);
int sensor_3 = digitalRead(pin3);
int sensor_4 = digitalRead(pin4);

if (sensor_1 == HIGH){
Serial.println(sensor_1);
myservo.write(125);
delay(wait);
}
else if (sensor_2 == HIGH){
Serial.println(sensor_2);
myservo.write(106);
delay(wait);
}
else if (sensor_3 == HIGH){
Serial.println(sensor_3);
myservo.write(68);
delay(wait);
}
else if (sensor_4 == HIGH){
Serial.println(sensor_4);
myservo.write(55);
delay(wait);

}
else
{

myservo.write(90);
}

}
Print
Go Up
Pages: [1]
Jump to:

At the moment the problem is communication ...

mustafa27: There are 4 pir and 1 servo motor and each pir has its own degree

So when a PIR goes 'high' the servo should drive to preset position for that PIR? (what if more than 1 goes high concurrently ??)

mustafa27: when a pir detect the motion then servo motor rotates to degree of motion

Might help to give us some context and explain what 'the' motion is. You've set specific positions for each PIR, so what does 'degree of motion' mean ? I think I get it from the code, but wouldn't have guessed it without.

mustafa27: but this pir returns to its degree as triggered after the servo motor return to starting point

'returns' to it's degree ?? 'as triggered' (by itself?) *after * wait, wha .. (?) very confusing

mustafa27: servo motor makes this situation 2 or 3 times.

that must suck if that's not what you're after - but be great if it is

OK, you might want to consider using a switch/case conditional rather than if/elses ...

Also have a good think about how all those delay(1000)'s are affecting your code - suggestion, maybe place it after all the conditions.

Imagine you're the code - don't miss a step - see where that gets you ;)

1:1: At the moment the problem is communication ... So when a PIR goes 'high' the servo should drive to preset position for that PIR? (what if more than 1 goes high concurrently ??) Might help to give us some context and explain what 'the' motion is. You've set specific positions for each PIR, so what does 'degree of motion' mean ? I think I get it from the code, but wouldn't have guessed it without. 'returns' to it's degree ?? 'as triggered' (by itself?) *after *wait, wha .. (?) very confusing that must suck if that's not what you're after - but be great if it is

OK, you might want to consider using a switch/case conditional rather than if/elses ...

Also have a good think about how all those delay(1000)'s are affecting your code - suggestion, maybe place it after all the conditions.

Imagine you're the code - don't miss a step - see where that gets you ;)

I want it to return to the position of the first triggered pir.

Each 4 pir have their own degrees(125,106,68,55).Whichever pir is triggered,then servo motor turns to that direction. pir Looks to only one direction.

Last triggered pir as it is triggered 2 3 times,servo motor returns to its degree.

++++

Sorry, it’s just too unclear…

Have you tried:

Imagining you’re the code, not missing a step and seeing where that gets you ?