control de servo con joystick
//
//GND - GND
//Vcc - 5v
//VRx - A0
//VRy - A1
//SW - D9
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int val; // variable to read the value from the analog pin
float contro_servo;
float x;
const int pinLED = 13;
const int pinJoyX = A0;
const int pinJoyY = A1;
const int pinJoyButton = 9;
void setup() {
pinMode(pinJoyButton , INPUT_PULLUP); //activar resistencia pull up
pinMode(7,OUTPUT);
myservo.attach(10);
Serial.begin(9600);
}
void loop() {
float Xvalue = 0;
int Yvalue = 0;
bool buttonValue = false;
//leer valores
Xvalue = analogRead(pinJoyX);
delay(100); //es necesaria una pequeña pausa entre lecturas analógicas
Yvalue = analogRead(pinJoyY);
buttonValue = digitalRead(pinJoyButton);
//mostrar valores por serial
Serial.print("X:" );
Serial.print(Xvalue);
Serial.print(" | Y: ");
Serial.print(Yvalue);
Serial.print(" | Pulsador: ");
Serial.println(buttonValue);
delay(1000);
// reads the value of the potentiometer (value between 0 and 1023)
x=(Xvalue*180)/1024;
Serial.print(" | grados: ");
Serial.println(x);
myservo.write(x); // sets the servo position according to the scaled value
delay(15);
}
Comentarios
Publicar un comentario