PRÁCTICA Nº4: VAMOS A SIMULAR EL FUNCIONAMIENTO DE UNA BARRERA DE GARAJE UTILIZANDO UN SERVOMOTR Y TRES LUCES (VERDE, AMARILLA Y ROJA) VAMOS A HACER LA SIGUIENTE SECUENCIA: - EL SERVOMOTOR EMPIEZA EN CERO GRADOS Y MIENTRAS ESTÁ AHÍ, LA LUZ ROJA ESTÁ ENCENDIDA. - EL SERVOMOTOR EMPIEZA A MOVERSE LENTAMENTE DESDE LOS CERO GRADOS HASTA LOS 90º Y LA LUZ AMARILLA ESTÁ ENCENDIDA MIENTRAS REALIZA DICHA ACCIÓN, APAGÁNDOSE LA LUZ ROJA - EL SERVOMOTOR SE QUEDA PARADO EN 90º Y SE ENCIENDE LA LUZ VERDE INDICANDO QUE SE PUEDE PASAR DURANTE UN TIEMPO PROGRAMADO. SE APAGA LA LUZ AMARILLA - EL SERVOMOTOR EMPIEZA A MOVERSE DESDE LOS 90º A LOS 0 GRADOS LENTAMENTE Y MIENTRAS HACE ESO LA LUZ AMARILLA SE ENCIENDE Y LA VERDE SE APAGA - CUANDO LLEGA A CERO GRADOS, EL LED AMARILLO SE APAGA Y SE ENCIENDE EL ROJO, INICIÁNDOSE DE NUEVO EL PROCESO

#include #define LUZVERDE 2 #define LUZAMARILLO 3 #define LUZROJO 4 #define PINSERVO 5 Servo myservo; int posicion=0; void setup(){ pinMode(LUZVERDE,OUTPUT); pinMode(LUZROJO,OUTPUT); pinMode(LUZAMARILLO,OUTPUT); myservo.attach(PINSERVO); } void loop(){ digitalWrite(LUZROJO,HIGH); delay(1000); digitalWrite(LUZROJO,LOW); digitalWrite(LUZAMARILLO,HIGH); delay(1); for(posicion=0;posicion<=90;posicion=posicion+1){ myservo.write(posicion); delay(30); } digitalWrite(LUZAMARILLO,LOW); digitalWrite(LUZVERDE,HIGH); delay(2000); digitalWrite(LUZVERDE,LOW); digitalWrite(LUZAMARILLO,HIGH); for(posicion=90;posicion>=0;posicion=posicion-1){ myservo.write(posicion); delay(30); } digitalWrite(LUZAMARILLO,LOW); }

#include<Servo.h>//incluimos el servomotor a la galeria de arduino
#define LUZVERDE 2// definimos el led y lo pinchamos en el pin nº2
#define LUZAMARILLO 3// definimos el led y lo pinchamos en el pin nº3
#define LUZROJO 4// definimos el led y lo pinchamos en el pin nº4
#define PINSERVO 5// definimos el servo y lo pinchamos en el pin nº5

Servo myservo;
int posicion=0; // lo ponemos en la posicion 0



void setup(){

pinMode(LUZVERDE,OUTPUT);//ponemos el led como salida
  pinMode(LUZROJO,OUTPUT);//ponemos el led como salida
  pinMode(LUZAMARILLO,OUTPUT);//ponemos el led como salida
 myservo.attach(PINSERVO);//ponemos servo como salida
}
 
 
void loop(){
  digitalWrite(LUZROJO,HIGH);//encendemos el led rojo
  delay(1000);//lo mantenemos 1 segundo
  digitalWrite(LUZROJO,LOW);//apagamos el led rojo
  digitalWrite(LUZAMARILLO,HIGH);//encendemos el led amarillo
  delay(1);//lo mantenemos 1 milisegundo
  for(posicion=0;posicion<=90;posicion=posicion+1){//lo mandamos desde la posicion 0 a la 90
    myservo.write(posicion);
    delay(30);//lo mantenemos 1 segundo
  }
  digitalWrite(LUZAMARILLO,LOW);//apagamos el led amarillo
  digitalWrite(LUZVERDE,HIGH);//encendemos el led verde
  delay(2000);//lo mantenemos 2 segundo
  digitalWrite(LUZVERDE,LOW);//apagamos el led verde
  digitalWrite(LUZAMARILLO,HIGH);//encendemos el led amarillo
  for(posicion=90;posicion>=0;posicion=posicion-1){//lo mandamos desde la posicion 90 a la 0
    myservo.write(posicion);
    delay(30);//lo mantenemos 30 milisegundo
   
  }
  digitalWrite(LUZAMARILLO,LOW);//apagamos el led amarillo
}



Comentarios