/*
MSJ Researchers World
Date - 22nd OCT 2024
Mentor - Mr. Siranjeevi M
Contact - 7373771991
*/
#include <Servo.h>
Servo myServo; // Create a servo object
const int potPin = A0; // Analog pin connected to potentiometer
int potValue = 0; // Variable to store the potentiometer value
int servoPos = 0; // Variable to store the servo position (0 to 180)
void setup()
{
myServo.attach(9); // Attach the servo to pin 9
Serial.begin(9600); // Initialize serial communication
}
void loop()
{
potValue = analogRead(potPin); // Read the potentiometer value (0-1023)
// Map the potentiometer value to a servo position (0-180 degrees)
servoPos = map(potValue, 0, 1023, 0, 180);
// Move the servo to the corresponding position
myServo.write(servoPos);
// Print the potentiometer and servo values to the serial monitor
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" | Servo Position: ");
Serial.println(servoPos);
delay(15); // Small delay to smooth out the servo movement
}