Need help with this code

I have written this code to power a servo motor by using a button and arduino ide does not find any errors in it. However when i load it onto my arduino the servo motor acts like it is not running any program. I have already checked if i messed up the inputs and if the motor works with other programs. (the serial monitor code was to check if the program works in general)

`#include <Servo.h>


Servo servo;
int taster = 7;
int tasterstatus = 0;

void setup()
{
  pinMode(taster,INPUT);
  servo.attach(6);
  Serial.begin(9600);
}

void loop()
{
  tasterstatus=digitalRead(taster);
  if (tasterstatus==HIGH){
    servo.write(500);
    Serial.println("test");
  } 
  else {
    servo.write(200);
  }

}`

Welcome to the forum

Normal servos have a range of movement between 0 and 180 degrees. The servo.write() function takes the angle to be moved to as its parameter

Can you see a problem in your code with how you use servo.write() ?

I see. so the problem is that the maximum i can use is 180 and not 500?

That's right

Why do you want to use 500 ?
A servo cannot move to an angle of 500 degrees

I think i misread something because i thought it would stand for the speed at which it rotates so i just used a convenient number. Im not that familiar with these things.

Try using numbers in the range 0 to 180 to get a feel for how it works. Assuming, that is, that you have got a conventional servo and not a continuous rotation "servo" unless that is what you really want

You cannot control the angle of the latter type, only its speed and direction. Which type do you have/want ?

Hi, @ouroboros-twinstrokeoftheend
Welcome to the forum.

Thanks for using code tags. :+1: :+1: :+1:

Can you post a link or image of your servo.

Usually a servo motor is one that rotates continuously with encoder feedback.

Then there is a servo, which generally is the type used in remote control for 0 to 180 degrees back and forth operation.

Tom.. :smiley: :+1: :coffee: :australia:

In my experience it is unusual for a continuous rotation "servo" of the type used by members of this forum to have any form of encoder feedback

The write() method is fairly smart as it converts to the internal use of microseconds.

void Servo::write(int value)
{
  if(value < MIN_PULSE_WIDTH)
  {  // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
    if(value < 0) value = 0;
    if(value > 180) value = 180;
    value = map(value, 0, 180, SERVO_MIN(),  SERVO_MAX());
  }
  this->writeMicroseconds(value);
}

MINPULSEWIDTH, 544 unless changed, is threshold for seeing the argument passed as a value in time, not degrees.

So both 200 and 500 are constrained to 180 degrees, then mapped into a range of pulse widths.

I'm not sure that is such a good idea. I like constraining to 0 .. 180, and I do not see the advantage to the flexibility afforded by allowing, say, 1000 .. 2000 to be used and interpreted differently, mostly because anyone doing would know enough to use writeMicroseconds().

Chicken door openers use degrees, more sophisticated code like found in radio control stuff happily uses pulse width.

writeMicroseconds() similarly constrains the input value, which is always nice to see but should probably not be why something works.

a7

1 Like

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