Introduction: Writing the Code

The following information is a single lesson in a larger project. Find more great projects here.

Return to Previous Lesson: Setting Up the Circuit

Lesson Overview:

Now we'll write our mood software!

Step 1: Introduction

In this lesson you will learn how to write the code that connects the potentiometer to the servo motor. Servo motors can easily be controlled by the Arduino using a library, which is a collection of code that extends a programming environment to include more useful commands! You will use a library called Servo.h in this project.

Our version of the Mood Cue circuit is already in the Workplane. It should look a lot like the circuit that you created in the previous lesson!

  1. Continue to the next step.

Step 2: Import the Servo Library

To use the servo library, you'll first need to import it. This makes the additions from the library available to your sketch.

  1. Copy the code below into the Code Editor in order to add the servo library. Notice that there is no semicolon (;) at the end of this line! #include
  2. In the simulator, you can also include the Servo library by clicking the "Libraries" tab and clicking the blue "include" button.
  3. Continue to the next step.

Step 3: Creating the 'Servo' Object

To refer to the servo, you’re going to need to create a named instance of the servo library in a variable. This is called an "object."

  1. Copy the code below into the Code Editor. Servo myServo;
  2. When create the servo object, you’re making a unique name that will have all the functions and capabilities that the servo library offers. From this point on in the program, every time you refer to myServo, you’ll be talking to a specific servo object.
  3. Continue to the next step.

Step 4: Variable Declaration

Set up a named constant for the analog pin the potentiometer is attached to.

Also create variables to hold the analog input value and the angle you want the servo to move to. The servo angle will have an initial value of 0 degrees.

  1. Copy the code below, including comments, into the Code Editor. int const potPin = A0; //analog pin A int potVal; //stores value of pot int angle = 0; //stores position of servo
  2. Continue to the next step.
  3. Stuck? HINT: Notice that the variable potVal doesn't have a value assigned to it yet. We don't know what value it should have until we read the analog pin! The angle of the servo motor will start at 0 when the program begins.

Step 5: Connect Servo to Analog Pin

In the setup() function, you’re going to need to tell the Arduino what pin your servo is attached to.

  1. Copy the code, including comment, into the Code Editor. void setup() { myServo.attach(9); //tells board which pin the servo is on

  2. Continue to the next step.

Step 6: Initialize the Serial Port

Include a serial connection so you can watch the values from the potentiometer and see how they map to angles on the servo motor.

  1. Copy the code below into the Code Editor. Serial.begin(9600); } //end of setup()

  2. Don't forget to include the left {curly bracket} to close the setup() function!

  3. Continue to the next step.

Step 7: Read the Potentiometer Value

In the loop() function, you will read the analog input from the pot and print out the value to the serial monitor.

  1. Copy the code below into the Code Editor. void loop() { potVal = analogRead(potPin); Serial.print("potVal: "); Serial.print(potVal);

  2. In the serial monitor, the value associated with the potentiometer input will be between 0 and 1023 and the text will look like this (for example): potVal: 500

  3. Continue to the next step.

  4. Stuck? HINT: Consider adding your own comments to remind yourself what each block of code does!

Step 8: Mapping Pot Values to Servo Values

To create a usable value for the servo motor from your analog input, it’s easiest to use a function called map(). This handy function scales numbers for you! In this case it will translate potentiometer input values (0-1023) to motor angle values (0-179 degrees).

The map() function is explained below. You will also print out the mapped value to the serial monitor.

  1. Copy the code below, including comment, into the Code Editor. angle = map(potVal, 0, 1023, 0, 179); //angle is the pot value re-scaled to 0-179 Serial.print(", angle: "); Serial.println(angle);
  2. The map() function takes five arguments : the number to be scaled (here it\u2019s potVal), the minimum value of the input (0), the maximum value of the input (1023), the minimum value of the output (0), and the maximum value of the output (179). We'll store this new value in the "angle" variable.
  3. Including the printed information from the previous step, the text in the serial monitor will look like (for example): potVal: 500, angle: 87.9
  4. Continue to the next step.
  5. Stuck? HINT: Recall that Serial.println() will jog the next set of data to the next line in the serial monitor.

Step 9: Rotating the Servo

Finally, it’s time to move the servo. The write() function moves the motor to the angle you specify!

Afterwards, put a delay of about 15 milliseconds so the servo has time to move to its new position before going back to the beginning of the loop() function.

  1. Copy the last section of code below to the Code Editor. myServo.write(angle); delay(15); } //end of loop()

  2. Continue to the next step.

Step 10: Use It!

Now you are ready to simulate the circuit, or upload and run your code on the Arduino Uno! 

Once your Arduino has been programmed, open the serial monitor. When you turn the potentiometer, you will see the numbers change and the servo motor moving to a new position!

  1. Upload & Run your code in the simulator or upload your sketch to the Arduino Uno and open up the serial monitor.
  2. In the serial monitor, you will see a string of numbers that represents the analog data from the pot and the mapped angle value used to turn the servo motor.
  3. Try turning the potentiometer. Notice the relationship between the values of potVal and angle, and the position of the servo. You should see consistent results as you turn the pot!
  4. Continue to the next step.

Step 11: More About Servo Motors

Servo motors are regular motors with a number of gears and some circuits inside. The mechanics inside provide feedback to the circuit, so it is always aware of its position. While it may seem like this is a limited range of motion, it’s possible to get it to make a wide variety of different kinds of movements with some additional mechanics.

There are a number of resources that describe mechanisms in detail like robives.com/ mechs and the book Making Things Move by Dustyn Roberts.

  1. Continue to the next step.

Step 12: Review

Congratulations on finishing the Mood Cue circuit and code!

In this lesson you used an analog input and the Servo.h library to control a servo motor.

One nice thing about using potentiometers as analog inputs is that they will give you a full range of values between 0 and 1023. This makes them helpful in prototyping projects that use analog input.

  1. Continue to the next lesson to learn how to create a paper gauge for the Mood Cue and for some extra challenges.

Next Lesson:Creating a Gauge

Comments

About This Instructable

469views

1favorite

License:

More by circuits:Move Circuit AssemblyGlow Circuit AssemblyIntroduction
Add instructable to: