Sweeping Motion for Simple Turret Code Help

Hello, I am working on a sentry turret that fires nerf darts. It uses a ultrasonic sensor so if someone comes in front of it, the gun shoots. Whenever power is plugged in the code uses the inital position of the gun as a starting point and sweeps with that location as the center. However, I noticed that whenever the blaster fires it resets the starting point as the place where it fires. Both of the servos are programmed as continuous. Any suggestions as to how to keep the starting point constant? Any help is appreciated :slight_smile:

//Sentry Turret Code by Dhruv Batra

//References
//https://www.arduino.cc/en/Reference/Servo
//https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

#include<Servo.h>

//define serovs
Servo pan;
Servo trig;

//define pins
const int trigPin = 12;
const int echoPin = 13;

//define variables
long duration;
int dist;
int count=0;
int flag=0;
int pos=0;

void setup() {
// put your setup code here, to run once:
pinMode(trigPin,OUTPUT);//set trigPin as output
pinMode(echoPin,INPUT);//set echoPin as input
Serial.begin(9600); //Starts serial connection
pan.attach(10);
}

void loop() {
// put your main code here, to run repeatedly:
//clears trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

//Set trigPin on HIGH state for 10 micro-seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);

//read echopin, return soundwave travel time in ms
duration = pulseIn(echoPin,HIGH);

// Calculating the distance
int dist= duration*0.034/2;

//print distance on serial monitor
Serial.println();
Serial.print("Distance: ");
Serial.println(dist);

//print servo position
int position=pan.read();
Serial.print("Position: ");
Serial.println(position);
Serial.print("Count: ");
Serial.println(count);
Serial.println(“Flag:”);
Serial.print(flag);
Serial.println();

//sweep
if(dist>20 and flag==0)
{
pan.write(0); //speed in one direction 0-90
delay(200);
count+=1;
}else if(dist>120 and flag==1)
{
pan.write(180);//speed in other direction 90-180
delay(200);
count-=1;
}else if(dist<20){
Serial.println(“BANG”);
trig.attach(9);
//pan.write(90);
trig.write(0);//servo rotates one way to pull trigger
delay(1000); //MODIFY ME how long for ^
trig.write(180);//servo rotates back so string is taunt and gun ready for another trigger
delay(25);//MODIFY ME how long for ^
trig.detach();
}

//switch sweep
if (count>=3)
flag=1;
if (count<=-3)
flag=0;

}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
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?

Thanks.. Tom.... :slight_smile:

Both of the servos are programmed as continuous.

What does that mean?

(You’ve been a member here for over two years and you haven’t figured out code tags?)