Go Down

### Topic: 360 continuous servo being assigned an angle based on 6 surrounding PIR sensor (Read 291 times)previous topic - next topic

#### Copemake

hi I have the basis of my project. it is a 360 degree setup with 6 PIR sensors. The sensors are all set up well with the rig I've made and I get accurate readings in the serial monitor. the code I based this on uses a 180 degree servo but id like my continuous servo to be able to reach all the 6 pir sensor points. code shown below:

// Servor motor
#include <Servo.h>
Servo camServo; // name the servo motor controlling the camera base
int currentPIRposition = 0; // set current angle of servo

// PIR sensors
int PIRpin[] = {8,9,10,11,12,13}; // PIR pin numbers
int currentPIRpin = 12; // the current PIR pin; begin with the first in the sequence above
int PIRprevState[] = {1,1,1,1,1,1}; // the previous state of the PIR (0 = LOW, 1 = HIGH)
int PIRposition[] = {60,120,180,240,300,0}; // assign angles for servo motor (0-157 distributed equally between 5 PIR sensors)
boolean PIRstatus; // Set status of PIR sensor as either true or false

///// SETUP //////////////////////////////////////
void setup()  {

Serial.begin(9600);
camServo.attach(7); // assign servo pin

for (int p = 0; p < 6; p++)  { // set all PIR sensors as INPUTS
pinMode(PIRpin[p], INPUT);
} // end 'p' for

/////// CALIBRATE PIR SENSORS ///////
Serial.print("calibrating");
for(int c = 5; c < 10; c++){ // calibrate PIR sensors for 10 seconds (change from 10-60 sec depending on your sensors)
Serial.print(".");
delay(1000); // wait 1 second
} // end calibration for

camServo.write(0); // move the servo to the center position to begin

} // end setup

///// MAIN LOOP //////////////////////////////////
void loop()  {

for (int PIR = 0; PIR < 6; PIR++) { // start this loop for each PIR sensor
currentPIRpin = PIRpin[PIR]; // set current PIR pin to current number in 'for' loop

if (PIRstatus == HIGH) { // if motion is detected on current PIR sensor
if(PIRprevState[PIR] == 0) { // if PIR sensor's previous state is LOW
if (currentPIRposition != currentPIRpin && PIRprevState[PIR] == 0) { // if high PIR is different than current position PIR then move to new position
camServo.write(PIRposition[PIR]);
Serial.print("Current angle : ");
Serial.println(PIRposition[PIR]);
delay(50);
currentPIRposition = currentPIRpin; // reset current PIR position to active [PIR] pin
PIRprevState[PIR] = 1; // set previous PIR state to HIGH
}
PIRprevState[PIR] = 1; // set previous PIR state to HIGH if the current position is the same as the current PIR pin
} // end PIRprevState if
} // end PIRstatus if

else  { //

PIRprevState[PIR] = 0;   // set previous PIR state to LOW
} // end else

} // end [PIR] for loop
} // end main loop

I am fairly in experience so I'm looking for a way to translate the continuous servos movement into degrees so to tell it to stop on certain points then start again etc and how to merge the two together.

thanks responses greatly appreciated
matt

#### DVDdoug

The problem is, continuous servos are not really servos.   There's no feedback so it doesn't know where it is, which way to go, or when to stop.

...You might be able to gear-up your servo to get 360 degrees.  Or if there are only 6 stop-positions, maybe you can use 6 position sensors (of some kind).

#### Copemake

I set up the servo with a potentiometer earlier and could control speed and direction clockwise and counter clockwise by turning is there no way of implementing this? sorry I may sound naive this is the first coding I've done.

#### vinceherman

When you modified the servo by removing the pot, you remove the positional awareness.
It is no longer a servo, instead just a nicely boxed motor, gearbox and a controller that allows selecting the direction and limited speed control.

This is a common stumbling block for new users.

You can put limit switches on the rotating mass that will detect when the servo has reached the limit of travel each way.

You can put encoders on.

You can purchase servos that have more that 180 degrees of travel.  These may cost more.

#### slipstick

I set up the servo with a potentiometer earlier and could control speed and direction clockwise and counter clockwise by turning is there no way of implementing this? sorry I may sound naive this is the first coding I've done.
Controlling speed and direction are fine. But you want it to stop at specific positions around the circle. Could you control it to do that? That's the problem with continuous rotation ex-servos.

Steve

#### Robin2

You can get servos that will work through 360 degrees with position control though they are not common. And sail-winch servos can turn about 3 revolutions with position control.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up