/**********************************************************************************
* Tarea 3.1. Control de un relé con un pulsador
* Autor: José Miguel Martín Hernández
**********************************************************************************/
int boton = 14; // Pin del botón
int rele = 16; // Pin salida relé
int ldr = 2; // Pin entrada LDR
int estadoBoton = 0;
int estadoRele = 0;
int estadoLdr = 0;
int apagadoBoton=0; // variable para apagar por boton cuando enciende por ldr
int funcionLdr=0;
// variable que apaga cuando la LDR enciende
void setup() {
pinMode(boton, INPUT_PULLDOWN);
pinMode(rele, OUTPUT);
pinMode(ldr, INPUT);
Serial.begin(9600);
}
void loop() {
estadoBoton = digitalRead(boton);
estadoRele= digitalRead(rele);
estadoLdr= analogRead(ldr);
if ((estadoLdr > 3400)&(estadoBoton == HIGH)) {apagadoBoton=1;};
if (estadoLdr < 3400) {apagadoBoton=0;};
if (estadoLdr>3400) {funcionLdr=1;};
if ((estadoLdr < 3400)&(estadoBoton == HIGH)&(estadoRele == HIGH)) {digitalWrite(rele,LOW);funcionLdr=0;goto MARCA1;};
if ((estadoLdr < 3400)&(estadoBoton == HIGH)&(estadoRele == LOW)) {digitalWrite(rele,HIGH);funcionLdr=0;goto MARCA1;};
if ((estadoLdr > 3400)&(estadoBoton == HIGH)) {digitalWrite(rele,LOW);apagadoBoton=1;goto MARCA1;};
if ((estadoLdr < 3400)&(funcionLdr==1)) {digitalWrite(rele,LOW);goto MARCA1;};
if ((estadoLdr > 3400)&(apagadoBoton==0)) {digitalWrite(rele,HIGH);funcionLdr=0;goto MARCA1;};
MARCA1:;
delay(500);
Serial.print("apagado boton");
Serial.println(funcionLdr);
}