Connect with us

Arduino servo code

Discussion in 'Microcontrollers, Programming and IoT' started by altronic, Sep 29, 2017.

  1. altronic

    altronic

    6
    0
    Sep 20, 2017
    To run more than one servo.
    #include <Servo.h>

    Servo servoLeft; // Define left servo
    Servo servoRight; // Define right servo

    void setup() {
    servoLeft.attach(10); // Set left servo to digital pin 10
    servoRight.attach(9); // Set right servo to digital pin 9
    }

    void loop() { // Loop through motion tests
    forward(); // Example: move forward
    delay(2000); // Wait 2000 milliseconds (2 seconds)
    reverse();
    delay(2000);
    turnRight();
    delay(2000);
    turnLeft();
    delay(2000);
    stopRobot();
    delay(2000);
    }

    // Motion routines for forward, reverse, turns, and stop
    void forward() {
    servoLeft.write(0);
    servoRight.write(180);
    }

    void reverse() {
    servoLeft.write(180);
    servoRight.write(0);
    }

    void turnRight() {
    servoLeft.write(180);
    servoRight.write(180);
    }
    void turnLeft() {
    servoLeft.write(0);
    servoRight.write(0);
    }

    void stopRobot() {
    servoLeft.write(90);
    servoRight.write(90);
    }
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    9,129
    1,842
    Nov 17, 2011
    Do you have a specific question regarding your code?
     
    Ian and Arouse1973 like this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-