Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h> // Include the Servo library
- Servo myServo; // Create a Servo object
- int servoPin = 9; // Define the pin connected to the servo motor
- void setup() {
- myServo.attach(servoPin); // Attach the servo to pin 9
- Serial.begin(9600); // Start the Serial communication
- myServo.write(90); // Initialize servo to 90 degrees (straight)
- Serial.println("Enter 'L' for Left, 'R' for Right, 'S' for Straight (90 degrees):");
- }
- void loop() {
- if (Serial.available() > 0) {
- char command = Serial.read(); // Read the input from Serial
- command = toupper(command); // Convert to uppercase for uniformity
- switch (command) {
- case 'L': // Turn left
- myServo.write(0); // Move servo to 0 degrees
- Serial.println("Servo turned Left (0 degrees)");
- break;
- case 'R': // Turn right
- myServo.write(180); // Move servo to 180 degrees
- Serial.println("Servo turned Right (180 degrees)");
- break;
- case 'S': // Turn straight (90 degrees)
- myServo.write(90); // Move servo to 90 degrees
- Serial.println("Servo set to Straight (90 degrees)");
- break;
- default: // Invalid input
- Serial.println("Invalid command. Enter 'L', 'R', or 'S'.");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement