Need some help in a project with some sensors and LCD

Hello colleagues, I am new to this and I need to make a program that works with several types of sensors (DHT11, DS3231, ACS712, GL55 and a potentiometer) and display the data on an LCD screen. I have the programs separately and they work, but I can't get them to work together, I think because the loops are not well defined. I attach my code so that you can take a look at it and you can help me in the failures that you see, thank you very much. Ignore the texts in spanish :slight_smile:

#include <Wire.h>
#include "RTClib.h" //SCL(A5), SDA(A4), Vcc 5V, 
#include <LiquidCrystal.h>
#include <DHT.h>

// Definimos el pin digital donde se conecta el sensor y el tipo de sensor
#define DHTPIN 6
#define DHTTYPE DHT11

LiquidCrystal lcd(12,11,5,4,3,2); // pines donde se conecta el lcd
RTC_DS3231 rtc;
DHT dht(DHTPIN, DHTTYPE); // Inicializamos el sensor DHT11

//se definen distintos tipos de variables del programa
String daysOfTheWeek[7] = { "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab" };
String monthsNames[12] = { "Enero", "Febrero", "Marzo", "Abril", "Mayo",  "Junio", "Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre" };

float sensibilidad = 0.185;   // Sensibilidad del ACS712 en V/A, modelo 5A
float humedad;
float celsius;
float voltaje;
float intensidad;
float luz;
unsigned char seg=0;
unsigned char min=0;
unsigned char hor=0;
unsigned char date=0;
unsigned char month=0;
float year=0;

void setup() {
   Serial.begin(9600);
   lcd.begin(16, 2); // Configuramos las columnas y filas del LCD 16x2
   delay(1000); 
   dht.begin(); // Comenzamos el sensor DHT
   lcd.setCursor(0, 0);
   lcd.print("¡Hola!");
   delay(5000);
   lcd.clear();
   }

void loop() {
 loop Reloj(); 
 loop Voltaje();
 loop Intensidad();
 loop Luz();
 loop Temperatura();
 loop LCD();
 } 

void loop LCD() {
  
   {//Escribimos la linea superior, la temperatura
   lcd.setCursor(0,0);
   lcd.print("Temperatura");
   lcd.setCursor(13,0);
   lcd.print(celsius);
   lcd.setCursor(15,0);
   lcd.print("ºC");
   //Escribimos la linea inferior, la humedad
   lcd.setCursor(0,1);
   lcd.print("Humedad");
   lcd.setCursor(9,0);
   lcd.print(celsius);
   lcd.setCursor(12,0);
   lcd.print("%\t");
   delay(2000); // 2 segundos hasta la siguiente medida.
   lcd.clear(); //limpiamos la pantalla del LCD
   }
   
   {//Escribimos la linea superior de la siguiente medida, el voltaje
   lcd.setCursor(0,0);
   lcd.print("Voltaje");
   lcd.setCursor(9,0);
   lcd.print(voltaje, 2);
   lcd.setCursor(13,0);
   lcd.print("V");
   //Escribimos la linea inferior, la intensidad
   lcd.setCursor(0,1);
   lcd.print("Intensidad");
   lcd.setCursor(12,0);
   lcd.print(intensidad, 2);
   lcd.setCursor(16,0);
   lcd.print("I");
   delay(2000); // 2 segundos hasta la siguiente medida.
   lcd.clear(); //limpiamos la pantalla del LCD
   }
   
   {//Escribimos la linea superior, el nivel de luz
   lcd.setCursor(0,0);
   lcd.print("Nivel Luz");
   lcd.setCursor(11,0);
   lcd.print(luz);
   lcd.setCursor(13,0);
   lcd.print("%");
   delay(2000); // 2 segundos hasta la siguiente medida.
   lcd.clear(); //limpiamos la pantalla del LCD
   }
  /* 
   {//Escribimos la linea superior, el dia de la semana
   lcd.setCursor(0,0);
   lcd.print(date.year(), DEC);
   lcd.setCursor(5,0);
   lcd.print('/');
   lcd.setCursor(6,0);
   lcd.print(date.month(), DEC);
   lcd.setCursor(8,0);
   lcd.print('/');
   lcd.setCursor(9,0);
   lcd.print(date.day(), DEC);
   lcd.setCursor(13,0);
   lcd.print(daysOfTheWeek[date.dayOfTheWeek()]);
   //Escribimos la linea inferior, la hora
   lcd.setCursor(0,1);
   lcd.print(date.hour(), DEC);
   lcd.setCursor(3,0);
   lcd.print(':');
   lcd.setCursor(4,0);
   lcd.print(date.minute(), DEC);
   lcd.setCursor(6,0);
   lcd.print(':');
   lcd.setCursor(7,0);
   lcd.print(date.second(), DEC);
   delay(2000); // 2 segundos hasta la siguiente medida.
   lcd.clear(); //limpiamos la pantalla del LCD
   } 
}

void loopReloj() {
   // Obtener fecha actual
   DateTime now = rtc.now();
   printDate(now);
   delay(2000);
}   */

loop Voltaje() {

  float sensorValue = analogRead(A1); // lee el input del sensor
  float voltaje = sensorValue*5/1023; // convierte el valor a voltaje decimal
  
  delay(2000);
  }

loop Intensidad() {
  
  float intensidad=get_corriente(200);//obtenemos la corriente promedio de 200 muestras 
  delay(2000);
   
}

float get_corriente(int n_muestras)
{
  float voltajeSensor;
  float corriente=0;
  for(int i=0;i<n_muestras;i++)
  {
    voltajeSensor = analogRead(A2) * (5.0 / 1023.0);////lectura del sensor
    corriente=corriente+(voltajeSensor-2.5)/sensibilidad; //Ecuación  para obtener la corriente
  }
  corriente=corriente/n_muestras;
  return(corriente);     
}

loop Luz() {

  int luz = map(analogRead(A0), 1023 ,0, 100 ,0); 
  // leemos el valor de la entrada analogica donde tenemos el LDR y lo guardamos en la variable luz

   delay(2000);
  }

loop Temperatura() {
 
  float humedad = dht.readHumidity(); // Leemos la humedad relativa
  float celsius = dht.readTemperature(); // Leemos la temperatura en grados celsius (por defecto)

  delay(2000);// Esperamos 2 segundos entre medidas
  }


Too many loops. You only need the one. Get rid of the LOOP statements except for the first one.

1 Like

That is not right either. Lose the loop.

Here is a tutorial showing the syntax for defining and calling functions. None of them have a return type of loop.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.