SERVO MOTOR

servo motor

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/SERVO%20MOTOR.zip

#include <Servo.h>

Servo head;
void setup()
{
head.attach(9);
head.write(80);

}
void loop(){
head.write(180);
delay(1000);
head.write(160);
delay(1000);
head.write(140);
delay(1000);
head.write(120);
delay(1000);
head.write(100);
delay(1000);
head.write(80);
delay(1000);
head.write(60);
delay(1000);
head.write(40);
delay(1000);
head.write(20);
delay(1000);
head.write(0);
delay(1000);

head.write(20);
delay(1000);
head.write(40);
delay(1000);
head.write(60);
delay(1000);
head.write(80);
delay(1000);
head.write(100);
delay(1000);
head.write(120);
delay(1000);
head.write(140);
delay(1000);
head.write(160);
delay(1000);

}

OTHER SKET

#include <Servo.h>
Servo leftRightServo; // set a variable to map the servo
int leftRightPos = 0; // set a variable to store the servo position
const int numReadings = 3; // set a variable for the number of readings to take
int index = 0; // the index of the current reading
int total = 0; // the total of all readings

/* setup the pins, servo and serial port */
void setup() {
leftRightServo.attach(9);

Serial.begin(9600);
}

/* begin rotating the servo and getting sensor values */
void loop() {
for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) { // going left to right.
leftRightServo.write(leftRightPos);
for (index = 0; index<=numReadings;index++) { // take x number of readings from the sensor and average them

delay(3);
}

Serial.print(“X”); // print leading X to mark the following value as degrees
Serial.println(leftRightPos); // current servo position

}

for(leftRightPos = 180; leftRightPos > 0; leftRightPos–) { // going right to left
leftRightServo.write(leftRightPos);
for (index = 0; index<=numReadings;index++) {

delay(3);
}

Serial.print(“X”);
Serial.println(leftRightPos);

}
}

OTHER SKET

#include <Servo.h>
Servo leftRightServo; // set a variable to map the servo
int leftRightPos = 0; // set a variable to store the servo position
void setup() {
leftRightServo.attach(9);
Serial.begin(9600);
}
void loop() {
for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) { // going left to right.
leftRightServo.write(leftRightPos);
Serial.print(“X”); // print leading X to mark the following value as degrees
Serial.println(leftRightPos); // current servo position
delay(50);
}
for(leftRightPos = 180; leftRightPos > 0; leftRightPos–) { // going right to left
leftRightServo.write(leftRightPos);
Serial.print(“X”);
Serial.println(leftRightPos);
delay(50);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s