Arduino trashcan not working

Hi,
I made a simple Arduino trashcan. The code works fine and the components are connected correctly but I have been using a 9V battery for my servo motor. I was told that I need to give it only 5V of power but I don't know how to do that.
Here is the code itself:

#include <Servo.h>
#include <NewPing.h>

NewPing sonar (11, 12, 20); // First pin is trigger pin, second pin is echo pin, maximum distance

Servo myservo; // define servo name

void setup()
{
myservo.attach(9); // pin to wich servo is attached
myservo.write(0); //reset servo to starting position
myservo.detach(); //to avoid any random movements, detach servo
delay(50);
}
void loop()
{
if (sonar.ping_cm()>1) // if there is any mtion detected
{
myservo.attach(9);
myservo.write(135);//move servo by 135 degrees
delay(500);//keep lid open for one second

//If an object is present continuasly:
do
{
delay(1000); //keep lid open for 1cm
}
while (sonar.ping_cm()!=0); //as long as there is an object present

}
myservo.write(0);
delay(1000);
myservo.detach();

}

If you will measure the voltage on your servo while it is running, it probably is near 5 volts if you are using one of the smoke alarm 9 volt batteries.

Paul

I believe you have not had this operational long on the battery. You are burning power in the arduino that the battery will not supply for very long. You can get a buck converter for the servo that can be controlled with a logic level which would go to your arduino, Then you leave it off until you want to run the servo. You need to investigate the sleep mode but again the UNO board sucks power. I think a 10V - 12V wall wart, maybe 1A to power the thing will save you a lot of grief in the long run unless you go to a custom design.
Good Luck & Have Fun!
Gil

Here's OP's pic:

OP here's how to inline an image

Here's how to use an external (that is to say, not through the Arduino) power supply for a servo: