Тема: SERVO MOTOR SG90
Основная статья - http://rcl-radio.ru/?p=132542
int servoPin = 9; // сигнальный провод от серво на порт 9
int val;
void setup()
{
pinMode(servoPin, OUTPUT);
Serial.begin(9600);
Serial.println("Servo is ready");
}
void loop(){
val = 0;
for (int i = 0; i <= 50; i++){servoPulse(servoPin, val);}
delay(1000);
val = 90;
for (int i = 0; i <= 50; i++){servoPulse(servoPin, val);}
delay(1000);
val = 180;
for (int i = 0; i <= 50; i++){servoPulse(servoPin, val);}
delay(1000);
val = 90;
for (int i = 0; i <= 50; i++){servoPulse(servoPin, val);}
delay(1000);
}
// define a servo pulse function
void servoPulse(int pin, int angle){
int pulseWidth = map(angle, 0, 180 , 544, 2480);
Serial.println(pulseWidth);
digitalWrite(pin, HIGH); // set the level of servo pin as high
delayMicroseconds(pulseWidth); // delay microsecond of pulse width
digitalWrite(pin, LOW); // set the level of servo pin as low
delayMicroseconds(20000 - pulseWidth);
}
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(90);
delay(1000);
}