Skip to content

Instantly share code, notes, and snippets.

@chepecarlos
Forked from ale140/3servos.ino
Last active May 3, 2017 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chepecarlos/d663c413f1c254f6d64e7c8a3e285a7f to your computer and use it in GitHub Desktop.
Save chepecarlos/d663c413f1c254f6d64e7c8a3e285a7f to your computer and use it in GitHub Desktop.
control de 3 servos con pulsadores, el tercero debe de girar sólo mientras se active el pulsador y cambiar de sentido cuando se presione el otro
#include <Servo.h>
Servo myservo[3];//Objeto se los servos
int PosServo[3] = {85, 85, 85};//Posicopn acutal de los servos
const int Grados[2] = {5, 175}; //Rango maximo de los servos
int Movimiento = 1; //Cuanto se mueve el servo a la vez
const int PinBoton[3][2] = {{2, 3}, {4, 5}, {6, 7}};//Botones para controlor los sevos
int EstadoBoton[3][2] = {{0, 0}, {0, 0}, {0, 0}};
const int PinServo[3] = {8, 9, 10};//Pin de los servos
void setup()
{
for (int i = 0; i < 3; i++) {
myservo[i].attach(PinServo[i]);
pinMode(PinBoton[i][0], INPUT);
pinMode(PinBoton[i][1], INPUT);
}
}
void loop()
{
ActualizarEntradas();
CalcularMovimiento();
MoverServo();
delay(100);
}
void CalcularMovimiento() {
for (int i = 0; i < 3; i++) {
if (!(EstadoBoton[i][0] && EstadoBoton[i][1])) {
if (EstadoBoton[i][0]) {
if (PosServo[i] < Grados[1]) {
PosServo[i] += Movimiento;
}
else {
if (PosServo[i] > Grados[0]) {
PosServo[i] -= Movimiento;
}
}
}
}
}
}
void MoverServo() {
for (int i = 0; i < 3 ; i++) {
myservo[i].write(PosServo[i]);
}
}
void ActualizarEntradas() {
for (int i = 0; i < 3 ; i++) {
for (int j = 0; i < 2; j++) {
EstadoBoton[i][j] = digitalRead(PinBoton[i][j]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment