Page 4

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

Last updated