#include <Servo.h>
Servo Myservo; int
pos;
void setup() {
Myservo.attach(3);
Serial.begin(9600);
}
void loop() { for (pos = 0; pos <= 180;
pos++) { Myservo.write(pos);
delay(5);
}
delay(1);
for (pos = 180; pos >= 0; pos--) {
Myservo.write(pos); delay(5);
}
delay(1);
}
To rotate the servo motor from 0° to 180° and in reverse in step of 30°
#include <Servo.h> Servo
Myservo;
int pos;
void setup() {
Myservo.attach(3);
Serial.begin(9600);
}
void loop() {
for (pos = 0; pos <= 180; pos += 90) {
Serial.println(pos);
Myservo.write(pos); delay(1000);
}
delay(1000);
for (pos = 180; pos >= 0; pos -= 90) {
Serial.println(pos);
Myservo.write(pos);
delay(1000);
}
delay(1000);
}
//To rotate the servo motor from 0° to 180° and in reverse with increasing speed from 0°
//to 60° , constant speed between 61° to 120°, decreasing speed from 121° to 180° and
//constant speed from 180° to 0°
#include <Servo.h>
Servo Myservo; int
pos; int d = 310;
void setup() {
Myservo.attach(3);
Serial.begin(9600);
}
void loop() { d = 310; for (int pos = 0; pos < 60;
pos += 1, d -= 5) {
Myservo.write(pos);
Serial.print(pos);
Serial.print(": ");
Serial.println(d); delay(d);
}
for (int pos = 60; pos < 120; pos += 1) {
Myservo.write(pos);
Serial.print(pos);
Serial.print(": ");
Serial.println(d); delay(d);
}
for (int pos = 120; pos < 180; pos += 1, d += 5) {
Myservo.write(pos);
Serial.print(pos);
Serial.print(": ");
Serial.println(d); delay(d);
}
d = d / 10; for (int pos = 180; pos > 0; pos -
= 1) {
Myservo.write(pos);
Serial.print(pos);
Serial.print(": ");
Serial.println(d); delay(d);
}
}
//Using servo motors to implement sorting technology. Take shift degree = 45 and total 5
//different sections
#include <Servo.h>
Servo Myservo; int
pos;
int green = 0; int
red = 0; int yellow
= 0; int pink = 0;
int white = 0; int
current = 0; int
shift_by = 45;
void setup() {
Myservo.attach(3);
Serial.begin(9600);
Serial.println("G R Y P W");
}
void loop() { current =
random(0, 5);
Myservo.write(shift_by * current); if
(current == 0) { green += 1;
Serial.print("green "); }
else if (current == 1) { red
+= 1;
Serial.print("red "); }
else if (current == 2) {
yellow += 1;
Serial.print("yellow "); }
else if (current == 3) { pink
+= 1;
Serial.print("pink "); }
else if (current == 4) {
white += 1;
Serial.print("white ");
}
Serial.print(" ");
if (green < 10) Serial.print(" ");
Serial.print(green); Serial.write(" ");
if (red < 10) Serial.print(" ");
Serial.print(red); Serial.write(" ");
if (yellow < 10) Serial.print(" ");
Serial.print(yellow); Serial.write(" ");
if (pink < 10) Serial.print(" ");
Serial.print(pink); Serial.write(" ");
if (white < 10) Serial.print(" ");
Serial.print(white);
Serial.write(" "); Serial.println();
delay(1000);
}
//To rotate the servo motor as per user input and relative to its previous position
#include <Servo.h>
Servo Myservo; int
pos;
int curr_pos = 0;
void setup() {
Myservo.attach(3);
Serial.begin(9600);
}
void loop() { while
(Serial.available()) { int val =
Serial.parseInt(); curr_pos +=
val; curr_pos %= 180;
Serial.println(curr_pos);
Myservo.write(curr_pos);
}
}