Servo Library and Functions include Servo h Servo
Servo Library and Functions #include <Servo. h> Servo myservo 1; void setup() { myservo 1. attach(2); myservo 1. write. Microseconds(1000); } void loop() { } 1
DISCLAIMER & USAGE • The content of this presentation is for informational purposes only and is intended for students attending Louisiana Tech University only. • The authors of this information do not make any claims as to the validity or accuracy of the information or methods presented. • Any procedures demonstrated here are potentially dangerous and could result in damage and injury. boosting application-focused learning through student ownership of learning platforms • Louisiana Tech University, its officers, employees, agents and volunteers, are not liable or responsible for any injuries, illness, damage or losses which may result from your using the materials or ideas, or from your performing the experiments or procedures depicted in this presentation. • The Living with the Lab logos should remain attached to each slide, and the work should be attributed to Louisiana Tech University. • If you do not agree, then please do not view this content. 2
Review - Servos are Controlled with Electrical Pulses voltage (V) pulse width varies between roughly between 1 ms and 2 ms 5 V - 20 ms 0 V - time (milliseconds) void loop() { digital. Write(12, HIGH); delay. Microseconds(1000); digital. Write(12, LOW); delay(20); } // hold pin 12 high for 1000μs or 1 ms // hold pin 12 low for 20 ms 3
Libraries • Libraries are computer programs written to add functionality to Arduino programs. • A library to control hobby servos is included by default in the Arduino IDE. . . you already have this library on your computer. • A host of libraries are available for Arduino, some must be downloaded separately. • See www. arduino. cc/en/Reference/Libraries for some of the available libraries. 4
Manual Pulses versus Servo Library Both programs below cause a servo attached to pin 2 to continuously spin full speed forward void setup(){ pin. Mode(2, OUTPUT); } void loop() { digital. Write(2, HIGH); delay. Microseconds(1000); digital. Write(2, LOW); delay(20); } #include <Servo. h> Servo myservo 1; put this statement in your program to use library. . . don’t forget the space “declare” the name of your servo as “myservo 1”. . . can choose any name void setup() { indicate that servo will be wired to pin 2 myservo 1. attach(2); myservo 1. write. Microseconds(1000); } void loop() { } send a pulse of 1000 μs or 1 ms out pin 2 This program continues to send pulses to the servo so that it continues to run write. Microseconds(1000) continues to send pulses to the servo unless the myservo 1. detach() function is called Almost all of the execution time is spent on the delay(20) function. . . and you can’t do anything else while this is happening You could also send write. Microseconds(1500) to stop the motion of the servo without detaching it When using the servo library, you can do other things, like check whisker status, while the servo is running Write a sketch that uses the servo library turn both of your servos full speed forward continuously. 8
Creating a User-Defined Functions are lines of code that are given a name and written once at the end of a sketch. The name can then be used within the sketch instead of writing the code multiple times. With User-Defined Functions: Without User-Defined Functions: SAME UPPER PART OF SKETCH #include <Servo. h> Servo myservo 1; Servo myservo 2; void loop() { forward(); delay(2000); backward(); delay(5000); } void setup() { myservo 1. attach(2); myservo 2. attach(12); } void loop() { myservo 1. write. Microseconds(1000); myservo 2. write. Microseconds(2000); delay(2000); Makes Arduino go full speed forward myservo 1. write. Microseconds(2000); myservo 2. write. Microseconds(1000); delay(5000); Makes Arduino go full speed backward } Calls on the forward function Calls on the backward function void forward(){ myservo 1. write. Microseconds(1000); myservo 2. write. Microseconds(2000); } void backward() { myservo 1. write. Microseconds(2000); myservo 2. write. Microseconds(1000); “void” means the function doesn’t return anything when it is completed (no numbers are sent back to the loop) } Play around with the sketch above! For example, add multiple forward and backward motions for different amounts of time using user-defined functions. Try adding a turn function. 6
Around the Pencil Challenge Use the servo library and user-defined functions to complete the following challenge: • Start with all three wheels on a sheet of paper • Navigate your bot around the pencil at the end of one floor tile • Return your bot to the sheet of paper (all three wheels on the paper) 36 in Sheet of paper 36 in Pencil Tip: Form a plan first. Break the challenge up into pieces. Write a flowchart or words that describe the different pieces of the challenge that must be accomplished. Then try to code each task individually. 1 Floor Tile Extra Challenge: Have the motion start after pressing the whisker. 7
- Slides: 7