Hola:
Estoy probando el micro servo que puedes ver aquí sus hoja de datos.
El código siguiente me funciona si pongo los números en el Monitor Serial, de 0 al 180. Este código de abajo me funciona muy bien.
#include <Servo.h>
//Creamos una variable servo para poder usar las funciones con ella.
Servo miservo;
void setup()
{
Serial.begin(115200);
//Definimos el pin al que ira conectado el servo.
miservo.attach(3);
//Movemos el servo al centro
miservo.write(90); // coloca el servo en su posición central
}
void loop() {
long angulo;
if (Serial.available() > 0)
{
angulo=Serial.parseInt();
if (angulo >= 0 && angulo <= 180)
{
miservo.write(angulo);
delay(15);
}
}
}
El problema que tengo, es a la hora de introducir el código similar al de arriba de alguna manera. Se me esfuerza el servo motor a menos de 0º y hace un ruido raro, parece que se va a quemar.
#include <Servo.h>
//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
/*******************************************************
This program will test the LCD panel and the buttons
Mark Bramwell, July 2010
********************************************************/
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int pinLed13 = 13; // Declaramos la variable pin del Led.
int pinLed8 = 8;
int pinLed10 = 10; // Luz de fondo.
char caracter;
String comando;
Servo miservo; //Creamos una variable servo para poder usar las funciones con ella.
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
/*
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
*/
// For V1.0 comment the other threshold and use the one below:
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup()
{
lcd.begin(16, 2); // start the library
lcd.setCursor(0, 0);
//lcd.print("Pulsa botones"); // print a simple message
lcd.print("L8: L10: L13: ");
lcd.setCursor(0, 1);
lcd.print("AtoS 2016");
Serial.begin(115200);
digitalWrite(pinLed10, HIGH);
miservo.attach(6); // Pin 6.
}
void loop()
{
long angulo;
//lcd.setCursor(12, 1); // move cursor to second line "1" and 9 spaces over
//lcd.print(millis() / 1000); // display seconds elapsed since power-up
lcd.setCursor(0, 1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons
pinMode(pinLed13, OUTPUT);
pinMode(pinLed8, OUTPUT);
pinMode(pinLed10, OUTPUT);
/* Voy leyendo carácter a carácter lo que se recibe por el canal
serie (mientras llegue algún dato allí), y los voy concatenando
uno tras otro en una cadena. En la práctica, si usamos el
"Serial monitor" el bucle while acabará cuando pulsemos Enter.
El delay es conveniente para no saturar el canal serie y que la
concatenación se haga de forma ordenada.
*/
while (Serial.available() > 0)
{
caracter = Serial.read();
comando.concat(caracter);
delay(10);
}
/* Unavez ya tengo la cadena "acabada", compruebo su valor y hago
que la placa Arduino reaccione según sea este. Aquí podríamos
hacer lo que quiesiéramos: si el comando es "tal", enciende
un Led, si es cual, mueve un motor... y así.
*/
if (comando.equals("Led_13_ON") == true) // Led_ON.
{
digitalWrite(pinLed13, HIGH); // Enciende el Led.
lcd.setCursor(15, 0);
lcd.print("1");
Serial.println("Led 13 encendido.");
}
if (comando.equals("Led_13_OFF") == true) // Led_OFF.
{
digitalWrite(pinLed13, LOW); // Apaga el Led.
lcd.setCursor(15, 0);
lcd.print("0");
Serial.println("Led 13 apagado.");
}
if (comando.equals("Led_8_ON") == true) // Led_ON.
{
digitalWrite(pinLed8, HIGH); // Enciende el Led.
lcd.setCursor(3, 0);
lcd.print("1");
Serial.println("Led 8 encendido.");
}
if (comando.equals("Led_8_OFF") == true) // Led_OFF.
{
digitalWrite(pinLed8, LOW); // Apaga el Led.
lcd.setCursor(3, 0);
lcd.print("0");
Serial.println("Led 8 apagado.");
}
if (comando.equals("Led_10_ON") == true) // Led_ON.
{
digitalWrite(pinLed10, HIGH); // Enciende el Led.
lcd.setCursor(9, 0);
lcd.print("1");
Serial.println("Led 10 encendido.");
}
if (comando.equals("Led_10_OFF") == true) // Led_OFF.
{
digitalWrite(pinLed10, LOW); // Apaga el Led.
lcd.setCursor(9, 0);
lcd.print("0");
Serial.println("Led 10 apagado.");
}
if (Serial.available() > 0)
{
angulo = Serial.parseInt();
if ((angulo >= 0) && (angulo <= 180))
{
miservo.write(angulo);
delay(15);
}
}
// Limpiamos la cadena para volver a recibir el siguiente comando.
comando = "";
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.print("Derecha ");
break;
}
case btnLEFT:
{
lcd.print("Izquierda");
break;
}
case btnUP:
{
lcd.print("Arriba ");
break;
}
case btnDOWN:
{
lcd.print("Abajo ");
break;
}
case btnSELECT:
{
lcd.print("SELECT ");
break;
}
case btnNONE:
{
//lcd.print("Nada ");
break;
}
}
}
Del primer código he cogido este otro pequeño indicado abajo y se lo puse, pero se me esfuerza el motor y no responde.
if (Serial.available() > 0)
{
angulo = Serial.parseInt();
if ((angulo >= 0) && (angulo <= 180))
{
miservo.write(angulo);
delay(15);
}
}
¿Alguna idea?
Llevo así semanas y esto pude conmigo.
Saludos.