InteractiveBody Workshop_002_03=Arduino_Servo

Page 1

ARDUINO (servo)


What is Servo?


Servo Motor


PWM Pulse Width Modulation


BLACK

BROWN

-

RED

RED

+

WHITE

Yellow

Signal


Let your Servo MOVE?


PWM PIN: 3 5 6 9 10 11 But if you import the Servo Library in Arduino: You will have 8 servo can be created

PWM


Upload the code: File > Examples > Servo> Sweep. #include <Servo.h> Set up your servo Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object }

void loop() Position { for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }


Rotate between 0-180


2 types of servo

1 2

Position

0-180 : the position of the rotation

0-90 : contour-clock wise, speed-up once it closer to 0

Continuous

90-180 : clock wise, speed up once it gets to 180

90 : no movement


How about? potentiometer + Servo Motor



Upload the code: File > Examples > Servo> Knob. #include <Servo.h> Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int inPin = 0; int pos = 0; // variable to store the servo position int newPos; //position for remap void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object }

void loop() { pos = analogRead(inPin); newPos = map(pos,0,1023,0,179); myservo.write(newPos); delay(15); }

Read the data from “A0� Remap the data from 0-180 Output data


Multiple Servo Motor


#include <Servo.h> Servo myservo01; Servo myservo02; int inPin1 = 0; int inPin2 = 1; int pos01 = 0; int pos02 = 0; // variable to store the servo position

int newPos01; int newPos02; void setup() { myservo01.attach(9); myservo02.attach(10); } void loop() { zero(); delay(1000); writeData(); delay(1000); }

void zero(){ myservo01.write(0); myservo02.write(0); } void readData(){ pos01 = analogRead(inPin1); pos02 = analogRead(inPin2); newPos01 = map(pos01,0,1023,0,179); newPos02 = map(pos02,0,1023,0,179); } void writeData(){ myservo01.write(30); myservo02.write(120); }

Functions


#include <Servo.h> Servo myservo01; Servo myservo02; int inPin1 = 0; int inPin2 = 1; int pos01 = 0; int pos02 = 0; // variable to store the servo position int newPos01; int newPos02; void setup() { myservo01.attach(9); myservo02.attach(10); } void loop() { //pos01 = analogRead(inPin1); //pos02 = analogRead(inPin2); //newPos01 = map(pos01,0,1023,0,179); //newPos02 = map(pos02,0,1023,0,179); readData(); writeData(); delay(15); }

void zero(){ myservo01.write(0); myservo02.write(0); } void readData(){ pos01 = analogRead(inPin1); pos02 = analogRead(inPin2); newPos01 = map(pos01,0,1023,0,179); newPos02 = map(pos02,0,1023,0,179); } void writeData(){ myservo01.write(newPos01); myservo02.write(newPos02; }

Functions


It’s your turn? LDRs + Servo Motors


Some Examples I did


https://vimeo.com/13977001



https://vimeo.com/13977001



https://vimeo.com/13977213 https://vimeo.com/13978876


https://vimeo.com/13976901









https://vimeo.com/13977063


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.