the servo works

This commit is contained in:
Milad Mohtashamirad 2025-08-17 15:41:49 +10:00
parent 75c0a2af6e
commit 173fab6433
1 changed files with 27 additions and 14 deletions

View File

@ -1,20 +1,33 @@
// Blink LED on pin 13, read analog input from A0, and print to Serial #include <Servo.h>
Servo myServo; // Create a Servo object
const int servoPin = 10; // D10 is OC1B on Pro Mini
const int ledPin = 13;
const int analogPin = A0;
void setup() { void setup() {
pinMode(13, OUTPUT); // Set pin 13 as output pinMode(ledPin, OUTPUT);
Serial.begin(115200); // Start serial communication at 9600 baud Serial.begin(115200); // Initialize Serial
myServo.attach(servoPin); // Attach servo to pin 10
} }
void loop() { void loop() {
digitalWrite(13, HIGH); // Turn the LED on int analogValue = analogRead(analogPin); // Read from A0 (01023)
int analogValue = analogRead(A0); // Read analog value from A0 int angle = map(analogValue, 0, 1023, 0, 180); // Map to 0180 degrees
Serial.print("LED ON - Analog Value: ");
Serial.println(analogValue); // Print the analog value
delay(1000); // Wait 1 second
digitalWrite(13, LOW); // Turn the LED off myServo.write(angle); // Set servo angle
analogValue = analogRead(A0); // Read analog again
Serial.print("LED OFF - Analog Value: "); digitalWrite(ledPin, HIGH);
Serial.println(analogValue); // Print the analog value // Serial.print("LED ON - Analog: ");
delay(1000); // Wait 1 second // Serial.print(analogValue);
// Serial.print(" | Angle: ");
// Serial.println(angle);
delay(10);
digitalWrite(ledPin, LOW);
// Serial.print("LED OFF - Analog: ");
// Serial.print(analogValue);
// Serial.print(" | Angle: ");
// Serial.println(angle);
delay(10);
} }