Skip to content

Instantly share code, notes, and snippets.

@jmtexla68
Last active June 3, 2016 20:40
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 jmtexla68/585ed9f9e728a96185a496383ee91c1b to your computer and use it in GitHub Desktop.
Save jmtexla68/585ed9f9e728a96185a496383ee91c1b to your computer and use it in GitHub Desktop.
En este ejemplo vamos a controlar la posición del motor desde el Monitor Serial. Le mandaremos la letra 's' para aumentar 10º o la letra 'b' cuando para decrementar 10º. Además con la función constrain nos aseguramos de que el valor no supere los 180 ni baje de los 0.
/************************************************
Tutorial Arduino Servo
https://conbotassucias.wordpress.com/
************************************************/
#include <Servo.h>
Servo miServo;
int angulo=90;
void setup(){
miServo.attach(9);
Serial.begin(9600);
}
void loop(){
unsigned char comando=0;
if(Serial.available()){//solo leeremos si hay un byte en el buffer
comando=Serial.read();//leemos el byte
if(comando=='s')angulo+=10;//incrementamos 10
else if(comando=='b')angulo-=10;//decrementamos 10
angulo=constrain(angulo,0,180);//restringimos el valor de 0 a 180
}
miServo.write(angulo);
Serial.print("Angulo:");Serial.println(angulo);
delay(100);
}//Fin de ciclo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment