Guest User

garduino 1 stack overflow español

a guest
Jun 6th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.00 KB | None | 0 0
  1. #include <Adafruit_Sensor.h>
  2. #include "DHT.h"
  3. //librerias para el sensor temperatura & humedad
  4.  
  5. #include <Adafruit_MLX90614.h>
  6. //libreria para el sensor de calor a distancia
  7.  
  8. #include <Wire.h>
  9. #include <LCD.h>
  10. #include <LiquidCrystal_I2C.h>
  11. /* librerias para el LCD con circuito I2C
  12. /*
  13. ** This example uses F Malpartida's NewLiquidCrystal library. Obtain from:
  14. ** https://bitbucket.org/fmalpartida/new-liquidcrystal
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <DS1302.h>
  19. //libreria para reloj externo modelo DS1302
  20. //https://github.com/msparks/arduino-ds1302
  21. //datasheet: // http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
  22.  
  23.  
  24. #define D7 7//pin de sensor de aire mq 135
  25.  
  26. //tutorial: https://components101.com/sensors/mq135-gas-sensor-for-air-quality
  27. //datasheet: http://www.china-total.com/product/meter/gas-sensor/mq135.pdf
  28. //otro tutorial xd
  29. //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
  30.  
  31. /*
  32. S0 - 3 VERDE
  33. S1 - 4 AMARILLO
  34. S2 - 5 AZUL
  35. s3 - 6 AMARILLO
  36. z (sig ) = 7 PURPURA
  37. */
  38. //SCL 1 - y3
  39. // SDA 2 - y2
  40. //barometro bpm180
  41. //------------------------ajustes de LCD--------------------------
  42.  
  43. #define I2C_ADDR 0x3F // Define I2C Address where the PCF8574A is
  44. #define BACKLIGHT_PIN 3
  45. #define En_pin 2
  46. #define Rw_pin 1
  47. #define Rs_pin 0
  48. #define D4_pin 4
  49. #define D5_pin 5
  50. #define D6_pin 6
  51. #define D7_pin 7
  52.  
  53. //------------------------ajustes de LCD--------------------------
  54.  
  55. //---------------constructores---------------
  56. //constructor para utilizar el LCD
  57. LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
  58.  
  59. DHT dht(13, DHT11);
  60.  
  61. //constructor para el sensor de calor a distancia MLX90614
  62. Adafruit_MLX90614 mlx = Adafruit_MLX90614();
  63.  
  64. //---------------------variables globales----------------
  65. //variables del reloj
  66. boolean sync = false;
  67. boolean noExisteCLK = false;
  68. //
  69. int clk[3];
  70. // 0 - clk;
  71. // 1 - rst;
  72. // 2 - dat;
  73.  
  74. //-----------------------------variables globales del sistema---------------------
  75. //al parecer las variables que dependen de varias variables con los puetos binarios
  76. //en PORTD no pueden ser leidas al mismo tiempo por suficente tiempo hasta que el sistema
  77. //crashee.
  78.  
  79. int contador=0; //contador que llevara el tiempo de loops
  80.  
  81. int temperatura;
  82. int humedad;
  83. int calor;
  84. int oxigeno;
  85. int lumen;
  86. int atm;
  87. static int temp_dis[2];
  88.  
  89. int fuego;
  90. int sonico[2];
  91. //0 - trigPin; //no da con la señal y la pantalla se queda en blanco por varios segundos
  92. //1 - echoPin;
  93. int distancia;
  94. long duracion;
  95.  
  96. int agua;
  97. /*
  98. * PORTD maps to Arduino digital pins 0 to 7
  99.  
  100. DDRD - The Port D Data Direction Register - read/write
  101. PORTD - The Port D Data Register - read/write
  102. PIND - The Port D Input Pins Register - read only
  103. https://www.arduino.cc/en/Reference/PortManipulation
  104. */
  105.  
  106. namespace {
  107.  
  108. void foo(){
  109. //esta tecnica crea un constructor falso en la clase abstracta
  110. //para poder asi inicializar valores volatiles o yo que se
  111. PORTD = B00001100;
  112. clk[0] = PORTD;
  113.  
  114. PORTD = B00001011;
  115. clk[1] = PORTD;
  116.  
  117. PORTD = B00001101;
  118. clk[2] = PORTD;
  119. }
  120.  
  121. int relojPinMux(int posicion){
  122. if(noExisteCLK){
  123. foo();
  124. noExisteCLK = true;
  125. return clk[posicion];
  126. }
  127. else{
  128. return clk[posicion];
  129. }
  130. }
  131.  
  132. DS1302 rtc(relojPinMux(0), relojPinMux(1), relojPinMux(2));
  133.  
  134. // Set the appropriate digital I/O pin connections. These are the pin
  135. // assignments for the Arduino as well for as the DS1302 chip. See the DS1302
  136. // datasheet:
  137. //
  138. // http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
  139.  
  140. //CLK - D12
  141. //DAT - D13
  142. //RST - DY11
  143.  
  144. // Create a DS1302 object.
  145.  
  146. String dayAsString(const Time::Day day) {
  147. switch (day) {
  148. case Time::kSunday: return "Domingo";
  149. case Time::kMonday: return "Lunes";
  150. case Time::kTuesday: return "Martes";
  151. case Time::kWednesday: return "Miercoles";
  152. case Time::kThursday: return "Jueves";
  153. case Time::kFriday: return "Viernes";
  154. case Time::kSaturday: return "Sabado";
  155. }
  156. return "(unknown day)";
  157. }
  158.  
  159. //---------------constructores---------------
  160.  
  161. //-------------------reloj-----------------
  162. void lcdReloj(Time t) {
  163.  
  164. // Get the current time and date from the chip.
  165.  
  166. // Name the day of the week.
  167. const String day = dayAsString(t.day);
  168.  
  169. // Format the time and date and insert into the temporary buffer.
  170. char bufferFecha[25];
  171. char bufferHora[25];
  172. snprintf(bufferFecha, sizeof(bufferFecha), "%s %04d-%02d-%02d",
  173. day.c_str(),
  174. t.yr, t.mon, t.date);
  175. snprintf(bufferHora, sizeof(bufferHora), "%02d:%02d:%02d",
  176. t.hr, t.min, t.sec);
  177.  
  178. /*
  179. snprintf() in C library
  180.  
  181. The snprintf() function formats and stores a series of characters and values
  182. in the array buffer. The snprintf() function with the addition of the n argument,
  183. which indicates the maximum number of characters (including at the end of null character)
  184. to be written to buffer. It is defined in <stdio.h> header file.
  185. */
  186. lcd.setCursor(0, 2);
  187. lcd.print(bufferFecha);
  188. //loop para actualizar el reloj cada segundo
  189. lcd.setCursor(0, 3);
  190. lcd.print(bufferHora);
  191. }//fin de lcdreloj
  192.  
  193. }//fin de namespace
  194.  
  195.  
  196.  
  197.  
  198. void modulo() {
  199.  
  200.  
  201. lcd.print("********************");
  202. lcd.setCursor(0, 1);
  203. lcd.print("** Bienvenido al ***");
  204. lcd.setCursor(0, 2);
  205. lcd.print("***** modulo de ****");
  206. lcd.setCursor(0, 3);
  207. lcd.print("**** JOJO :^) ******");
  208. delay(5000);
  209. /*
  210. lcd.print("********************");
  211. lcd.setCursor(0, 1);
  212. lcd.print("** Bienvenido al ***");
  213. lcd.setCursor(0, 2);
  214. lcd.print("***** modulo de ****");
  215. lcd.setCursor(0, 3);
  216. lcd.print("**** WEEDUINO ******");
  217. delay(5000);
  218. */
  219. }
  220.  
  221. void dht11(){
  222.  
  223. temperatura = dht.readTemperature();
  224. humedad = dht.readHumidity();
  225. calor = dht.computeHeatIndex(temperatura, humedad);
  226. Serial.print("1) temperatura: ");
  227. Serial.print(temperatura);
  228. Serial.println("*C");
  229. lcd.print("1) temperatura: ");
  230. lcd.print(temperatura);
  231. lcd.print("*C");
  232.  
  233. lcd.setCursor(0, 1);
  234.  
  235. //2)
  236. Serial.print("2) humedad: ");
  237. lcd.print("2) humedad: ");
  238. Serial.print(humedad);
  239. Serial.println("%");
  240. lcd.print(humedad);
  241. lcd.print("%");
  242.  
  243. //3)
  244. lcd.setCursor(0, 2);
  245. Serial.print("3) calor: ");
  246. Serial.print(calor);
  247. Serial.println("*C");
  248.  
  249. lcd.print("3) calor: ");
  250. lcd.print(calor);
  251. lcd.print("*C");
  252.  
  253. lcd.setCursor(0,3);
  254. lcd.print("round: #");
  255. lcd.print(contador);
  256.  
  257. delay(5000);
  258. }
  259.  
  260. void aire(){
  261.  
  262. PORTD = B00000000; //1)
  263. oxigeno = analogRead(PORTD);
  264.  
  265. Serial.print("4) oxigeno: ");
  266. lcd.print("4) oxigeno : ");
  267. Serial.print(oxigeno, DEC);
  268. Serial.println(" PPM");
  269. lcd.print(oxigeno, DEC);
  270. lcd.print(" ppm");
  271.  
  272. lcd.setCursor(0,3);
  273. lcd.print("round: #");
  274. lcd.print(contador);
  275.  
  276. }
  277.  
  278. void lcdlumen(){
  279.  
  280. PORTD = B00000001; //2
  281. lumen = analogRead(PORTD);
  282.  
  283. Serial.print("5) lumenes: ");
  284. lcd.print("5) lumenes: ");
  285. Serial.print(lumen, DEC);
  286. Serial.println(" lm");
  287.  
  288. lcd.print(lumen, DEC);
  289. lcd.print(" lm");
  290. lcd.setCursor(0,3);
  291. lcd.print("round: #");
  292. lcd.print(contador);
  293. }
  294.  
  295. void lcdatm(){
  296.  
  297. PORTD = B00000011; //3
  298. atm = analogRead(PORTD);
  299.  
  300. Serial.print("6) presion atmosferica: ");
  301. lcd.print("6)presion atm: ");
  302.  
  303. Serial.print(atm, DEC);
  304. Serial.println(" ATM");
  305.  
  306. lcd.setCursor(0, 1);
  307. lcd.print(atm, DEC);
  308. lcd.print(" ATM");
  309. lcd.setCursor(0,3);
  310. lcd.print("round: #");
  311. lcd.print(contador);
  312. }
  313.  
  314.  
  315. int* lcdtemp_a_dis(int pos){
  316. //su salida SDA Y SCL va a ser multiplexada
  317. // datasheet:
  318. //https://www.sparkfun.com/datasheets/Sensors/Temperature/MLX90614_rev001.pdf
  319.  
  320. //SDA - DY4
  321. //SCL DY5
  322.  
  323. PORTD = B00000100; //SDA - DY4
  324. temp_dis[0] = analogRead(PORTD);
  325.  
  326. PORTD = B00000101; ////SCL - DY5
  327. temp_dis[1] = analogRead(PORTD);
  328.  
  329. return temp_dis[pos];
  330.  
  331. }//FIN de temp_a_dis
  332.  
  333.  
  334. void lcdfuego(){
  335.  
  336. PORTD = B00000110; //6
  337. fuego = analogRead(PORTD);
  338.  
  339. Serial.print("8) Fuego: ");
  340. lcd.print(fuego);
  341. Serial.println(fuego);
  342.  
  343. lcd.setCursor(0,3);
  344. lcd.print("round: #");
  345. lcd.print(contador);
  346.  
  347. }
  348.  
  349.  
  350. int ultrasonido(){
  351. //https://www.arduino.cc/en/Tutorial/Ping
  352.  
  353. //0 - trigPin; //no da con la señal y la pantalla se queda en blanco por varios segundos
  354. //1 - echoPin;
  355.  
  356. PORTD = B00001000;
  357. sonico[0] = analogRead(PORTD); //0 - trigPin
  358.  
  359. PORTD = B00000111;
  360. sonico[1] = analogRead(PORTD); //1 - echoPin;
  361.  
  362. //echo y7
  363. //trig: y8
  364.  
  365. pinMode(sonico[0], OUTPUT); // Sets the trigPin as an Output
  366. digitalWrite(sonico[0], LOW);
  367. delayMicroseconds(5);
  368. digitalWrite(sonico[0], HIGH);
  369. delayMicroseconds(10);
  370. digitalWrite(sonico[0], LOW);
  371.  
  372. pinMode(sonico[1], INPUT); // Sets the echoPin as an Input
  373. duracion = pulseIn(sonico[1], HIGH);
  374.  
  375. // Calculating the distance
  376. distancia= microsecondsToCentimeters(duracion);
  377.  
  378. return distancia;
  379. }
  380.  
  381. long microsecondsToCentimeters(long microseconds) {
  382. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  383. // The ping travels out and back, so to find the distance of the object we
  384. // take half of the distance travelled.
  385. return microseconds / 29 / 2;
  386. }
  387.  
  388. void lcdagua(){
  389. PORTD = B00001010; //DY10
  390. agua = analogRead(PORTD);
  391.  
  392. Serial.print("11) agua: ");
  393. lcd.print(agua);
  394. Serial.println(agua);
  395. lcd.setCursor(0,3);
  396. lcd.print("round: #");
  397. lcd.print(contador);
  398. }
  399.  
  400. void setup() {
  401. Serial.begin(9600);
  402.  
  403. dht.begin();
  404. lcd.begin (20, 4);
  405. lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  406. lcd.setBacklight(HIGH);
  407.  
  408. //rtc.writeProtect(false);
  409. //rtc.halt(false);
  410. //Time t(2018, 6, 5, 10, 47, 00 , Time::kTuesday);
  411.  
  412. DDRD = B11111111;
  413. //DDRD - The Port D Data Direction Register - read/write
  414. //http://tronixstuff.com/2011/10/22/tutorial-arduino-port-manipulation/
  415. //https://www.arduino.cc/en/Reference/PortManipulation
  416.  
  417. //establece el pin D7 como por donde va a recibir toda la informacion
  418. //del multiplexor
  419. pinMode(7, INPUT);
  420. }
  421.  
  422. void loop() {
  423.  
  424. //try{
  425. modulo();
  426. lcd.clear();
  427.  
  428. dht11();
  429. lcd.clear();
  430.  
  431. //1) temperatura
  432. //2) humedad
  433.  
  434. //3) sensor de oxigeno DY2
  435. for(int x = 0; x < 5; x++){
  436. aire();
  437. delay(1000);
  438. lcd.clear();
  439. }
  440. lcd.clear();
  441.  
  442. //4) sensor de luz GL5528 DY3
  443. for(int x=0; x < 5; x++){
  444. lcdlumen();
  445. delay(1000);
  446. lcd.clear();
  447. }
  448. lcd.clear();
  449.  
  450. //5 barometro BMP180 DY4
  451. for(int x = 0; x < 5; x++){
  452. lcdatm();
  453. delay(1000);
  454. lcd.clear();
  455. }
  456.  
  457. //6 DY5 temperatura a distancia MLX90614
  458. //DY5 - 0 - temp_dis1;
  459. //DY6 -1 - temp_dis2;
  460.  
  461. //solo imprime "000.." en el espacio del lcd de
  462. //"round"
  463. //ejemplo (3er round): "round #30000
  464.  
  465. Serial.print("7) temperatura a distancia: ");
  466. lcd.print("7)temp a dis: ");
  467. for(int x = 0; x < 5; x++){
  468. int cont=0;
  469. if (cont <= 3){
  470.  
  471. //referencia:
  472. //https://forum.arduino.cc/index.php?topic=463292.0
  473. Serial.println(*lcdtemp_a_dis(cont), DEC);
  474. lcd.print(*lcdtemp_a_dis(cont), DEC);
  475.  
  476. lcd.setCursor(0,3);
  477. lcd.print("round: #");
  478. lcd.print(contador);
  479. delay(1000);
  480.  
  481. cont++;
  482. }
  483. else{
  484. cont=0;
  485. }
  486. }
  487. lcd.clear();
  488.  
  489. //7 DY6
  490. //sensor generico de fuego
  491. for(int x = 0; x < 5; x++){
  492. lcd.print("8) fuego: ");
  493. lcdfuego();
  494. delay(1000);
  495. lcd.clear();
  496. }
  497. lcd.clear();
  498.  
  499. //8 DY7
  500. //sensor ultrasonido HC-SR04
  501. /*------------------------------------------------
  502. * se queda estancado en este espacio del programa
  503. * y no avanza. porque? no solo eso, la pantalla flashea
  504. * y solo avienta 0. A VECES da la distancia, pero
  505. * en contadas ocasiones y casi por un golpe de suerte
  506. *------------------------------------------------
  507. Serial.print("9) distancia: ");
  508. lcd.print("9) distancia: ");
  509. for(int x = 0; x < 5; x++){
  510. Serial.println(ultrasonido(), DEC);
  511. lcd.print(ultrasonido(), DEC);
  512.  
  513. lcd.setCursor(0,3);
  514. lcd.print("round: #");
  515. lcd.print(contador);
  516. delay(1000);
  517. }
  518. lcd.clear();
  519. */
  520.  
  521. //9 D12
  522. //reloj
  523. /*
  524. * el reloj tampoco guarda la hora. despliega:
  525. * 65-1
  526. * (unknown day) 2165-
  527. * 27:165:85
  528. */
  529. Serial.println("10) tiempo :");
  530. lcd.print("10 tiempo : ");
  531. for(int x= 0; x < 5; x++){
  532. lcdReloj(rtc.time());
  533. delay(1000);
  534. }
  535. lcd.clear();
  536.  
  537. //11 DY10 sens0r agua 2
  538. for(int x=0; x < 5; x++){
  539. lcd.print("11) agua nivel: ");
  540. lcdagua();
  541. //10 Y9
  542. //sensor de agua rojo MH
  543. delay(1000);
  544. lcd.clear();
  545. }
  546. lcd.clear();
  547.  
  548. contador++;
  549. Serial.println();
  550. //}
  551. /*
  552. *catch(Exception e){
  553. Serial.print("numero de conteos hasta crash: ");
  554. Serial.println(contador);
  555. Serial.println(e);
  556. }
  557. */
  558.  
  559. }//fin de loop
  560. /*
  561. El Sketch usa 13290 bytes (41%) del espacio de almacenamiento
  562. de programa. El máximo es 32256 bytes.
  563. Las variables Globales usan 910 bytes (44%) de la memoria
  564. dinámica, dejando 1138 bytes para las variables locales
  565.  
  566. // fata:
  567. //bluetooth txt DY14
  568. // rxd DY15
  569. //sesor humedad para tierra
  570. //sensor temperatura paa tierra
  571. //infrarojo
  572. //3
  573. */
Advertisement
Add Comment
Please, Sign In to add comment