Sunteți pe pagina 1din 3

SKETCH PARA PARKING DOMÓTICO

1ª FASE: SEMÁFORO (3leds) + 2 BOTONES + 1 SERVO


Zona de declaración de variables globales

const int buttonPinENTRY = 5; // the number of the pushbutton ENTRY pin


const int buttonPinEXIT = 6; // the number of the pushbutton EXIT pin
const int ledPinR = 3; // the number of the RED LED pin
const int ledPinG = 2; // the number of the GREEN LED pin
const int ledPinY = 7; // the number of the GREEN LED pin

const int x = 300; // Inicializamos la constante x como un número entero


int t = 0; // Inicializamos la variable t como un número entero (para usarla más
adelante en mi bucle for)

// variables will change:


int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
int ledStatusRed = HIGH; // variable for reading the pushbutton status
int ledStatusGreen = LOW; // variable for reading the pushbutton status
int ledStatusYellow = LOW; // variable for reading the pushbutton status

#include <Servo.h> // incluye librería servomotor 180º


Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position

Zona de configuración de nuestro montaje


void setup(){

pinMode(ledPinR, OUTPUT); // initialize the LED pin as an output


pinMode(ledPinG, OUTPUT); // initialize the LED pin as an output
pinMode(ledPinY, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPinENTRY, INPUT); // initialize the pushbuttons pins as inputs
pinMode(buttonPinEXIT, INPUT); // initialize the pushbuttons pins as inputs
myservo.attach(4); // attaches the servomotor on pin 4 to the servo object
myservo.write(0); //función para configurar la posición (pos) del servo, “escribiendo” su ángulo,
empezamos en cero grados.
}

Zona de ejecución de instrucciones forever and ever


void loop(){

// read the state of the pushbutton value


buttonState1 = digitalRead(buttonPinENTRY); // check if the pushbutton is pressed.
buttonState2 = digitalRead(buttonPinEXIT); // check if the pushbutton is pressed.
//al pulsar cualquiera de los dos botones sucederá esto [de momento lo configuramos así: con … OR…: ||]
//recuerda que == significa igual a. Mientras que = es asignar a
if ((buttonState1 == HIGH)||(buttonState2 == HIGH)) {

ledStatusRed = LOW; //[fase asignación]


ledStatusGreen = HIGH; //[fase asignación]
digitalWrite(ledPinR, ledStatusRed); // [fase de escritura] Red light turn off.
digitalWrite(ledPinG, ledStatusGreen); // [fase de escritura] Traffic light in green.
//otra manera de programar esto es como ya sabéis:
digitalWrite(ledPinR, LOW); // red light turn off.
digitalWrite(ledPinG, HIGH); // traffic light in green.

myservo.write(90); // BARRERA SUBIDA de golpe a 90º


delay(3000); // Tiempo de espera en semáforo verde

// INICIO dos fases a distintas velocidades de flicking-parpadeo


for (t = 0; t < 6; t = t++) // Primera fase de parpadeo
{
ledStatusGreen = LOW;
digitalWrite(ledPinG, ledStatusGreen);
//como ya bien sabéis sería lo mismo que comandar:
digitalWrite(ledPinG, LOW);
delay (x);
ledStatusGreen = HIGH;
digitalWrite(ledPinG, ledStatusGreen);
//idem que antes
delay (x);
}
ledStatusGreen = LOW;
digitalWrite(ledPinG, ledStatusGreen);

for (pos = 90; pos > 0 ; pos--) // Segunda fase de parpadeo. Va desde 90 grados a 0 grados.
{
if (!(pos%10)) { // Yellow LED toggle (invertir estados) sólo una vez cada 10 ejecuciones
// Invertir (o toggle) el estado del led amarillo sea cual sea a su
llegada, esto es solo a nivel de memoria pero no escribimos nada.
if (ledStatusYellow == HIGH) {
ledStatusYellow = LOW;
}
else {
ledStatusYellow = HIGH;
}
digitalWrite(ledPinY, ledStatusYellow);
// En esta última instrucción escribo el valor en el led del estado anteriormente leído (que lo acabamos de
invertir). Esto ya sabéis que lo podéis programar de la otra manera que es más sencilla…
}
myservo.write(pos); // BARRERA BAJANDO: ordenar al servo posicionarse en el valor de la variable "pos", un
grado menos en cada bucle del for().
delay(15); // Esta instrucción myservo.write(pos); se ejecutará 90 veces.
}
}
// FIN de la segunda fase de parpadeo flicking.

else { // Normal state of the traffic light.


digitalWrite(ledPinR, HIGH); // traffic light in red
digitalWrite(ledPinG, LOW); // green light off
digitalWrite(ledPinY, LOW); // yellow light off
}
}

NOTA:
Para entender el condicional if (!(pos%10)) tenéis que ver el cuadro explicativo en:
www.educarparaelcambio.com > pensamiento computacional > arduino > miniproyecto parking domótico > reto 3: servomotor
de 180º

https://educarparaelcambio.com/reto-3-servomotor-de-180o/

S-ar putea să vă placă și