Professional Documents
Culture Documents
to 180 degrees, one degree at a time. When the motor has rotated 180 degrees, it
will begin to rotate in the other direction until it returns to the home position.
int angle = 0;
void setup()
{
servo_test.attach(9); // attach the signal pin of servo to pin9 of arduino
}
void loop()
{
for(angle = 0; angle < 180; angle += 1) // command to move from 0 degrees to 180 degrees
{
servo_test.write(angle); //command to rotate the servo to the specified angle
delay(15);
}
delay(1000);
for(angle = 180; angle>=1; angle-=5) // command to move from 180 degrees to 0 degrees
{
servo_test.write(angle); //command to rotate the servo to the specified angle
delay(5);
}
delay(1000);
}
Once the program is started, rotating the potentiometer should cause the shaft of
the servo motor to rotate.
int angle = 0;
void setup()
void loop()
angle = map(angle, 0, 1023, 0, 179); // scaling the potentiometer value to angle value for servo between 0 and
180)
delay(5);
}
Target Problem:
Move the servo cap to the left when one button is pushed and to the right when the other button is pushed.
Double-check the wiring connections before connecting the microcontroller to the computer unit . Observe the
rotation of the servo cap to see if it moves to the angle desired.
volatile int i=0;//initializing a integer for incrementing and decrementing duty ratio.
void setup()
}
void loop()
if (digitalRead(0)==LOW)
{
if (i<180)
{
}
}
if (digitalRead(1)==LOW)
{
if (i>0)
{
delay(30);
}
}
}
#include <Servo.h>;
// pushbutton pin
// servo pin
Servo servo;
int counter = 0;
void setup()
{
servo.attach (servoPin);
pinMode(buttonPin, INPUT);
void loop()
int buttonState;
buttonState = digitalRead(buttonPin);
counter++;
delay(150);
if(counter == 0)
servo.write (0);
}
else if(counter == 1 )
servo.write (180);
else
counter = 0;
}
//include the servo library
#include
// map the light readings to the angle possible by the servo motor
lightValue = map (lightValue, 0, 1023, 0, 180);
// control the servo motor based on the light value read, adjust
linearly by angles servo1.write (lightValue); }
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <Servo.h>
Servo myservo;
lcd.setCursor(0, 1);
lcd.print(pos);
lcd.print(" ");
void setup(){
myservo.attach(10);
lcd.begin(16, 2);
lcd.print("SERVO ANGLE");
lcd.setCursor(0, 1);
}
void loop(){
int pos;
myservo.write(pos);
write_angle_lcd(pos);
delay(50);
myservo.write(pos);
write_angle_lcd(pos);
delay(50);