Hola a todos hace tiempo ando buscando como hacer esto y no he encontrado nada que funcione, realmente no se mucho de esto, pero necesito hacerlo para controlar el zoom de un lente de camara, usando un joystick para hacer girar un servo motor hacia un lado y otro y que la velocidad del motor aumente mientras presiono mas el joystick, esto es para que el zoom sea rapido o lento segun la presion en el joystick, no se si alguien podria ayudarme, estoy probando con un arduino uno, joystick y motor MG996R , tiene que hacer lo que se ve en este video https://youtu.be/jjAXLSxl2Vo?si=fIYt2iKRMWLEy0-4
Eso es un servo 360. Aquí se simula el 360 usando un servo de 180. Las etiquetas dicen cómo actuará un 360.
sketch.ino
/*
Continuous servo.
myservo.write(0); // full-speed counter-clockwise
myservo.write(90); // stop - approximate, adjust for servo
myservo.write(180); // full-speed clockwise
*/
#include <Servo.h> // servo library
Servo myservo; // create servo object
byte servoPin = 9; // servo signal pin
byte maxCCW = 0, stop = 90, maxCW = 180; // angle definitions
int wait = 20;
#define horz A0
#define vert A1
#define sel 2
void setup() {
Serial.begin(115200); // start serial communication
myservo.attach(servoPin); // attach servo to object
myservo.write(stop); // stop the motor
}
void loop() {
joysticksweep();
// autosweep();
}
void joysticksweep() {
int val = map(analogRead(horz), 0, 1023, 180, 0);
myservo.write(val);
}
void autosweep() {
sweep(stop, maxCW); // increase CW speed from stop to max
sweep(maxCW, stop); // decrease CW speed from max to stop
sweep(stop, maxCCW); // increase CCW speed from stop to max
sweep(maxCCW, stop); // decrease CCW speed from max to stop
}
void sweep(int a, int b) { // get start and end angle values
if (a < b) {
for (int i = a; i < b; i++) {
myservo.write(i);
delay(wait);
}
} else {
for (int i = a; i > b; i--) {
myservo.write(i);
delay(wait);
}
}
}
diagram.json for wokwi
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
{
"type": "wokwi-servo",
"id": "servo1",
"top": -164.2,
"left": 11.4,
"rotate": 270,
"attrs": {}
},
{
"type": "wokwi-text",
"id": "legendservo1",
"top": -124.8,
"left": 163.2,
"attrs": { "text": "CW max speed" }
},
{
"type": "wokwi-text",
"id": "legendservo2",
"top": -124.8,
"left": -86.4,
"attrs": { "text": "CCW max speed" }
},
{
"type": "wokwi-text",
"id": "legendservo3",
"top": -230.4,
"left": 57.6,
"attrs": { "text": "360 servo" }
},
{
"type": "wokwi-text",
"id": "legendservo4",
"top": -211.2,
"left": 76.8,
"attrs": { "text": "stop" }
},
{
"type": "wokwi-text",
"id": "legendservo5",
"top": -144,
"left": -67.2,
"attrs": { "text": "360 servo" }
},
{
"type": "wokwi-text",
"id": "legendservo6",
"top": -144,
"left": 182.4,
"attrs": { "text": "360 servo" }
},
{
"type": "wokwi-analog-joystick",
"id": "joystick1",
"top": -58.2,
"left": 178.2,
"attrs": {}
}
],
"connections": [
[ "servo1:GND", "nano:GND.3", "black", [ "v0" ] ],
[ "nano:5V.2", "servo1:V+", "red", [ "h-57.6", "v-57.6" ] ],
[ "nano:9", "servo1:PWM", "green", [ "v-9.6", "h57.6" ] ],
[ "joystick1:VCC", "nano:5V", "red", [ "v9.6", "h-76.8" ] ],
[ "joystick1:VERT", "nano:A1", "green", [ "v19.2", "h-115.2" ] ],
[ "joystick1:HORZ", "nano:A0", "green", [ "v28.8", "h-182.4" ] ],
[ "joystick1:GND", "nano:GND.1", "black", [ "v38.4", "h-115.2" ] ],
[ "joystick1:SEL", "nano:2", "green", [ "v48", "h-67.2" ] ]
],
"dependencies": {}
}
Muchas gracias por su ayuda voy a comprar un nano y lo probare, y subire el resultado
Para que vas a comprar un NANO?
El NANO y el UNO son iguales salvo cuando se lo indicas al IDE.
Hazlo con tu UNO sin problemas.
Entiendo, gracias por la aclaracion, ya conecte todo ya carge el archivo y el motor hace exactamente lo que necesito pero solo para un lado, para el otro no lo hace ni siquiera se mueve, el motor que estoy usando es un mg996r
Muchas gracias por su ayuda, ya todo funciona muy bien, solo tengo una pregunta como hago para que la velocidad maxima para ambos setidos del motor sea mas lenta
Para CW, entre 90 (STOP) y 0 (máximo).
Para CCW, entre 90 (STOP) y 180 (máximo)
/*
Continuous servo.
myservo.write(0); // full-speed counter-clockwise
myservo.write(90); // stop - approximate, adjust for servo
myservo.write(180); // full-speed clockwise
*/
Utilice "map()" para limitar la velocidad.
int val = map(analogRead(horz), 0, 1023, MAX_CW, MAX_CCW);
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.