Download presentation
Presentation is loading. Please wait.
1
Servo Library and Functions
#include <Servo.h> Servo myservo1; void setup() { myservo1.attach(2); myservo1.writeMicroseconds(1000); } void loop() {
2
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. 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. boosting application-focused learning through student ownership of learning platforms
3
Review - Servos are Controlled with Electrical Pulses
time (milliseconds) voltage (V) 5V - 0V - 20ms pulse width varies between roughly between 1ms and 2ms void loop() { digitalWrite(12, HIGH); delayMicroseconds(1000); // hold pin 12 high for 1000μs or 1ms digitalWrite(12, LOW); // hold pin 12 low for 20ms delay(20); }
4
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 for some of the available libraries.
5
Manual Pulses versus Servo Library
Both programs below cause a servo attached to pin 2 to continuously spin full speed forward put this statement in your program to use library don’t forget the space #include <Servo.h> Servo myservo1; void setup() { myservo1.attach(2); myservo1.writeMicroseconds(1000); } void loop() { void setup(){ pinMode(2,OUTPUT); } void loop() { digitalWrite(2, HIGH); delayMicroseconds(1000); digitalWrite(2, LOW); delay(20); “declare” the name of your servo as “myservo1” can choose any name indicate that servo will be wired to pin 2 send a pulse of 1000 μs or 1ms out pin 2 This program continues to send pulses to the servo so that it continues to run Almost all of the execution time is spent on the delay(20) function and you can’t do anything else while this is happening writeMicroseconds(1000) continues to send pulses to the servo unless the myservo1.detach() function is called You could also send writeMicroseconds(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.
6
Creating a User-Defined Function
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. Without User-Defined Functions: With User-Defined Functions: SAME UPPER PART OF SKETCH #include <Servo.h> Servo myservo1; Servo myservo2; void setup() { myservo1.attach(2); myservo2.attach(12); } void loop() { myservo1.writeMicroseconds(1000); myservo2.writeMicroseconds(2000); delay(2000); myservo1.writeMicroseconds(2000); myservo2.writeMicroseconds(1000); delay(5000); void loop() { forward(); delay(2000); backward(); delay(5000); } void forward(){ myservo1.writeMicroseconds(1000); myservo2.writeMicroseconds(2000); void backward() { myservo1.writeMicroseconds(2000); myservo2.writeMicroseconds(1000); Calls on the forward function Calls on the backward function “void” means the function doesn’t return anything when it is completed (no numbers are sent back to the loop) Makes Arduino go full speed forward Makes Arduino go full speed backward 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.
7
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) 36in 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. Sheet of paper 36in Pencil 1 Floor Tile Extra Challenge: Have the motion start after pressing the whisker.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.