I got an Otto robot kit with 4 servo motor, Arduino nano, Arduino sensor shield included.
When I'm starting to do some basic programming with these setup. I got some problems with my servo motors.
When I try to run this code for rotating 45 degree one of my servo motor, it rotating forever. I searched almost every article about this issue but nothing could solve.
Note: The arduino nano's power supply coming from usb cable that connected to the PC.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(4); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 45; pos++) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
This sounds not correct. Learn the difference between a servo motor and a servo. They are very different in what they do.
Don't expect helpers to use that kit. We are newbies to it, know nothing. Think like this: How will You explain the situation for Your grandmother?
That can be not good. USB is not made for supplying motor currents. Some USB only provide 0.5 Amps. Motors should in general not be powered by the controller board.
void loop()
{
static bool Flag;
if(Flag == false)
{
for(pos = 0; pos <= 45; pos++) // goes from 0 degrees to 45 degrees
{
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
} //END of for( )
Flag = true;
} //END of if( )
} //END of loop( )
This code will execute a loop that will iterate 45 times. The loop variable, pos , is initialized to 0 before the loop starts. On each iteration of the loop, the value of pos will be incremented by 1. The loop will continue to execute until the value of pos is no longer less than 45.