06-06-2016, 17:16
Este lo copié de un foro ,,,pero le cambiado velocidad y angulo ¡¡¡ funciona bien pero al desconectar y conectar de nuevo no guada la posición ...hace un raro al posicionarse ,,,NO me sirve me romperá el mecanismo ¡¡¡¡
#include <Servo.h>
const int servoPin = 9;
const int buttonPin = 2;
int buttonState = 0;
int directionState = 0;
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(9);
pinMode(buttonPin, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (directionState == 0){
if (buttonState == HIGH) {
directionState = 1;
for(pos = 15; pos < 40; pos=pos+1)
{
myservo.write(pos);
delay(35);
}
}
} else if (directionState == 1) {
if (buttonState == HIGH) {
directionState = 0;
for(pos = 40; pos>=15; pos=pos-1)
{
myservo.write(pos);
delay(35);
}
}
}
}
#include <Servo.h>
const int servoPin = 9;
const int buttonPin = 2;
int buttonState = 0;
int directionState = 0;
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(9);
pinMode(buttonPin, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (directionState == 0){
if (buttonState == HIGH) {
directionState = 1;
for(pos = 15; pos < 40; pos=pos+1)
{
myservo.write(pos);
delay(35);
}
}
} else if (directionState == 1) {
if (buttonState == HIGH) {
directionState = 0;
for(pos = 40; pos>=15; pos=pos-1)
{
myservo.write(pos);
delay(35);
}
}
}
}