Wrong spinning servo!

hello, im having trouble with servo's code
it isn't spinning 270 degrees. tho, I tried fixing it but it still isnt working

#include <Servo.h>   //servo library
Servo servo;     
int trigPin = 5;    
int echoPin = 6;   
int servoPin = 7;
int led= 10;
long duration, dist, average;   
long aver[3];   //array for average


void setup() {       
    Serial.begin(9600);
    servo.attach(servoPin);  
    pinMode(trigPin, OUTPUT);  
    pinMode(echoPin, INPUT);  
    servo.write(0);         //close cap on power on
    delay(100);
    servo.detach(); 
} 

void measure() {  
 digitalWrite(10,HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2) / 29.1;    //obtain distance
}
void loop() { 
  for (int i=0;i<=2;i++) {   //average distance
    measure();               
   aver[i]=dist;            
    delay(10);              //delay between measurements
  }
 dist=(aver[0]+aver[1]+aver[2])/3;    

if ( dist<50 ) {
//Change distance as per your need
 servo.attach(servoPin);
  delay(1);
 servo.write(0);  
 delay(3000);       
 servo.write(270);    
 delay(1000);
 servo.detach();      
}
Serial.print(dist);
}`Preformatted text`

Hi itsmejoy,

welcome to the arduino-forum. For best support to you

RE-edit
your

first post

that your code is presented as a code-section by following this short tutorial.

best regards Stefan

Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

After you go through an remove all of those extra ******

1 Like

Most hobby servos are limited to 0-180 degrees (if that). What makes you think that your servo will do 270 degrees?

1 Like

I'm pretty sure that your assumption that a servo can turn 270 degrees is wrong!
normal RC-servos turn 180 degrees some servo maybe 190 degrees.

Or if it is a continous rotation servo it will go on spinning and spinning and spinning.
Anyway you have to specify which exact type of servo you are using.
maybe now a days some digital servos are able to turn 270 degrees on an expanded pulsewith-range.

The standard servo-library limits the pulsewith from 1000 to 2000 microseconds. You can change this but it is unlikely that a standard RC-servo can cope with shorter pulses than 600 microseconds or longer pulses than 2400 microseconds.

If you really need a turning-angle of 270 degrees you could use one of these super-modified servos
https://www.01mechatronics.com/supermodified_servos_list_teaser

But these servos need a completely different kind of control-signal

best regards Stefan
but

1 Like

im sorry im a total beginner i didn't know this but thanks anyway! really appreciate ur help

write a detailed description of what you want to do.

There is a good chance that different solutions are possible

1 Like

i didn't know that i thought all the servos limits are 360, but thanks anyway

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.