Hola, a ver si me podeis hechar una mano que acabo de incorcoporarme a este mundillo del arduino y tengo un problemilla para girar un servo y esque no me gira mas de 180º por mucho que le pase a la funcion un numero mayor.
#include <Servo.h>
Servo myservo; // crea el objeto para controlarlo
const int buttonPin = 2;
int potpin = 0; // pin analogico q usa el potenciometro
int val; //varaible para leer el valor del pin analogico
int buttonState = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
myservo.attach(9); //asignas el pin 9 al objeto servo
}
void loop() {
//val =analogRead(potpin); //lee el valor del potenciometro
//int val = myservo.read();
//val =map (val,0,1023,0,179); // escala el valor para usarlo con el servo
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
moveTo(190);
delay(15);
}
else{
//currentPos=currentPos + 20;
moveTo(20);
delay(95);
}
}
void moveTo(int newPosition){
// degreesPerStep of 4 moves 160 degrees in a little under a second
// degreesPerStep of 1 moves 160 degrees in around 3.2 seconds
int degreesPerStep = 4; // decrease this to slow movement
int currentPos = myservo.read();
Serial.print (currentPos);
Serial.println();
int movement = newPosition - currentPos; // the number of degrees to move
if(movement < 0){
while(currentPos > newPosition){
currentPos = currentPos - degreesPerStep;
if(currentPos < newPosition)
currentPos = newPosition;
myservo.write( currentPos);
delay(20);
}
}
else{ // movement is >0 )
while(currentPos < newPosition){
currentPos = currentPos + degreesPerStep;
if(currentPos > newPosition)
currentPos = newPosition;
myservo.write( currentPos);
delay(20);
}
}
}
A ver si m podeis echar una mano. Gracias de antemano.