> For the complete documentation index, see [llms.txt](https://htooaunklinn-organization.gitbook.io/htooaunklinn-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://htooaunklinn-organization.gitbook.io/htooaunklinn-docs/hardware-and-iot/arduino-on-macos/page-4.md).

# Page 4

<figure><img src="https://3422137941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJ1pJMMB2xXaxqLgUEeCs%2Fuploads%2FbpFDHSPoChPGKR8E9VKk%2Ff.png?alt=media&amp;token=5d41843c-9df4-4e5a-822a-9a376dc9abc1" alt="" width="375"><figcaption></figcaption></figure>

```cpp
#include <Servo.h>

Servo servo1;

int potPin = A0;
int potValue = 0;
int delayTime = 0;
int angle = 0;
bool increasing = true;

void setup() {
  servo1.attach(A1);
}

void loop() {
  // Read the potentiometer value (0 to 1023)
  potValue = analogRead(potPin);
  
  delayTime = map(potValue, 0, 1023, 10, 0);  // Adjust delay range as needed (500ms to 50ms)

  servo1.write(angle);

  if (increasing) {
    angle++;
    if (angle >= 90) {
      increasing = false;  // Change direction when 45° is reached
    }
  } else {
    angle--;
    if (angle <= 0) {
      increasing = true;
    }
  }

  delay(delayTime);
}
```
