Planning and Implementing an Arduino Program

CHAPTER 3 - SERVO

Now let's start to add some code so that something actually happens. We will start with the servo as that is easiest.

We need to add a reference to the servo library at the top of the code - before setup(). (Libraries are pre-written code that we use to simplify common tasks). We need to decide which pin will be used for the servo signal. Because this number won't be changed by the program it can be defined as a constant. And we need to attach the servo to that pin. The new code looks like this

#include <Servo.h>

Servo myServo;
const byte servoPin = 5;

void setup() {
  myServo.attach(servoPin);
}

Then we need a variable to record the angle the servo is required to move to. As the range of angles is 0 to 180 a byte (which can hold values from 0 to 255) will be sufficient. Some servos can't move to 0 degrees or to 180 degrees so lets also set min and max angles. Let's start servoPosition at servoMin.

const byte servoMin = 20;
const byte servoMax = 160;
byte servoPosition = servoMin;

It is much better to define a named constant (such as servoMin) rather than just include the number (20 in this case) directly in your code. That's because if you find you need to change the number you only need to edit one line of your code rather than remember to change everywhere that you had the number 20.

We will move the servo to the servoPosition when we attach the servo. We also need to include a delay to allow the servo time to move - and to allow us time to see what is happening. We can use the delay() function in setup() because it will only happen once and we won't need anything else to happen at the same time. The delay() function should not be used in loop().

myServo.write(servoPosition);
delay(5000);

Finally, let's add code to the moveServo() function. All that's needed is

myServo.write(servoPosition);

If you have a very small servo with nothing connected to the servo arm (so there is no load on it) you can probably power the servo from the Arduino 5V pin - with the servo signal wire connected to pin 5 (in this case) and the servo GND wire connected to the Arduino GND pin. With a bigger servo there is a risk that it will draw too much current from the Arduino causing it to reset with all sorts of strange consequences. If you think that may be happening give the servo its own power supply but be sure to connect the GND to the Arduino GND.

The full code is in LessonB.ino - try it and see what happens.

// LessonB.ino
// Program with some servo activity

#include <Servo.h>

Servo myServo;
const byte servoPin = 5;
const byte servoMin = 20;
const byte servoMax = 160;
byte servoPosition = servoMin;

void setup() {
  myServo.attach(servoPin);
  myServo.write(servoPosition);
  
  delay(5000);
}

void loop() {
    checkButtons();
    setFlashPeriod();
    flashLedA();
    flashLedB();
    
    askForUserInput();
    getUserResponse();
    
    readPotentiometer();
    setServoPosition();
    moveServo();
}

void flashLedA() {

}

void flashLedB() {

}

void checkButtons() {

}

void setFlashPeriod() {

}

void askForUserInput() {

}

void getUserResponse() {

}

void moveServo() {
  myServo.write(servoPosition);
}

void readPotentiometer() {

}

void setServoPosition() {

}

...R

2 Likes