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();
}

