Advertisement
DrAungWinHtut

stearing_servo.ino

Jan 14th, 2025
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h> // Include the Servo library
  2.  
  3. Servo myServo; // Create a Servo object
  4. int servoPin = 9; // Define the pin connected to the servo motor
  5.  
  6. void setup() {
  7.   myServo.attach(servoPin); // Attach the servo to pin 9
  8.   Serial.begin(9600); // Start the Serial communication
  9.   myServo.write(90); // Initialize servo to 90 degrees (straight)
  10.   Serial.println("Enter 'L' for Left, 'R' for Right, 'S' for Straight (90 degrees):");
  11. }
  12.  
  13. void loop() {
  14.   if (Serial.available() > 0) {
  15.     char command = Serial.read(); // Read the input from Serial
  16.     command = toupper(command);  // Convert to uppercase for uniformity
  17.  
  18.     switch (command) {
  19.       case 'L': // Turn left
  20.         myServo.write(0); // Move servo to 0 degrees
  21.         Serial.println("Servo turned Left (0 degrees)");
  22.         break;
  23.  
  24.       case 'R': // Turn right
  25.         myServo.write(180); // Move servo to 180 degrees
  26.         Serial.println("Servo turned Right (180 degrees)");
  27.         break;
  28.  
  29.       case 'S': // Turn straight (90 degrees)
  30.         myServo.write(90); // Move servo to 90 degrees
  31.         Serial.println("Servo set to Straight (90 degrees)");
  32.         break;
  33.  
  34.       default: // Invalid input
  35.         Serial.println("Invalid command. Enter 'L', 'R', or 'S'.");
  36.         break;
  37.     }
  38.   }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement