#include <EEPROM.h>
#include <SimpleDHT.h>
// Definición de pines
const int buttonStart = 2;
const int buttonStop = 3;
const int buttonTemp = 4;
const int buttonSound = 5;
const int buttonLight = 6;
const int soundSensorPin = A1;
const int lightSensorPin = A2;
int pinDHT11 = 7;
SimpleDHT11 dht11(pinDHT11);
// Variables de estado
bool measureTemp = false;
bool measureSound = false;
bool measureLight = false;
bool measuring = false;
unsigned long startTime;
unsigned long endTime;
// Variables para almacenar los resultados finales
float finalTempValue = 0;
float finalSoundValue = 0;
float finalLightValue = 0;
// Información del empleado
String empleado;
String puesto;
String operacion;
// Almacenamiento de datos en EEPROM
struct EmployeeData {
char name[30];
float lastTime;
int measurements;
};
EmployeeData currentEmployee;
int eepromAddress = 0;
void setup() {
Serial.begin(9600);
// Configuración de pines de botones como entrada con resistencias pull-up internas
pinMode(buttonStart, INPUT_PULLUP);
pinMode(buttonStop, INPUT_PULLUP);
pinMode(buttonTemp, INPUT_PULLUP);
pinMode(buttonSound, INPUT_PULLUP);
pinMode(buttonLight, INPUT_PULLUP);
// Mensaje de inicio
Serial.println("Bienvenido al sistema de mediciones de condiciones adversas");
// Solicitar información del empleado
Serial.print("Empleado: ");
while (Serial.available() == 0) {}
empleado = Serial.readString();
empleado.trim();
empleado.toCharArray(currentEmployee.name, 30);
Serial.print("Puesto: ");
while (Serial.available() == 0) {}
puesto = Serial.readString();
puesto.trim();
Serial.print("Operación: ");
while (Serial.available() == 0) {}
operacion = Serial.readString();
operacion.trim();
// Mensaje para seleccionar sensores
Serial.println("Seleccione los sensores a utilizar y presione el botón de arranque.");
// Leer datos anteriores del empleado desde EEPROM
bool found = false;
for (int i = 0; i < EEPROM.length(); i += sizeof(EmployeeData)) {
EmployeeData storedEmployee;
EEPROM.get(i, storedEmployee);
if (String(storedEmployee.name) == empleado) {
currentEmployee.lastTime = storedEmployee.lastTime;
currentEmployee.measurements = storedEmployee.measurements;
found = true;
eepromAddress = i;
break;
}
}
if (!found) {
currentEmployee.lastTime = -1;
currentEmployee.measurements = 0;
eepromAddress = EEPROM.length() - sizeof(EmployeeData);
}
}
void loop() {
// Leer el estado actual de los botones
bool readingTemp = digitalRead(buttonTemp);
bool readingSound = digitalRead(buttonSound);
bool readingLight = digitalRead(buttonLight);
// Manejar el botón de temperatura
if (readingTemp == LOW) {
measureTemp = !measureTemp;
Serial.print("Sensor de temperatura ");
Serial.println(measureTemp ? "seleccionado" : "deseleccionado");
delay(200); // Simple debounce
}
// Manejar el botón de sonido
if (readingSound == LOW) {
measureSound = !measureSound;
Serial.print("Sensor de sonido ");
Serial.println(measureSound ? "seleccionado" : "deseleccionado");
delay(200); // Simple debounce
}
// Manejar el botón de luz
if (readingLight == LOW) {
measureLight = !measureLight;
Serial.print("Sensor de luz ");
Serial.println(measureLight ? "seleccionado" : "deseleccionado");
delay(200); // Simple debounce
}
// Iniciar medición
if (digitalRead(buttonStart) == LOW && !measuring) {
measuring = true;
startTime = millis();
Serial.println("Medición iniciada...");
}
// Parar medición
if (digitalRead(buttonStop) == LOW && measuring) {
measuring = false;
endTime = millis();
Serial.println("Medición detenida.");
// Calcular tiempo total de medición
float totalTime = (endTime - startTime) / 1000.0;
float percentageChange = 0;
String performance;
if (currentEmployee.measurements > 0) {
percentageChange = ((totalTime - currentEmployee.lastTime) / currentEmployee.lastTime) * 100;
if (percentageChange > 0) {
performance = String(percentageChange) + "% mayor";
} else {
performance = String(-percentageChange) + "% menor";
}
} else {
performance = "N/A (primera medición)";
}
// Guardar el tiempo actual y aumentar el contador de mediciones en EEPROM
currentEmployee.lastTime = totalTime;
currentEmployee.measurements += 1;
EEPROM.put(eepromAddress, currentEmployee);
// Convertir valores de sensores a unidades adecuadas
if (measureTemp) {
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err=");
Serial.println(err);
} else {
finalTempValue = temperature;
}
}
if (measureSound) {
finalSoundValue = analogRead(soundSensorPin) * (5.0 / 1023.0) * 100; // Simulación de conversión a dB
}
if (measureLight) {
finalLightValue = analogRead(lightSensorPin) / 10.23; // Convertir a %
}
// Mostrar resultados finales
String resultados = "El empleado " + empleado + " con el puesto " + puesto + " y operación " + operacion + " tardó un total de " + String(totalTime, 2) + " segundos";
if (measureTemp) {
resultados += " con una temperatura de " + String(finalTempValue, 2) + "°C";
}
if (measureLight) {
resultados += ", luz de " + String(finalLightValue, 2) + "%";
}
if (measureSound) {
resultados += " y un sonido de " + String(finalSoundValue, 2) + " dB";
}
resultados += ", su rendimiento fue " + performance + " que la vez anterior.";
Serial.println(resultados);
delay(200); // Debounce
}
// Realizar mediciones si está en modo de medición
if (measuring) {
unsigned long currentTime = millis();
Serial.print("Tiempo transcurrido: ");
Serial.print((currentTime - startTime) / 1000); // Mostrar tiempo transcurrido en segundos sin decimales
Serial.println(" segundos");
if (measureTemp) {
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err=");
Serial.println(err);
} else {
finalTempValue = temperature;
Serial.print("Temperatura: ");
Serial.println(finalTempValue);
}
}
if (measureSound) {
finalSoundValue = analogRead(soundSensorPin) * (5.0 / 1023.0) * 100; // Simulación de conversión a dB
Serial.print("Sonido: ");
Serial.println(finalSoundValue);
}
if (measureLight) {
finalLightValue = analogRead(lightSensorPin) / 10.23; // Convertir a %
Serial.print("Luz: ");
Serial.println(finalLightValue);
}
delay(1000); // Ajusta el intervalo de medición según sea necesario
}
}