#include <RTClib.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
//#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32

float t;
float temp;
#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define A 2
#define B 3
#define SW 4
volatile int posicion = 12;
int anterior = 0;
unsigned long ultimotiempo3 = 0;
unsigned long ultimotiempo4 = 0;
unsigned long ultimotiempo = 0;
unsigned long ultimotiempo2 = 0;
int menu = 0;
int conteo = 0;
int enc = 0;

int minutoset = 0;
int horaset = 12;
int minutoset2 = 0;
int segundoset = 12;
int hora;
int minuto ;
int segundo ;

RTC_DS3231  rtc;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

static const unsigned char PROGMEM REGAR[] = {
	// 'regar, 48x48px
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x01, 0x76, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00, 0x02, 0xc5, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x25, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x05, 0x00, 0x00, 0x00, 0x00, 0x48, 0x01, 0x00, 0x00, 0x00, 
	0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x00, 0x90, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x08, 
	0x00, 0x00, 0x00, 0x40, 0x00, 0x04, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x04, 0x00, 0x00, 0x01, 0x80, 
	0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x08, 0x00, 0x00, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 
	0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x70, 0x00, 0x20, 0x00, 0x00, 0xc0, 0x68, 0x00, 0x40, 0x00, 0x10, 0xf0, 0xe4, 
	0x00, 0x80, 0x00, 0x00, 0xcf, 0xc0, 0x01, 0x00, 0x00, 0x02, 0xc0, 0x00, 0x02, 0x00, 0x00, 0x10, 
	0xcf, 0x00, 0x04, 0x00, 0x00, 0x01, 0xd0, 0x00, 0x20, 0x00, 0x00, 0x80, 0xc0, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x40, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static const unsigned char PROGMEM CALOR[] = {
	// 'CALOR, 48x48px
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 
	0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0xc0, 0x00, 0x00, 0x03, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x07, 0x00, 0x00, 0x60, 0x00, 0x00, 
	0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 
	0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x01, 0xff, 0xfe, 0x00, 0x00, 
	0x00, 0x03, 0xff, 0xff, 0x00, 0x00, 0x00, 0x03, 0xff, 0xfe, 0x20, 0x00, 0x00, 0x07, 0xff, 0xfe, 
	0x78, 0x00, 0x00, 0x07, 0xff, 0xfe, 0xf8, 0x00, 0x00, 0x0f, 0xff, 0xfe, 0xf8, 0x00, 0x00, 0x0f, 
	0xff, 0xfe, 0xd8, 0x00, 0x00, 0x0f, 0xff, 0xfe, 0xd8, 0x00, 0x38, 0x0f, 0xff, 0xfe, 0xd8, 0x1c, 
	0x38, 0x0f, 0xff, 0xfe, 0xd8, 0x1c, 0x00, 0x0f, 0xff, 0xfe, 0xd8, 0x00, 0x00, 0x0f, 0xff, 0xfe, 
	0xd8, 0x00, 0x00, 0x0f, 0xff, 0xfe, 0xd8, 0x00, 0x00, 0x0f, 0xff, 0xfe, 0xdc, 0x00, 0x00, 0x0f, 
	0xff, 0xfe, 0xd8, 0x00, 0x00, 0x03, 0xff, 0xfe, 0xd8, 0x00, 0x00, 0x03, 0xff, 0xfe, 0xd8, 0x00, 
	0x00, 0x01, 0xff, 0xfe, 0xd8, 0x00, 0x00, 0x00, 0x7f, 0xfc, 0xd8, 0x00, 0x00, 0x00, 0x3f, 0xf0, 
	0xd8, 0x00, 0x00, 0x00, 0x03, 0x80, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x01, 0xdc, 0x00, 0x00, 0x60, 
	0x00, 0x01, 0x06, 0x00, 0x00, 0xe0, 0x00, 0x03, 0x26, 0x00, 0x00, 0xc0, 0x00, 0x03, 0x26, 0x00, 
	0x00, 0x00, 0x00, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0xf8, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 
	0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const unsigned char PROGMEM logo_bmp[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0xf8, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x19, 0x8c, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0f, 0x04, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x06, 0x04, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x06, 0x0c, 0x00,
  0x00, 0x00, 0x03, 0xe0, 0x60, 0xf8, 0x7e, 0x1f, 0x18, 0x40, 0x00, 0x00, 0x1b, 0x06, 0x0d, 0x80,
  0x00, 0x00, 0x0f, 0x30, 0xe0, 0xdc, 0x67, 0x18, 0x1c, 0x40, 0x00, 0x00, 0x7f, 0x04, 0x0f, 0xe0,
  0x00, 0x00, 0x0c, 0x00, 0xf0, 0xc6, 0x63, 0x10, 0x1c, 0x40, 0x00, 0x00, 0xc3, 0x8c, 0x18, 0x30,
  0x00, 0x00, 0x08, 0x01, 0xb0, 0xce, 0x61, 0x98, 0x16, 0x40, 0x00, 0x00, 0x80, 0xff, 0xb0, 0x30,
  0x00, 0x00, 0x18, 0xe1, 0x98, 0xfc, 0x61, 0x9f, 0x16, 0x40, 0x00, 0x00, 0x80, 0x70, 0xf0, 0x10,
  0x00, 0x00, 0x18, 0xf1, 0x18, 0xf8, 0x61, 0x9e, 0x13, 0x40, 0x00, 0x00, 0x80, 0x40, 0x60, 0x30,
  0x00, 0x00, 0x0c, 0x33, 0xf8, 0xcc, 0x61, 0x90, 0x13, 0xc0, 0x00, 0x00, 0xc0, 0xc0, 0x30, 0x20,
  0x00, 0x00, 0x0c, 0x33, 0xfc, 0xcc, 0x63, 0x10, 0x11, 0xc0, 0x00, 0x00, 0x70, 0x80, 0x38, 0xe0,
  0x00, 0x00, 0x07, 0x72, 0x0c, 0xc6, 0x7f, 0x1f, 0x10, 0xc0, 0x00, 0x00, 0x3f, 0x80, 0x3f, 0xc0,
  0x00, 0x00, 0x03, 0xf2, 0x04, 0xc6, 0x7e, 0x1f, 0x90, 0xc0, 0x00, 0x00, 0x7f, 0x80, 0x33, 0xc0,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x20, 0x60,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x40, 0x60, 0x30,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe0, 0xe0, 0x30,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0xff, 0xf0, 0x30,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0x8e, 0x38, 0x30,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x06, 0x0f, 0xe0,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x06, 0x0f, 0xc0,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x06, 0x0c, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0e, 0x04, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0e, 0x0c, 0x00,
  0x03, 0xc3, 0xe0, 0x82, 0x7f, 0x3c, 0x07, 0x86, 0x0c, 0x1f, 0x1e, 0x00, 0x03, 0x1f, 0x0c, 0x00,
  0x07, 0xe7, 0xf1, 0xc2, 0x7f, 0x3f, 0x1f, 0xc6, 0x0c, 0x1f, 0x3f, 0x80, 0x01, 0xf5, 0xf8, 0x00,
  0x0e, 0x0e, 0x19, 0xe2, 0x1c, 0x23, 0x18, 0x66, 0x0c, 0x18, 0x31, 0x80, 0x00, 0x64, 0x60, 0x00,
  0x0c, 0x0c, 0x19, 0xf2, 0x1c, 0x23, 0x30, 0x66, 0x0c, 0x18, 0x31, 0x80, 0x00, 0x04, 0x00, 0x00,
  0x1c, 0x0c, 0x19, 0xb2, 0x1c, 0x37, 0x38, 0x76, 0x0c, 0x1f, 0x3b, 0x80, 0x00, 0x04, 0x00, 0x00,
  0x1c, 0x0c, 0x19, 0x9e, 0x1c, 0x3e, 0x30, 0x76, 0x0c, 0x1f, 0x3f, 0x00, 0x3f, 0xc4, 0x00, 0x00,
  0x0c, 0x0c, 0x19, 0x9e, 0x1c, 0x26, 0x30, 0x76, 0x0c, 0x18, 0x33, 0x00, 0xf0, 0xf4, 0x00, 0x00,
  0x0c, 0x0c, 0x19, 0x8e, 0x1c, 0x23, 0x30, 0x66, 0x0c, 0x18, 0x31, 0x81, 0x83, 0xfc, 0x00, 0x00,
  0x0e, 0x66, 0x39, 0x8e, 0x1c, 0x23, 0x18, 0xe7, 0x0e, 0x18, 0x31, 0x80, 0xe0, 0x7c, 0x00, 0x00,
  0x07, 0xe3, 0xe1, 0x86, 0x1c, 0x21, 0x8f, 0x87, 0xef, 0xdf, 0xb0, 0xc0, 0x7f, 0xe4, 0x1f, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x04, 0xff, 0xe0,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf8, 0x70,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf8, 0x38,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xf1, 0xe0,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x3f, 0x80,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

#define riesgo_clima 11
#define buzzer 15
#define ejecutando 10
#define activado 9

#define falta_agua 8
#define rele 7
#define sensor_agua 5
int estado=0;
bool valor = false;
bool valor1 = false;
bool valor2 = false;
int terminar = 0;
void setup() {

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.display();
  delay(20); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();


  display.drawBitmap(0, 10, logo_bmp, 128, 52, 1);
  //display.setCursor(0, 0);// Draw a single pixel in white
  //display.print("by MONKEY CLUB");
  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
  if (!rtc.begin()) {
  }
  //rtc.adjust(DateTime(__DATE__,__TIME__));

  // put your setup code here, to run once:
  pinMode(B, INPUT);
  pinMode(A, INPUT);
  pinMode(rele, OUTPUT);
  pinMode(falta_agua, OUTPUT);
  pinMode(sensor_agua, INPUT);
  pinMode(5, INPUT);
  pinMode(activado, OUTPUT);
  pinMode(SW, INPUT);
  pinMode(ejecutando, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(A), encoder, LOW);
  Serial.begin(9600);

  pinMode(riesgo_clima, OUTPUT);
  pinMode(buzzer, OUTPUT);

  ultimotiempo3 = millis();
  ultimotiempo = millis();
  ultimotiempo2 = millis();
  ultimotiempo4 = millis();
}

void loop() {
  // put your main code here, to run repeatedly:

DateTime fecha= rtc.now();
hora= fecha.hour();
minuto= fecha.minute();
segundo= fecha.second();

 // alarma_agua();
  sensor_lm35();
  pulsador();
  //artificial();
  regado();
  if (menu == 0) {
    oled();
    alerta();
  }
  else if ( menu == 1) {
    alerta();
    oled2();
  }
   else if ( menu == 2) {
    alerta();
    oled3();
  }


}



void alerta() {
  if ( temp >= 24) {// CONFIGURAR TEMPERATURA
    menu = 1;
    alarma();
  }

  else {
    menu=estado;
    digitalWrite(riesgo_clima, LOW);
    digitalWrite(buzzer, LOW);
   // menu = 0;
  }


}
void alarma() {
  //  digitalWrite(alarma_buzzer, HIGH);
  //  enc3=1;
  unsigned long  tiempo = millis();
  //activar cuando se tenga en fisico
  if (tiempo - ultimotiempo > 1000) {
    valor = !valor;
    digitalWrite(riesgo_clima, valor);
    digitalWrite(buzzer, valor);
    ultimotiempo = tiempo;
  }

}
void alarma_agua() {

  if (digitalRead(5) == HIGH) {
    unsigned long  tiempo2 = millis();
    if (tiempo2 - ultimotiempo2 > 500) {
      digitalWrite(falta_agua, !digitalRead(falta_agua));
      ultimotiempo2 = tiempo2;
    }
  }
  else {
      digitalWrite(falta_agua, LOW);
  }
}
void oled() {
  
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("HORA ACTUAL: ");
  display.setCursor(80, 0);
  display.print(hora);
  display.print(":");
  display.print(minuto);
  display.print(":");
  display.print(segundo);
  display.setTextSize(2);
  display.setCursor(5, 16);
  display.print("H.RG:");
  display.print(horaset);
  display.print(":");
  display.print(minutoset);
  display.setCursor(5, 32);
  display.print("TEMP:");
  display.print(temp);
  display.setCursor(5, 48);
  display.print("T.PR:");
  display.print(segundoset);
  //display.print(":");
  //display.print(minutoset2);
  display.display();
}
void oled2 () {
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(30, 0);
  display.print("ALERTA");
  display.drawBitmap(10, 20, CALOR, 48, 48, 1);
  display.setCursor(65, 35);
  display.print(temp);
  display.display();
}
void oled3 () {
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(20, 0);
  display.print("REGANDO");
  display.setCursor(80, 0);
  display.print(hora);
  display.print(":");
  display.print(minuto);
  display.print(":");
  display.print(segundo);
  display.drawBitmap(5, 15, REGAR, 48, 48, 1);
  display.setTextSize(2);
  display.setCursor(70, 25);
  display.print("FIN");
  display.setCursor(65, 45);
  display.print(segundoset);
  //display.print(":");
 // display.print(minutoset2);
  display.display();
}



void encoder() {
  unsigned long tiempoInterrupcion = millis();
  static unsigned long ultimaInterrupcion = 0;

  if (tiempoInterrupcion - ultimaInterrupcion > 10) {
    if (digitalRead(B) == HIGH) {

      posicion = posicion + 1;
    }
    else {
      posicion = posicion - 1;
    }

    posicion = min(60, max(0, posicion));
    ultimaInterrupcion = tiempoInterrupcion;
  }
}

void sensor_lm35() {
  unsigned long tiempo3 = millis();
  if (tiempo3 - ultimotiempo3 > 1000) {
    t = analogRead(A3);
    temp = (t * 480.0) / 1024.0;
    ultimotiempo3 = millis();
  }
}
void pulsador() {
  if (digitalRead(SW) == HIGH && enc == 0) {
    conteo++;
    enc = 1;
  }
  if (digitalRead(SW) == LOW && enc == 1) {
    enc = 0;
  }
  if (conteo == 1) {
  display.drawRect(65,15,25,18,WHITE);
  display.display();

    if (posicion != anterior) {
      anterior = posicion;
    }
    horaset = posicion;
    horaset = horaset;
    if (posicion >= 24) {
      posicion = 0;
    }
    digitalWrite(activado, LOW);
  }
  if (conteo == 2) {
  display.drawRect(100,15,25,18,WHITE);
  display.display();

    if (posicion != anterior) {
      anterior = posicion;
    }

    minutoset = posicion;
    minutoset = minutoset;
    if (posicion >= 60) {
      posicion = 0;
    }
    digitalWrite(activado, LOW);
  }

  if (conteo == 3) {
  display.drawRect(65,46,25,18,WHITE);
  display.display();

    if (posicion != anterior) {
      anterior = posicion;
    }
    segundoset = posicion;
    segundoset = segundoset;
    if (posicion >= 59) {
      posicion = 0;
    }
    digitalWrite(activado, LOW);
  }
  /*if (conteo == 4) {
  display.drawRect(100,46,25,18,WHITE);
  display.display();
    if (posicion != anterior) {
      anterior = posicion;
    }
    minutoset2 = posicion;
    minutoset2 = minutoset2;
    if (posicion >= 60) {
      posicion = 0;
    }
    digitalWrite(activado, LOW);
  }*/
  if (conteo == 4) {
    digitalWrite(activado, HIGH);
    menu = 0;
    terminar=0;
    conteo = 0;
  }
}
void artificial() {
  unsigned long  tiempo4 = millis();
  if (tiempo4 - ultimotiempo4 > 972) {
    segundo++;
    ultimotiempo4 = tiempo4;
  }
  if (segundo >= 60) {
    segundo = 0;
    minuto++;
  }
  if (minuto >= 60) {
    minuto = 0;
    hora++;
  }
  if (hora >= 24) {
    hora = 0;
  }
}
void regado() {

  if (hora == horaset && minuto == minutoset && terminar==0) {
    if(conteo==0){
      digitalWrite (ejecutando, HIGH);
      digitalWrite(rele, HIGH);
      estado=2;
      terminar=1;
    
    //terminar = 1;
    }
   // terminar=1;
 // else {
//terminar=1;

  }
  if (segundoset==segundo && terminar==1) {
    if(conteo==0){
      digitalWrite (ejecutando, LOW);
      digitalWrite(rele, LOW);
      estado=0;
     //terminar=0;
    }
    if(minutoset==minuto){
    terminar=1;
    }
    else{
      terminar=0;
    }
  // terminar=0;
  }
}
GND5VSDASCLSQWRTCDS1307+
NOCOMNCVCCGNDINLED1PWRRelay Module