Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_Sensor.h>
- #include "DHT.h"
- //librerias para el sensor temperatura & humedad
- #include <Adafruit_MLX90614.h>
- //libreria para el sensor de calor a distancia
- #include <Wire.h>
- #include <LCD.h>
- #include <LiquidCrystal_I2C.h>
- /* librerias para el LCD con circuito I2C
- /*
- ** This example uses F Malpartida's NewLiquidCrystal library. Obtain from:
- ** https://bitbucket.org/fmalpartida/new-liquidcrystal
- */
- #include <stdio.h>
- #include <DS1302.h>
- //libreria para reloj externo modelo DS1302
- //https://github.com/msparks/arduino-ds1302
- //datasheet: // http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
- #define D7 7//pin de sensor de aire mq 135
- //tutorial: https://components101.com/sensors/mq135-gas-sensor-for-air-quality
- //datasheet: http://www.china-total.com/product/meter/gas-sensor/mq135.pdf
- //otro tutorial xd
- //https://www.element14.com/community/community/design-challenges/in-the-air-design-challenge/blog/2015/02/21/in-the-air-challenge-air-quality-sensor-box
- /*
- S0 - 3 VERDE
- S1 - 4 AMARILLO
- S2 - 5 AZUL
- s3 - 6 AMARILLO
- z (sig ) = 7 PURPURA
- */
- //SCL 1 - y3
- // SDA 2 - y2
- //barometro bpm180
- //------------------------ajustes de LCD--------------------------
- #define I2C_ADDR 0x3F // Define I2C Address where the PCF8574A is
- #define BACKLIGHT_PIN 3
- #define En_pin 2
- #define Rw_pin 1
- #define Rs_pin 0
- #define D4_pin 4
- #define D5_pin 5
- #define D6_pin 6
- #define D7_pin 7
- //------------------------ajustes de LCD--------------------------
- //---------------constructores---------------
- //constructor para utilizar el LCD
- LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
- DHT dht(13, DHT11);
- //constructor para el sensor de calor a distancia MLX90614
- Adafruit_MLX90614 mlx = Adafruit_MLX90614();
- //---------------------variables globales----------------
- //variables del reloj
- boolean sync = false;
- boolean noExisteCLK = false;
- //
- int clk[3];
- // 0 - clk;
- // 1 - rst;
- // 2 - dat;
- //-----------------------------variables globales del sistema---------------------
- //al parecer las variables que dependen de varias variables con los puetos binarios
- //en PORTD no pueden ser leidas al mismo tiempo por suficente tiempo hasta que el sistema
- //crashee.
- int contador=0; //contador que llevara el tiempo de loops
- int temperatura;
- int humedad;
- int calor;
- int oxigeno;
- int lumen;
- int atm;
- static int temp_dis[2];
- int fuego;
- int sonico[2];
- //0 - trigPin; //no da con la señal y la pantalla se queda en blanco por varios segundos
- //1 - echoPin;
- int distancia;
- long duracion;
- int agua;
- /*
- * PORTD maps to Arduino digital pins 0 to 7
- DDRD - The Port D Data Direction Register - read/write
- PORTD - The Port D Data Register - read/write
- PIND - The Port D Input Pins Register - read only
- https://www.arduino.cc/en/Reference/PortManipulation
- */
- namespace {
- void foo(){
- //esta tecnica crea un constructor falso en la clase abstracta
- //para poder asi inicializar valores volatiles o yo que se
- PORTD = B00001100;
- clk[0] = PORTD;
- PORTD = B00001011;
- clk[1] = PORTD;
- PORTD = B00001101;
- clk[2] = PORTD;
- }
- int relojPinMux(int posicion){
- if(noExisteCLK){
- foo();
- noExisteCLK = true;
- return clk[posicion];
- }
- else{
- return clk[posicion];
- }
- }
- DS1302 rtc(relojPinMux(0), relojPinMux(1), relojPinMux(2));
- // Set the appropriate digital I/O pin connections. These are the pin
- // assignments for the Arduino as well for as the DS1302 chip. See the DS1302
- // datasheet:
- //
- // http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
- //CLK - D12
- //DAT - D13
- //RST - DY11
- // Create a DS1302 object.
- String dayAsString(const Time::Day day) {
- switch (day) {
- case Time::kSunday: return "Domingo";
- case Time::kMonday: return "Lunes";
- case Time::kTuesday: return "Martes";
- case Time::kWednesday: return "Miercoles";
- case Time::kThursday: return "Jueves";
- case Time::kFriday: return "Viernes";
- case Time::kSaturday: return "Sabado";
- }
- return "(unknown day)";
- }
- //---------------constructores---------------
- //-------------------reloj-----------------
- void lcdReloj(Time t) {
- // Get the current time and date from the chip.
- // Name the day of the week.
- const String day = dayAsString(t.day);
- // Format the time and date and insert into the temporary buffer.
- char bufferFecha[25];
- char bufferHora[25];
- snprintf(bufferFecha, sizeof(bufferFecha), "%s %04d-%02d-%02d",
- day.c_str(),
- t.yr, t.mon, t.date);
- snprintf(bufferHora, sizeof(bufferHora), "%02d:%02d:%02d",
- t.hr, t.min, t.sec);
- /*
- snprintf() in C library
- The snprintf() function formats and stores a series of characters and values
- in the array buffer. The snprintf() function with the addition of the n argument,
- which indicates the maximum number of characters (including at the end of null character)
- to be written to buffer. It is defined in <stdio.h> header file.
- */
- lcd.setCursor(0, 2);
- lcd.print(bufferFecha);
- //loop para actualizar el reloj cada segundo
- lcd.setCursor(0, 3);
- lcd.print(bufferHora);
- }//fin de lcdreloj
- }//fin de namespace
- void modulo() {
- lcd.print("********************");
- lcd.setCursor(0, 1);
- lcd.print("** Bienvenido al ***");
- lcd.setCursor(0, 2);
- lcd.print("***** modulo de ****");
- lcd.setCursor(0, 3);
- lcd.print("**** JOJO :^) ******");
- delay(5000);
- /*
- lcd.print("********************");
- lcd.setCursor(0, 1);
- lcd.print("** Bienvenido al ***");
- lcd.setCursor(0, 2);
- lcd.print("***** modulo de ****");
- lcd.setCursor(0, 3);
- lcd.print("**** WEEDUINO ******");
- delay(5000);
- */
- }
- void dht11(){
- temperatura = dht.readTemperature();
- humedad = dht.readHumidity();
- calor = dht.computeHeatIndex(temperatura, humedad);
- Serial.print("1) temperatura: ");
- Serial.print(temperatura);
- Serial.println("*C");
- lcd.print("1) temperatura: ");
- lcd.print(temperatura);
- lcd.print("*C");
- lcd.setCursor(0, 1);
- //2)
- Serial.print("2) humedad: ");
- lcd.print("2) humedad: ");
- Serial.print(humedad);
- Serial.println("%");
- lcd.print(humedad);
- lcd.print("%");
- //3)
- lcd.setCursor(0, 2);
- Serial.print("3) calor: ");
- Serial.print(calor);
- Serial.println("*C");
- lcd.print("3) calor: ");
- lcd.print(calor);
- lcd.print("*C");
- lcd.setCursor(0,3);
- lcd.print("round: #");
- lcd.print(contador);
- delay(5000);
- }
- void aire(){
- PORTD = B00000000; //1)
- oxigeno = analogRead(PORTD);
- Serial.print("4) oxigeno: ");
- lcd.print("4) oxigeno : ");
- Serial.print(oxigeno, DEC);
- Serial.println(" PPM");
- lcd.print(oxigeno, DEC);
- lcd.print(" ppm");
- lcd.setCursor(0,3);
- lcd.print("round: #");
- lcd.print(contador);
- }
- void lcdlumen(){
- PORTD = B00000001; //2
- lumen = analogRead(PORTD);
- Serial.print("5) lumenes: ");
- lcd.print("5) lumenes: ");
- Serial.print(lumen, DEC);
- Serial.println(" lm");
- lcd.print(lumen, DEC);
- lcd.print(" lm");
- lcd.setCursor(0,3);
- lcd.print("round: #");
- lcd.print(contador);
- }
- void lcdatm(){
- PORTD = B00000011; //3
- atm = analogRead(PORTD);
- Serial.print("6) presion atmosferica: ");
- lcd.print("6)presion atm: ");
- Serial.print(atm, DEC);
- Serial.println(" ATM");
- lcd.setCursor(0, 1);
- lcd.print(atm, DEC);
- lcd.print(" ATM");
- lcd.setCursor(0,3);
- lcd.print("round: #");
- lcd.print(contador);
- }
- int* lcdtemp_a_dis(int pos){
- //su salida SDA Y SCL va a ser multiplexada
- // datasheet:
- //https://www.sparkfun.com/datasheets/Sensors/Temperature/MLX90614_rev001.pdf
- //SDA - DY4
- //SCL DY5
- PORTD = B00000100; //SDA - DY4
- temp_dis[0] = analogRead(PORTD);
- PORTD = B00000101; ////SCL - DY5
- temp_dis[1] = analogRead(PORTD);
- return temp_dis[pos];
- }//FIN de temp_a_dis
- void lcdfuego(){
- PORTD = B00000110; //6
- fuego = analogRead(PORTD);
- Serial.print("8) Fuego: ");
- lcd.print(fuego);
- Serial.println(fuego);
- lcd.setCursor(0,3);
- lcd.print("round: #");
- lcd.print(contador);
- }
- int ultrasonido(){
- //https://www.arduino.cc/en/Tutorial/Ping
- //0 - trigPin; //no da con la señal y la pantalla se queda en blanco por varios segundos
- //1 - echoPin;
- PORTD = B00001000;
- sonico[0] = analogRead(PORTD); //0 - trigPin
- PORTD = B00000111;
- sonico[1] = analogRead(PORTD); //1 - echoPin;
- //echo y7
- //trig: y8
- pinMode(sonico[0], OUTPUT); // Sets the trigPin as an Output
- digitalWrite(sonico[0], LOW);
- delayMicroseconds(5);
- digitalWrite(sonico[0], HIGH);
- delayMicroseconds(10);
- digitalWrite(sonico[0], LOW);
- pinMode(sonico[1], INPUT); // Sets the echoPin as an Input
- duracion = pulseIn(sonico[1], HIGH);
- // Calculating the distance
- distancia= microsecondsToCentimeters(duracion);
- return distancia;
- }
- long microsecondsToCentimeters(long microseconds) {
- // The speed of sound is 340 m/s or 29 microseconds per centimeter.
- // The ping travels out and back, so to find the distance of the object we
- // take half of the distance travelled.
- return microseconds / 29 / 2;
- }
- void lcdagua(){
- PORTD = B00001010; //DY10
- agua = analogRead(PORTD);
- Serial.print("11) agua: ");
- lcd.print(agua);
- Serial.println(agua);
- lcd.setCursor(0,3);
- lcd.print("round: #");
- lcd.print(contador);
- }
- void setup() {
- Serial.begin(9600);
- dht.begin();
- lcd.begin (20, 4);
- lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
- lcd.setBacklight(HIGH);
- //rtc.writeProtect(false);
- //rtc.halt(false);
- //Time t(2018, 6, 5, 10, 47, 00 , Time::kTuesday);
- DDRD = B11111111;
- //DDRD - The Port D Data Direction Register - read/write
- //http://tronixstuff.com/2011/10/22/tutorial-arduino-port-manipulation/
- //https://www.arduino.cc/en/Reference/PortManipulation
- //establece el pin D7 como por donde va a recibir toda la informacion
- //del multiplexor
- pinMode(7, INPUT);
- }
- void loop() {
- //try{
- modulo();
- lcd.clear();
- dht11();
- lcd.clear();
- //1) temperatura
- //2) humedad
- //3) sensor de oxigeno DY2
- for(int x = 0; x < 5; x++){
- aire();
- delay(1000);
- lcd.clear();
- }
- lcd.clear();
- //4) sensor de luz GL5528 DY3
- for(int x=0; x < 5; x++){
- lcdlumen();
- delay(1000);
- lcd.clear();
- }
- lcd.clear();
- //5 barometro BMP180 DY4
- for(int x = 0; x < 5; x++){
- lcdatm();
- delay(1000);
- lcd.clear();
- }
- //6 DY5 temperatura a distancia MLX90614
- //DY5 - 0 - temp_dis1;
- //DY6 -1 - temp_dis2;
- //solo imprime "000.." en el espacio del lcd de
- //"round"
- //ejemplo (3er round): "round #30000
- Serial.print("7) temperatura a distancia: ");
- lcd.print("7)temp a dis: ");
- for(int x = 0; x < 5; x++){
- int cont=0;
- if (cont <= 3){
- //referencia:
- //https://forum.arduino.cc/index.php?topic=463292.0
- Serial.println(*lcdtemp_a_dis(cont), DEC);
- lcd.print(*lcdtemp_a_dis(cont), DEC);
- lcd.setCursor(0,3);
- lcd.print("round: #");
- lcd.print(contador);
- delay(1000);
- cont++;
- }
- else{
- cont=0;
- }
- }
- lcd.clear();
- //7 DY6
- //sensor generico de fuego
- for(int x = 0; x < 5; x++){
- lcd.print("8) fuego: ");
- lcdfuego();
- delay(1000);
- lcd.clear();
- }
- lcd.clear();
- //8 DY7
- //sensor ultrasonido HC-SR04
- /*------------------------------------------------
- * se queda estancado en este espacio del programa
- * y no avanza. porque? no solo eso, la pantalla flashea
- * y solo avienta 0. A VECES da la distancia, pero
- * en contadas ocasiones y casi por un golpe de suerte
- *------------------------------------------------
- Serial.print("9) distancia: ");
- lcd.print("9) distancia: ");
- for(int x = 0; x < 5; x++){
- Serial.println(ultrasonido(), DEC);
- lcd.print(ultrasonido(), DEC);
- lcd.setCursor(0,3);
- lcd.print("round: #");
- lcd.print(contador);
- delay(1000);
- }
- lcd.clear();
- */
- //9 D12
- //reloj
- /*
- * el reloj tampoco guarda la hora. despliega:
- * 65-1
- * (unknown day) 2165-
- * 27:165:85
- */
- Serial.println("10) tiempo :");
- lcd.print("10 tiempo : ");
- for(int x= 0; x < 5; x++){
- lcdReloj(rtc.time());
- delay(1000);
- }
- lcd.clear();
- //11 DY10 sens0r agua 2
- for(int x=0; x < 5; x++){
- lcd.print("11) agua nivel: ");
- lcdagua();
- //10 Y9
- //sensor de agua rojo MH
- delay(1000);
- lcd.clear();
- }
- lcd.clear();
- contador++;
- Serial.println();
- //}
- /*
- *catch(Exception e){
- Serial.print("numero de conteos hasta crash: ");
- Serial.println(contador);
- Serial.println(e);
- }
- */
- }//fin de loop
- /*
- El Sketch usa 13290 bytes (41%) del espacio de almacenamiento
- de programa. El máximo es 32256 bytes.
- Las variables Globales usan 910 bytes (44%) de la memoria
- dinámica, dejando 1138 bytes para las variables locales
- // fata:
- //bluetooth txt DY14
- // rxd DY15
- //sesor humedad para tierra
- //sensor temperatura paa tierra
- //infrarojo
- //3
- */
Advertisement
Add Comment
Please, Sign In to add comment