Servo Library and Functions

Slides:



Advertisements
Similar presentations
temperature system wiring
Advertisements

Using the servo library living with the lab Libraries are computer programs written to add functionality to Arduino programs. A library to control hobby.
Analog and Digital Measurements living with the lab 14 digital input / output pins 6 analog input pins © 2012 David Hall.
Using the Arduino to Make an LED Flash Work in teams of two! living with the lab digital I/O pins (I/O = input / output) USB cable plug power pins.
User-defined functions in Arduino sketches living with the lab © 2012 David Hall.
Cascaded switching of a solenoid valve living with the lab transistor relay solenoid valve © 2012 David Hall.
Assembly of conductivity flow loop living with the lab (in preparation for calibrating conductivity sensor)
Switches & whiskers on the Arduino living with the lab lever arm switches mounted to Arduino © 2012 David Hall.
Using Hobby Servos with the Arduino living with the lab © 2012 David Hall.
Using for loops to control LEDs living with the lab 1 1 arduino.cc the for statement allows us to repeat a block of commands a limited number of times.
Building Circuits.
Measuring & Drawing a Barbed Fitting
Pump Fabrication Day Group A will draw their pump
Controlling Servos with the Arduino
Connecting Switches.
Series and Parallel Resistors
Pump Project Requirements
Pump Efficiency Fluid Energy Out + - Electrical Energy In.
Introduction to the Arduino
Why Won’t My Arduino Work?
Robot Challenge Introduction
Troubleshooting Your Multimeter
Using servos.
Introduction to SolidWorks
Servo Library and Functions
Line Following Tips photoresistor circuits
What’s in your kit?.
Pump Project Overview.
Robot Assembly.
How to Use Dial Calipers
Controlling a Motor with Cascading Switches
Introduction to Transistors
Introduction to the Fishtank
RGB LEDs.
Conservation of Mass Problem
Conductivity Sensor.
Schedule 8:00-11:00 Workshop: Arduino Fundamentals
Introduction to Transistors
Troubleshooting Your Multimeter
Relays.
using for loops to control LEDs
using the Arduino to make LEDs flash
Acquiring Data from an ADXL335 Accelerometer
Using Photoresistors with an Arduino
Introduction to Mathcad
Line Following Tips photoresistor circuit
Torque and RPM of Gears
Conservation of Mass Problem
analog and digital measurements
Finishing your Project
Using “if” statements.
Controlling the Heater
Design Project Forecast
Digital Input from Switches
Measuring & Drawing a Barbed Fitting
Cash Flow Diagrams <in> <out> $200 $300 $150 $100
Implementing Switches Using Interrupts
Arduino: For Loops.
Non-Concurrent Force Systems
IR Object Detection IR detector IR LED IR light reflected off object
Radio Frequency Transmitter and Receiver
Interfacing a Rotary Encoder with an Arduino
Conservation of Mass Problem
Non-Concurrent Force Systems
Evaluating Design Alternatives
Counting Servo Gear Teeth (FS90R Servos)
Static Equilibrium Problem
Reservoir Loop.
Freshman Design Expo Presentations
Gearmotor Efficiency W table top gearmotor pulley string.
Presentation transcript:

Servo Library and Functions #include <Servo.h> Servo myservo1; void setup() { myservo1.attach(2); myservo1.writeMicroseconds(1000); } void loop() {

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

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); }

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.

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.

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.

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.