Question about Servo function and angle detection at first run.

Greetings,

While waiting for some components for my robotic arm I decided to write the code. Searching for documentation I managed to finish the code but I have a question. Let's see for example the following lines:

int angle1=0;

if(button='1')
{
for(angle1 = 0; angle1 < 180; angle1 += 1)
{
servo_moto1.write(angle1);
delay(5);
}

  • as long as button '1' is pressed the servo_motor1 will rotate to one direction 180 degrees.

If I am understanding correctly the motor needs to be physically at 0 degrees for that for-function to work, right?

What happens if physically my motor is at 35 degrees when I first power/run it? Will this for-function break the motor?

Thank you in advance :slight_smile:

It will swing from 35 to 0 in one "go", then start the 1 degree-at-a-time stepping.

A servo on its own should be ok with doing that, but whether or not that breaks anything, will depend on the mechanical setup.

Your code snipe is concerning as your seeming to be using a for loop even without a next call, when would it stop as “button” is never checked inside the “for”.

Even the use of a delay 5 makes little sense as it’s a servo motor and it’s going to go to the specified PWM position regardless what the Arduino does next.

Servos get updated every 20ms and move slowly, so a delay of 5ms is a bit pointless.

It seems that every time when I reset the program, the position of the motor will reset too.

For example if I set the initial angle to 90 degrees, the servo motor first will go at 90 degrees physically and then will start to rotate depending of what I wrote.

It's interesting, I need to find how does the motor know to start at the default position.

The default position of 90 is in the servo library.

You can specify the position you start the servo at, if 90 doesn't suit, by doing a write() to your preferred position before the attach(). But if it's not already sitting there from the previous time, it will (try to) move to that position.

Unless you use uncommon 4-wire servos like these for example and read the extra wire for position, there is no way of knowing where a servo actually is at any point.

meltDown:
The default position of 90 [edit: degrees] is in the servo library.

Strictly speaking I should have said the default pulse is 1500 [edit: ms]. From servo.h:

#define DEFAULT_PULSE_WIDTH  1500     // default pulse width when servo is attached

I managed to write this code.

I have a phone witch sends a byte in ASCII code for the bluetooth functin -if-

For example: if bluetooth=='1' , it checks if the ASCII code sent from phone is 49. When any of the arrows/button are stopped being pressed the phone transmits the ASCII code corresponding to X.

I check with a servo motor every button if it works, and it does but strangely when I connect 2 servo motors, in paralel with 5V line and ground + the PWM accordingly to the axis, the motors shake and start moving randomly without pressing any keys.

The following code is:

#include <Servo.h>
int bluetooth=0;
Servo servo_moto1; //functia servo, de fapt e o functie liniara
Servo servo_moto2;
Servo servo_moto3;
Servo servo_moto4;
Servo servo_moto5;
Servo servo_moto6;

int angle1=1;
int angle2=1;
int angle3=1;
int angle4=1;
int angle5=1;
int angle6=1;

void setup()
{
servo_moto1.attach(3); // A1
servo_moto2.attach(4); // A2
servo_moto3.attach(5); // B1
servo_moto4.attach(6); // C1
servo_moto5.attach(7); // D1
servo_moto6.attach(8); // Cleste
Serial.begin(9600); //default comm
}

void loop()
{
if(Serial.available()>0)
{
bluetooth=Serial.read();
}
if(bluetooth=='1') //motorul A1 49,50
{
for(angle1; angle1 < 180; angle1 += 1)
{
servo_moto1.write(angle1);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}
if(bluetooth=='2')
{
for(angle1; angle1 >= 1; angle1 -= 1)
{
servo_moto1.write(angle1);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}

if(bluetooth=='3') //motorul A2 51,52
{
for(angle2; angle2 < 180; angle2 += 1)
{
servo_moto2.write(angle2);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}
if(bluetooth=='4')
{
for(angle2; angle2 >= 1; angle2 -= 1)
{
servo_moto2.write(angle2);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}

if(bluetooth=='5') //motorul B1 53,54
{
for(angle3; angle3 < 180; angle3 += 1)
{
servo_moto3.write(angle3);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}
if(bluetooth=='6')
{
for(angle3; angle3 >= 1; angle3 -= 1)
{
servo_moto3.write(angle3);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}

if(bluetooth=='7') //motorul C1 55,56
{
for(angle4; angle4 < 180; angle4 += 1)
{
servo_moto4.write(angle4);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}
if(bluetooth=='8')
{
for(angle4; angle4 >= 1; angle4 -= 1)
{
servo_moto4.write(angle4);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}

if(bluetooth=='9') //motorul D1 55,56
{
for(angle5; angle5 < 180; angle5 += 1)
{
servo_moto5.write(angle5);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}
if(bluetooth=='0')
{
for(angle5; angle5 >= 1; angle5 -= 1)
{
servo_moto5.write(angle5);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}

if(bluetooth=='A') //motorul cleste 65,66
{
for(angle6; angle6 < 180; angle6 += 1)
{
servo_moto6.write(angle6);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}
if(bluetooth=='B')
{
for(angle6; angle6 >= 1; angle6 -= 1)
{
servo_moto6.write(angle6);
delay(20);
bluetooth=Serial.read();
if(bluetooth=='X')
{
break;
}
}
}

}

What I am doing wrong?

1 Like

i33SoDA:
What I am doing wrong?

Failing to use code tags? If you have to ask, go read the sticky post at the top of the forum named How to use this forum - please read.

Please give us a wiring diagram of your project. Pencil, paper and a camera are good enough. Pay special attention to how you power the servos.

Hint, 95% of all servo problems are inadequate power supply.

1 Like

If you'd done any reading of the many threads about servos in this forum you'd know that powering even one small servo from the Arduino's 5V pin is a bad idea. More than one is a very bad idea. Trying to power 6 servos from that low current 5V pin is sheer insanity.

It would be quite surprising if you haven't already damaged the Arduino. Power your servos from a separate supply, 4 or 5 NiMH rechargeable cells might probably be o.k., depending on exactly what servos you are using.

Steve

1 Like

Thank you all for the information.

It works perfectly now and I understand perfectly what I did wrong.

You guys rock! :slight_smile: