hola de nuevo foro! espero que esteis pasando buen veranito. Tengo unas dudas acerca del codigo a emplear para un servo y salidas a rele.
Me explico un poco mejor. Tengo que hacer un inventillo...el servo actua cuando se mantiene activado un pulsador (pulsador1) y cuando se activa el pulsador2 va hacia el lado opuesto. Hasta aqui todo bien, la simulacion en Proteus y en arduino va genial..mi problema llega cuando quiero activar una salida a un rele...cuando cargo el arduino con el codigo para activar el servo y el rele, solo funciona el rele...el servo se queda quieto....necesito que cuando pulse los botones, el servo se mueva, independientemente de si esta a 1 o a 0 la salida al rele. Os dejo el codigo (use la libreria de servo, y tambien modifique la de blink):
FUNCIONA BIEN (aun no se ha añadido el rele)
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 90; // variable to store the servo position
const int maxDeg = 160;
const int minDeg = 5;
const int leftPin = 3;
const int rightPin = 2;
const int led1Pin = 6; // indicator
const int led2Pin = 5; // indicator
const int outputPin = 9; // pwm function will be disabled on pin 9 and 10 if using servo
int leftPressed = 0;
int rightPressed = 0;
void setup()
{
myservo.attach(outputPin); // attaches the servo on pin 9 to the servo object
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
}
void loop()
{
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);
if(leftPressed){
if(pos < maxDeg) pos += 3;
myservo.write(pos); // tell servo to go to position in variable ‘pos’
digitalWrite(led1Pin,HIGH);
}
else
digitalWrite(led1Pin,LOW);
if(rightPressed){
if(pos > minDeg) pos -= 3;
myservo.write(pos); // tell servo to go to position in variable ‘pos’
digitalWrite(led2Pin,HIGH);
}
else
digitalWrite(led2Pin,LOW);
delay(15); // waits 15ms for the servo to reach the position
}
SOLO FUNCIONA EL RELE (EL SERVO NO SE MUEVE AUNQUE PULSES)
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 90; // variable to store the servo position
const int maxDeg = 160;
const int minDeg = 5;
const int leftPin = 3;
const int rightPin = 2;
const int led1Pin = 6; // indicator
const int led2Pin = 5; // indicator
const int outputPin = 9; // pwm function will be disabled on pin 9 and 10 if using servo
int leftPressed = 0;
int rightPressed = 0;
int rele = 13;
void setup()
{
myservo.attach(outputPin); // attaches the servo on pin 9 to the servo object
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(rele, OUTPUT);
}
void loop()
{
digitalWrite(rele, HIGH); // turn the LED on (HIGH is the voltage level)
delay(4000); // wait for a second
digitalWrite(rele, LOW); // turn the LED off by making the voltage LOW
delay(1000);
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);
if(leftPressed){
if(pos < maxDeg) pos += 3;
myservo.write(pos); // tell servo to go to position in variable ‘pos’
digitalWrite(led1Pin,HIGH);
}
else
digitalWrite(led1Pin,LOW);
if(rightPressed){
if(pos > minDeg) pos -= 3;
myservo.write(pos); // tell servo to go to position in variable ‘pos’
digitalWrite(led2Pin,HIGH);
}
else
digitalWrite(led2Pin,LOW);
delay(15); // waits 15ms for the servo to reach the position
}
perdon por el ladrillo, espero que me ayudeis, es urgente.
Muchas gracias