Explicacion y analisis del codigo

CREZCAMOS JUNTOS 


#include <DS3231.h> #include <time.h> #include <Wire.h> // LIBRERIA PARA DISPOSITIVOS I2C #include <LiquidCrystal_I2C.h> // LIBRERIA PARA EL LCD LiquidCrystal_I2C lcd(0x27, 16, 2); // INDICA LA DIRECCION DEL LCD 0x27 PARA UN LCD 16X2 //RTClib RTC; #define DS3231_I2C_ADDRESS 104 // EL RELOJ ESTA CONECTADO, LA DIRECCION ES Hex68 (Decimal 104) byte seconds, minutes, hours, day, date, month, year; char weekDay[4]; byte tMSB, tLSB; char my_array[100]; // Character array for printing something. //CONSTANTES #define UV 4 // DEFINE EL PIN 4 COMO UV #define BOMBA 7 // DEFINE EL PIN 7 COMO BOMBA #define sensorRPM 2 //DEFINE EL PIN 2 COMO SENSOR RPM #define PWM 3 //DEFINE EL PIN 3 COMO PIN DE SALIDA PWM int tiemposegs = 20; // VARIABLE PA INDICR EN SEGS EL TIEMPO QUE ENCENDERA LA BOMBA DE AGUA int HENCENDIDO = 5; // VARIABLE QUE INDICA LA HORA DE ENCENDIDO DE LA LAMPAR UV (FORMATO DE 24 HRS.) int HAPAGADO = 23; // VARIABLE QUE INDICA LA HORA DE APAGADO DE LA LAMPAR UV (FORMATO DE 24 HRS.) int velocidadINTRACTOR = 150; //VARIABLE QUE DEFINE LA VELOCIDAD DEL INTRACTOR int sensorVal; //VARIABLE QUE ALMACENA EL VALOR DE RPM DEL INTRACTOR byte GOTA[] = { 0x04, 0x04, 0x0A, 0x0A, 0x11, 0x11, 0x0E, 0x00 }; byte UVENCENDIDA[] = { 0x0E, 0x0E, 0x1F, 0x1F, 0x00, 0x15, 0x15, 0x15 }; byte UVAPAGADA[] = { 0x0E, 0x0E, 0x1F, 0x1F, 0x00, 0x00, 0x00, 0x00 }; byte HOJA1[] = { B01000, B01000, B01100, B01110, B00110, B00111, B10011, B11001 }; byte HOJA2[] = { B00100, B01110, B01110, B01110, B00100, B10101, B11111, B11111 }; byte HOJA3[] = { B00010, B00010, B00110, B01110, B01100, B11100, B10001, B10011 }; byte HOJA4[] = { B11110, B01111, B00111, B00011, B00001, B11111, B00000, B00000 }; byte HOJA5[] = { B00100, B01110, B01110, B11111, B11111, B11111, B00100, B00100 }; byte HOJA6[] = { B10111, B11110, B11100, B11000, B10000, B11111, B00000, B00000 }; void setup() { Serial.begin(9600); lcd.init(); // initialize the lcd Wire.begin(); analogWrite(PWM, velocidadINTRACTOR); pinMode(sensorRPM, INPUT); //DEFINE EL PIN sensorRPM como entrada pinMode(PWM, OUTPUT); //DEFINE EL PIN PWM como salida pinMode(UV , OUTPUT); pinMode(BOMBA, OUTPUT); digitalWrite (BOMBA, HIGH); digitalWrite (UV, HIGH); lcd.backlight(); IMPRIMIRLOGO(); delay(150); lcd.noBacklight(); delay(150); lcd.backlight(); delay(150); lcd.noBacklight(); delay(150); lcd.backlight(); delay(150); lcd.noBacklight(); delay(150); lcd.backlight(); delay(150); lcd.noBacklight(); delay(150); lcd.backlight(); delay(150); lcd.noBacklight(); delay(150); lcd.backlight(); delay(150); lcd.noBacklight(); delay(150); lcd.backlight(); delay(2000); lcd.clear(); } void loop() { watchConsole(); get3231Date(); IMPRIMIRENSERIAL(); analogWrite(PWM, velocidadINTRACTOR); sensorVal = analogRead(sensorRPM); //LECTURA DEL PIN RPM sprintf(my_array, "%d:%d:%d", hours, minutes, seconds); //Utilizando sprintf para convertir números a cadenas // Imprimir en LCD. lcd.setCursor(0, 0); lcd.print(weekDay); lcd.print(":"); if (date < 10) { lcd.print("0"); } lcd.print(date, DEC); lcd.print("/"); if (month < 10) { lcd.print("0"); } lcd.print(month, DEC); lcd.print("/"); lcd.print(year, DEC); lcd.setCursor(0, 1); if (hours < 10) { lcd.print("0"); } lcd.print(hours ); lcd.print(":"); if (minutes < 10) { lcd.print("0"); } lcd.print(minutes); lcd.print(":"); if (seconds < 10) { lcd.print("0"); } lcd.print(seconds); int millis(1000); // CONTROL DE BOMBA DE AGUA // CONTROL DE BOMBA DE AGUA----HORARIO 1 if( hours == 3 && minutes == 59 && seconds == 59 ) { ENCENDERBOMBA(); } else APAGARBOMBA(); // CONTROL DE LAMPARA UV if (( hours >=HAPAGADO && seconds >= 0 ) || ( hours <= (HENCENDIDO -1) && seconds >= 0 )){ APAGARUV(); velocidadINTRACTOR = 255; } else ENCENDERUV(); velocidadINTRACTOR = 150 ; } // FUNCIONES // ENCENDIDO DE BOMBA DE AGUA void ENCENDERBOMBA(){ while( tiemposegs != 0 ) { lcd.backlight(); digitalWrite( BOMBA, LOW ); lcd.clear(); lcd.createChar(0, GOTA); lcd.home(); lcd.write(0); lcd.setCursor(1, 0); lcd.print(tiemposegs); lcd.setCursor(0, 1); lcd.print("RIEGO EN PROCESO"); delay (1000); tiemposegs--; } digitalWrite( BOMBA, HIGH ); lcd.backlight(); IMPRIMIRLOGO(); delay(300); lcd.noBacklight(); delay(300); lcd.backlight(); delay(300); lcd.noBacklight(); delay(300); lcd.backlight(); delay(2000); lcd.clear(); } void APAGARBOMBA(){ digitalWrite( BOMBA, HIGH ); } // ENCENDIDO DE LUZ UV void ENCENDERUV(){ digitalWrite( UV, LOW ); lcd.backlight(); lcd.createChar(1, UVENCENDIDA); lcd.home(); lcd.setCursor(15, 1); lcd.write(1); } // APAGADO DE LUZ UV void APAGARUV(){ digitalWrite( UV, HIGH ); lcd.noBacklight(); lcd.createChar(2, UVAPAGADA); lcd.home(); lcd.setCursor(15, 1); lcd.write(2); } // IMPRESION EN LCD Y MONITOR SERIAL void IMPRIMIRLOGO(){ lcd.backlight(); lcd.home(); lcd.createChar(3, HOJA1); lcd.createChar(4, HOJA2); lcd.createChar(5, HOJA3); lcd.createChar(6, HOJA4); lcd.createChar(7, HOJA5); lcd.createChar(8, HOJA6); lcd.setCursor(0, 0); lcd.write(3); lcd.write(4); lcd.write(5); lcd.setCursor(0, 1); lcd.write(6); lcd.write(7); lcd.write(8); lcd.home(); lcd.setCursor(2, 1); lcd.print("CULTICUBO V1.0"); } void IMPRIMIRENSERIAL(){ Serial.print(weekDay); Serial.print(", "); Serial.print(date, DEC); Serial.print("/"); Serial.print(month, DEC); Serial.print("/"); Serial.print(year, DEC); Serial.print(" - "); Serial.print(hours, DEC); Serial.print(":"); Serial.print(minutes, DEC); Serial.println(); Serial.println(sensorVal); Serial.println(); } // Convert normal decimal numbers to binary coded decimal byte decToBcd(byte val) { return ( (val / 10 * 16) + (val % 10) ); } void watchConsole() { if (Serial.available()) { // Look for char in serial queue and process if found if (Serial.read() == 84) { //If command = "T" Set Date set3231Date(); get3231Date(); Serial.println(" "); } } } void set3231Date() { //T(sec)(min)(hours)(dayOfWeek)(dayOfmonth)(month)(year) //T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) //Example: 02-Feb-09 @ 19:57:11 for the 3rd day of the week -> T0524211190420 // T1124154091014 seconds = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result. minutes = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); hours = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); day = (byte) (Serial.read() - 48); date = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); month = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); year = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0x00); Wire.write(decToBcd(seconds)); Wire.write(decToBcd(minutes)); Wire.write(decToBcd(hours)); Wire.write(decToBcd(day)); Wire.write(decToBcd(date)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); } void get3231Date() { // send request to receive data starting at register 0 Wire.beginTransmission(DS3231_I2C_ADDRESS); // 104 is DS3231 device address Wire.write(0x00); // start at register 0 Wire.endTransmission(); Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes if (Wire.available()) { seconds = Wire.read(); // get seconds minutes = Wire.read(); // get minutes hours = Wire.read(); // get hours day = Wire.read(); date = Wire.read(); month = Wire.read(); //temp month year = Wire.read(); seconds = (((seconds & B11110000) >> 4) * 10 + (seconds & B00001111)); // convert BCD to decimal minutes = (((minutes & B11110000) >> 4) * 10 + (minutes & B00001111)); // convert BCD to decimal hours = (((hours & B00110000) >> 4) * 10 + (hours & B00001111)); // convert BCD to decimal (assume 24 hours mode) day = (day & B00000111); // 1-7 date = (((date & B00110000) >> 4) * 10 + (date & B00001111)); // 1-31 month = (((month & B00010000) >> 4) * 10 + (month & B00001111)); //msb7 is century overflow year = (((year & B11110000) >> 4) * 10 + (year & B00001111)); } else { //oh noes, no data! } switch (day) { case 1: strcpy(weekDay, "Dom"); break; case 2: strcpy(weekDay, "Lun"); break; case 3: strcpy(weekDay, "Mar"); break; case 4: strcpy(weekDay, "Mie"); break; case 5: strcpy(weekDay, "Jue"); break; case 6: strcpy(weekDay, "Vie"); break; case 7: strcpy(weekDay, "Sab"); break; } }

Comentarios

Entradas más populares de este blog

Versiones CultiCubo

Introducción a CultiCubo