Advertisement
hidromotic

Untitled

Nov 23rd, 2023
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.24 KB | None | 0 0
  1. #include <DFRobot_RGBLCD1602.h>
  2. #include "Ultrasonic.h"
  3.  
  4. // Pines de Sensores y configuracion inicial de la pantalla LCD
  5. Ultrasonic SENSOR_ULTRASONICO_DELANTERO(2, 3);
  6. Ultrasonic SENSOR_ULTRASONICO_TRASERO(11, 10);
  7. DFRobot_RGBLCD1602 lcd(0x27, 16, 2);
  8.  
  9. // Definiciones de los parametros del buzzer
  10. #define BUZZER_PIN        9
  11. #define FREQUENCIA         800
  12. #define CONFIGURAR_BUZZER  pinMode(BUZZER_PIN, OUTPUT)
  13. #define PRENDER_BUZZER     tone(BUZZER_PIN, FREQUENCIA)
  14. #define APAGAR_BUZZER      noTone(BUZZER_PIN)
  15.  
  16. // Definiciones de los Led´s delanteros y traseros
  17. #define LED_DELANTERO_PIN        4
  18. #define CONFIGURAR_LED_DELANTERO pinMode(LED_DELANTERO_PIN, OUTPUT)
  19. #define PRENDER_LED_DELANTERO    digitalWrite(LED_DELANTERO_PIN, HIGH)
  20.  
  21. #define LED_TRASERO_PIN          12
  22. #define CONFIGURAR_LED_TRASERO   pinMode(LED_TRASERO_PIN, OUTPUT)
  23. #define PRENDER_LED_TRASERO      digitalWrite(LED_TRASERO_PIN, HIGH)
  24.  
  25. // Definiciones de la lectura de distancia de los sensores
  26. #define DISTANCIA_DELANTERA     SENSOR_ULTRASONICO_DELANTERO.read()
  27. #define DISTANCIA_TRASERA       SENSOR_ULTRASONICO_TRASERO.read()
  28.  
  29. // Definicion sobre la distancia limite de los sensores
  30. #define DISTANCIA_LIMITE        20
  31.  
  32. // Definciones del LedTest
  33. #define MS_INTERVALO_LED_TEST   250 //Function LedTest()
  34. #define LED_TEST                LED_BUILTIN
  35. #define CONFIG_LED_TEST         pinMode(LED_TEST, OUTPUT)
  36. #define ACTUALIZAR_LED_TEST(x)  digitalWrite(LED_TEST, x)
  37.  
  38. // Variables globales utilizadas en el codigo
  39. bool pantallaLimpia = false;
  40. bool mostrarAlarma = false;
  41. bool mostrarDistancia = true;
  42. bool alarmaActiva = true;
  43.  
  44. unsigned long tiempoActualizacion = 0;
  45. unsigned long intervalo = 250;
  46.  
  47. //-----------------------------------------------------
  48.  
  49. void setup() {
  50.   CONFIG_LED_TEST;
  51.   ACTUALIZAR_LED_TEST(0);
  52.   CONFIGURAR_LED_DELANTERO;
  53.   CONFIGURAR_LED_TRASERO;
  54.   CONFIGURAR_BUZZER;
  55.   APAGAR_BUZZER;
  56.   lcd.init();
  57.   lcd.setCursor(0, 0);
  58.   lcd.print("Distancia1:  ");
  59.   lcd.setCursor(0, 1);
  60.   lcd.print("Distancia2:  ");
  61. }
  62.  
  63. void loop() {
  64.   LedTest();
  65.   medirDistanciaSensoresUltrasonicos();
  66.   controlarBuzzers();
  67.  
  68.   //UNIFICAR LA VISUALIZACION DEL LCD EN UNA ÚNICA FUNCIÓN, que podría llamarse ActualizaDisplay();
  69.   actualizarLCD(DISTANCIA_DELANTERA);
  70.   actualizarLCD(DISTANCIA_TRASERA);
  71.   valoresLeds();
  72. }
  73.  
  74. //-----------------------------------------------------
  75.  
  76. void LedTest() {
  77.   static int ledState = 0;
  78.   static unsigned long antMillis = 0;
  79.  
  80.   if (millis() - antMillis < MS_INTERVALO_LED_TEST) return;
  81.  
  82.   antMillis = millis();
  83.   ledState = !ledState;
  84.   ACTUALIZAR_LED_TEST(ledState);
  85. }
  86.  
  87. //-----------------------------------------------------
  88.  
  89. void medirDistanciaSensoresUltrasonicos() {
  90.   unsigned long currentTime = millis();
  91.   //tiempoActualizacion NO DEBE SER GLOBAL... DEBE SER LOCAL A LA FUNCIÓN
  92.  
  93.   if (currentTime - tiempoActualizacion >= intervalo) return;
  94.   medirDistanciaSensor1();
  95.   medirDistanciaSensor2();
  96.   verificarDistanciaSensores();
  97.   tiempoActualizacion = currentTime;
  98. }
  99.  
  100. //-----------------------------------------------------
  101.  
  102. void valoresLeds() {
  103.  
  104.   int ledDelantero = LED_DELANTERO_PIN;
  105.   int brilloLedDelantero = 255;
  106.   int intervaloDelantero = 500;
  107.   int distanciaDelantera = 1;
  108.  
  109.  
  110.   int ledTrasero = LED_TRASERO_PIN;
  111.   int brilloLedTrasero = 255;
  112.   int intervaloTrasero = 500;
  113.   int distanciaTrasera = 1;
  114.  
  115.  
  116.   parpadeoLedDelanteros(ledDelantero, brilloLedDelantero, intervaloDelantero, distanciaDelantera);
  117.   parpadeoLedTraseros(ledTrasero, brilloLedTrasero, intervaloTrasero, distanciaTrasera);
  118. }
  119.  
  120. void actualizarLCD(int valorDistancia) {
  121.   lcd.setCursor(12, 0);
  122.   lcd.print("   ");
  123.  
  124.   if (mostrarDistancia) {
  125.     lcd.setCursor(12, 0);
  126.     lcd.print(valorDistancia);
  127.   }
  128. }
  129.  
  130. void controlarBuzzers() {
  131.   unsigned long currentTime = millis();
  132.  
  133.   if (currentTime - tiempoActualizacion >= intervalo) return;
  134.   controlarBuzzerDelantero(DISTANCIA_DELANTERA, BUZZER_PIN);
  135.   controlarBuzzerTrasero(DISTANCIA_TRASERA, BUZZER_PIN);
  136.   tiempoActualizacion = currentTime;
  137. }
  138.  
  139. //-----------------------------------------------------
  140.  
  141. void controlarBuzzerDelantero(int distancia, int buzzerPin) {
  142.   //digitalWrite(buzzerPin, distancia == 0 ? HIGH : LOW);
  143.   /*
  144.   if(!distancia) ACTIVAR_BUZZER;
  145.   else          DESACTIVAR_BUZZER;
  146. }
  147. */
  148. void controlarBuzzerTrasero(int distancia, int buzzerPin) {
  149.   digitalWrite(buzzerPin, distancia == 0 ? HIGH : LOW);
  150. }
  151.  
  152. //-----------------------------------------------------
  153.  
  154. void medirDistanciaSensores(int distancia, int ledPin, int buzzerPin) {
  155.   int frequenciaBuzzer = map(distancia, 0, DISTANCIA_LIMITE, 800, 1000);
  156.   int valorPWM = map(distancia, 0, DISTANCIA_LIMITE, 255, 0);
  157.   int mapearIntervalo = map(distancia, 0, DISTANCIA_LIMITE, 255, 100);
  158.  
  159.   parpadeoLed(ledPin, valorPWM, mapearIntervalo, distancia);
  160.   actualizarLCD(distancia);
  161. }
  162.  
  163. //-----------------------------------------------------
  164. /*
  165. void medirDistancia() {
  166.   medirDistanciaSensores(DISTANCIA_DELANTERA, LED_DELANTERO_PIN, BUZZER_PIN);
  167.   medirDistanciaSensores(DISTANCIA_TRASERA, LED_TRASERO_PIN, BUZZER_PIN);
  168. }
  169. */
  170. void medirDistanciaSensor1() {
  171.   medirDistanciaSensores(DISTANCIA_DELANTERA, LED_DELANTERO_PIN, BUZZER_PIN);
  172. }
  173.  
  174. void medirDistanciaSensor2() {
  175.   medirDistanciaSensores(DISTANCIA_TRASERA, LED_TRASERO_PIN, BUZZER_PIN);
  176. }
  177.  
  178. //-----------------------------------------------------
  179.  
  180. void verificarDistanciaSensores() {
  181.  
  182.   //REMOVER DE ESTA FUNCIÓN TODO LO RELACIONADO CON LA VISUALIZACIÓN
  183.  
  184.   //REEMPLAZAR POR IF RÁPIDO para alarmaActiva
  185.   if (DISTANCIA_DELANTERA <= DISTANCIA_LIMITE || DISTANCIA_TRASERA <= DISTANCIA_LIMITE)alarmaActiva = true;
  186.   else alarmaActiva = false;
  187.  
  188.   if (!pantallaLimpia) {
  189.     lcd.clear();
  190.     pantallaLimpia = true;
  191.   }
  192.  
  193.   lcd.setCursor(0, 0);
  194.   lcd.print("Distancia1:    ");
  195.   lcd.setCursor(0, 1);
  196.   lcd.print("Distancia2:    ");
  197.  
  198.   if (!alarmaActiva) {
  199.     lcd.setCursor(12, 0);
  200.     lcd.print(DISTANCIA_DELANTERA);
  201.     lcd.setCursor(12, 1);
  202.     lcd.print(DISTANCIA_TRASERA);
  203.     APAGAR_BUZZER;
  204.  
  205.     if (mostrarAlarma) {
  206.       lcd.setCursor(1, 0);
  207.       lcd.print("                   ");
  208.       lcd.setCursor(2, 1);
  209.       lcd.print("                   ");
  210.       mostrarAlarma = false;
  211.     }
  212.   } else {
  213.     lcd.setCursor(0, 0);
  214.     lcd.print("  DETENGA SU      ");
  215.     lcd.setCursor(0, 1);
  216.     lcd.print("  VEHICULO       ");
  217.  
  218.     mostrarAlarma = true;
  219.     mostrarDistancia = false;
  220.     PRENDER_BUZZER;
  221.   }
  222. }
  223.  
  224. //-----------------------------------------------------
  225.  
  226. void parpadeoLed(int led, int brillo, int intervalo, int distancia) {
  227.  
  228.   static unsigned long previousMillis = 0;
  229.   static bool ledState = LOW;
  230.   unsigned long currentMillis = millis();
  231.  
  232.   if (currentMillis - previousMillis < intervalo) return;
  233.   previousMillis = currentMillis;
  234.   ledState = !ledState;
  235.  
  236.   distancia == 0 ? (PRENDER_LED_DELANTERO, PRENDER_LED_TRASERO) : analogWrite(led, brillo * ledState);
  237. }
  238.  
  239. void parpadeoLedDelanteros(int ledDelantero, int brilloLed, int intervalo, int distanciaDelantera) {
  240.   parpadeoLed(ledDelantero, brilloLed, intervalo, distanciaDelantera);
  241. }
  242.  
  243. void parpadeoLedTraseros(int ledTrasero, int brilloLed, int intervalo, int distanciaTrasera) {
  244.   parpadeoLed(ledTrasero, brilloLed, intervalo, distanciaTrasera);
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement