Having a bit of trouble stopping a Servo

Hello there! I’m working on a project that is basically a motion activated spinning laser pointer. I’m accomplishing the spinning by using a single servo. Here is my current code:

// Uses a PIR sensor to detect movement, buzzes a buzzer
// more info here: http://blog.makezine.com/projects/pir-sensor-arduino-alarm/
// email me, John Park, at jp@jpixl.net
// based upon:
// PIR sensor tester by Limor Fried of Adafruit
// tone code by michael@thegrebs.com
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0; 
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // 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
int laserPin = 12;
int servoPin = 11;

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(laserPin, OUTPUT);
  myservo.attach(11); 
  myservo.write(0);
  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
    digitalWrite(laserPin, HIGH);
     myservo.write(180);
     delay(8000);
     delay(300);
 
    
    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
      digitalWrite(laserPin, LOW);
      myservo.write(0);
      delay(4000);
      delay(300);    
      if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

Currently the servo does not stop vibrating or attempting to turn(I am not using a continuous servo) when it gets to either side. Power is coming from the USB connection. I’m new to this and would appreciate any help or advice. Thanks.

Hi

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

The spec of your servo would also help, it is not wise to use the USB or the Arduino supplies for servos, they draw too much current than can be supplied. Usually you need to provide a power supply for the servo itself. Is the servo a 0 to 180Deg servo or a continuous rotation servo?

Thanks...Tom...... :)

Is the servo capable of turning 180 degrees ? Not all of them are. What happens if you change the end points to say 30 and 150 ?

How exactly is the servo powered ? Via the Arduino or externally and what type of servo is it ?

UKHeliBob: Is the servo capable of turning 180 degrees ? Not all of them are. What happens if you change the end points to say 30 and 150 ?

How exactly is the servo powered ? Via the Arduino or externally and what type of servo is it ?

Bingo. My mistake for not reading the servo spec.. it only goes to 160. Making that edit and changing 0 to 30 stops the problem. Thank you both for responding and offering guidance so quickly. :)

Last question: What type of external power supply should I get to power servos and other things that draw too much current? If possible include a link or two so I can go ahead and order one. Thanks again!

What type of external power supply should I get to power servos and other things that draw too much current?

At a pinch 4AA batteries or any supply that can provide 6 volts or so at a couple of amps. Personally I use a 3S lipo and a Battery Eliminator Circuit but that's because I have them around.

Whatever you use connect the GNDs of the Arduino and power supply.