# Need help with this code

**URL:** <https://forum.arduino.cc/t/need-help-with-this-code/1328348>\
**Category:** Programming\
**Created:** [December 3, 2024, 7:20am UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348 "2024-12-03T07:20:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ouroboros-twinstrokeoftheend](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ouroboros-twinstrokeoftheend/32/1099504_2.png) [@ouroboros-twinstrokeoftheend](https://forum.arduino.cc/u/ouroboros-twinstrokeoftheend)\
**Post date:** [December 3, 2024, 7:20am UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/1 "2024-12-03T07:20:35Z")

</div>

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)

```arduino
`#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);
  }

}`

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 3, 2024, 7:24am UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/2 "2024-12-03T07:24:58Z")

</div>

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() ?

---

<div class="post-metadata">

**Author:** ![ouroboros-twinstrokeoftheend](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ouroboros-twinstrokeoftheend/32/1099504_2.png) [@ouroboros-twinstrokeoftheend](https://forum.arduino.cc/u/ouroboros-twinstrokeoftheend)\
**Post date:** [December 3, 2024, 7:28am UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/3 "2024-12-03T07:28:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 3, 2024, 7:44am UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/4 "2024-12-03T07:44:08Z")

</div>

That's right

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

---

<div class="post-metadata">

**Author:** ![ouroboros-twinstrokeoftheend](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ouroboros-twinstrokeoftheend/32/1099504_2.png) [@ouroboros-twinstrokeoftheend](https://forum.arduino.cc/u/ouroboros-twinstrokeoftheend)\
**Post date:** [December 3, 2024, 8:12am UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/5 "2024-12-03T08:12:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 3, 2024, 8:20am UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/6 "2024-12-03T08:20:13Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [December 3, 2024, 9:36am UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/7 "2024-12-03T09:36:49Z")

</div>

Hi, @ouroboros-twinstrokeoftheend  
Welcome to the forum.

Thanks for using code tags. 👍 👍 👍

> [@ouroboros-twinstrokeoftheend](#):
>
> 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.

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.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 3, 2024, 10:03am UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/8 "2024-12-03T10:03:29Z")

</div>

> [@TomGeorge](#):
>
> Usually a **servo motor** is one that rotates continuously with encoder feedback.

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

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [December 3, 2024, 8:49pm UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/9 "2024-12-03T20:49:11Z")

</div>

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

```arduino
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);
}

```

_MIN __PULSE__ WIDTH_, 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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 1, 2025, 8:49pm UTC](https://forum.arduino.cc/t/need-help-with-this-code/1328348/10 "2025-06-01T20:49:52Z")

</div>

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