From 173fab6433d4199c982af0b78dc41a376771311b Mon Sep 17 00:00:00 2001 From: Milad Mohtashamirad Date: Sun, 17 Aug 2025 15:41:49 +1000 Subject: [PATCH] the servo works --- flow_controller.ino | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/flow_controller.ino b/flow_controller.ino index e828f52..d7b2ad0 100644 --- a/flow_controller.ino +++ b/flow_controller.ino @@ -1,20 +1,33 @@ -// Blink LED on pin 13, read analog input from A0, and print to Serial +#include + +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() { - pinMode(13, OUTPUT); // Set pin 13 as output - Serial.begin(115200); // Start serial communication at 9600 baud + pinMode(ledPin, OUTPUT); + Serial.begin(115200); // Initialize Serial + myServo.attach(servoPin); // Attach servo to pin 10 } void loop() { - digitalWrite(13, HIGH); // Turn the LED on - int analogValue = analogRead(A0); // Read analog value from A0 - Serial.print("LED ON - Analog Value: "); - Serial.println(analogValue); // Print the analog value - delay(1000); // Wait 1 second + int analogValue = analogRead(analogPin); // Read from A0 (0–1023) + int angle = map(analogValue, 0, 1023, 0, 180); // Map to 0–180 degrees - digitalWrite(13, LOW); // Turn the LED off - analogValue = analogRead(A0); // Read analog again - Serial.print("LED OFF - Analog Value: "); - Serial.println(analogValue); // Print the analog value - delay(1000); // Wait 1 second -} \ No newline at end of file + myServo.write(angle); // Set servo angle + + digitalWrite(ledPin, HIGH); + // Serial.print("LED ON - Analog: "); + // 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); +}