Created
May 1, 2022 10:20
-
-
Save azaharafernandezguizan/7c1b55daaab25f8b0bf51e95b7e1245f to your computer and use it in GitHub Desktop.
Ejemplo estación metereológica muy simple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
#include <Wire.h> | |
#include "MAX30105.h" | |
#include "spo2_algorithm.h" | |
/* Configuracion leds*/ | |
int ledOnOFf = 48; | |
int ledGreen_AirSensor = 30; | |
int ledYellow_AirSensor = 32; | |
int ledRed_AirSensor = 34; | |
int ledGreen_PhotoSensor = 36; | |
int ledYellow_PhotoSensor = 38; | |
int ledRed_PhotoSensor = 40; | |
int ledGreen_SPO2Sensor = 42; | |
int ledYellow_SPO2Sensor = 44; | |
int ledRed_SPO2Sensor = 46; | |
/* Configuracion Sensores */ | |
int airSensorDig = 10; | |
int airSensorAnalog = 3; | |
int ppm_AirSensor = 0; | |
float tension_AirSensor = 0; | |
bool state_AirSensor = false; | |
String resultAirSensorPPM = ""; | |
int photoSensorDig = 9; | |
int photoSensorAnalog = 0; | |
String resultPhotoSensor = ""; | |
/* Configuracion LCD */ | |
const int rs = 8, en = 11, d4 = 4, d5 = 5, d6 = 6, d7 = 7; | |
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); | |
int InitialPositionMoveDisplay = 16; | |
int PositionsToMoveDisplay = 0; | |
/* Otras variables*/ | |
long bps = 9600; | |
int aplicationDelay = 500; | |
void setup() | |
{ | |
// Led onoff que indica si está funcionando | |
pinMode(ledOnOFf, OUTPUT); | |
// Pins que indiquen cómo van los sensores, sin falta de la pantalla | |
pinMode(ledGreen_AirSensor, OUTPUT); | |
pinMode(ledYellow_AirSensor, OUTPUT); | |
pinMode(ledRed_AirSensor, OUTPUT); | |
pinMode(ledGreen_PhotoSensor, OUTPUT); | |
pinMode(ledYellow_PhotoSensor, OUTPUT); | |
pinMode(ledRed_PhotoSensor, OUTPUT); | |
pinMode(ledGreen_SPO2Sensor, OUTPUT); | |
pinMode(ledYellow_SPO2Sensor, OUTPUT); | |
pinMode(ledRed_SPO2Sensor, OUTPUT); | |
isFirstIteration = true; | |
bufferLength = 50; //buffer length of 100 stores 4 seconds of samples running at 25sps | |
// lcd parameters -> hay que decirle cuántas columnas (16) y filas (2) tiene. | |
lcd.begin(16, 2); | |
} | |
void loop() | |
{ | |
digitalWrite(ledOnOFf, HIGH); //encendemos el led | |
AirSensorControl(); //hacemos la lógica de recoger datos del sensor de aire | |
PhotoSensorControl(); // hacemos la lógica de recoger datos del sensor de luz | |
showData(); //escribimos los datos en la pantalla | |
delay(aplicationDelay); | |
} | |
void AirSensorControl() | |
{ | |
state_AirSensor = digitalRead(airSensorDig); //leemos con la parte digital del sensor | |
ppm_AirSensor = analogRead(airSensorAnalog); //leemos con la parte analógica del sensor | |
tension_AirSensor = ppm_AirSensor * (5.0 / 1023.0); //con esta fórmula se calibran los datos | |
resultAirSensorPPM = ""; | |
resultAirSensorPPM.concat(ppm_AirSensor); //pasamos a string el valor para usarlo luego en la pantalla | |
//estos datos los decidimos nosotros, pero usa los que veas | |
if (!state_AirSensor || tension_AirSensor >= 700) | |
{ | |
//nivel rojo | |
digitalWrite(ledGreen_AirSensor, LOW); | |
digitalWrite(ledYellow_AirSensor, LOW); | |
digitalWrite(ledRed_AirSensor, HIGH); | |
} | |
else if (tension_AirSensor >= 100 && tension_AirSensor < 700) | |
{ | |
//nivel amarillo | |
digitalWrite(ledGreen_AirSensor, LOW); | |
digitalWrite(ledYellow_AirSensor, HIGH); | |
digitalWrite(ledRed_AirSensor, LOW); | |
} | |
else if (tension_AirSensor > 0 && tension_AirSensor < 100) | |
{ | |
//nivel verde | |
digitalWrite(ledGreen_AirSensor, HIGH); | |
digitalWrite(ledYellow_AirSensor, LOW); | |
digitalWrite(ledRed_AirSensor, LOW); | |
} | |
else | |
{ | |
//todos los leds encendidos porque está estropeado o no funciona | |
digitalWrite(ledGreen_AirSensor, HIGH); | |
digitalWrite(ledYellow_AirSensor, HIGH); | |
digitalWrite(ledRed_AirSensor, HIGH); | |
} | |
} | |
void PhotoSensorControl() | |
{ | |
int sensorValue = analogRead(photoSensorAnalog); //leemos analógicamente | |
float photoVoltage = sensorValue * (5.0 / 1024.0); // con esta fórmula se calibra | |
//de nuevo estos valores los decidimos nosotros, elige los tuyos | |
if (photoVoltage >= 0 && photoVoltage <= 1) | |
{ | |
//verde | |
digitalWrite(ledGreen_PhotoSensor, HIGH); | |
digitalWrite(ledYellow_PhotoSensor, LOW); | |
digitalWrite(ledRed_PhotoSensor, LOW); | |
} | |
else if (photoVoltage > 1 && photoVoltage <= 2) | |
{ | |
//amarillo | |
digitalWrite(ledGreen_PhotoSensor, LOW); | |
digitalWrite(ledYellow_PhotoSensor, HIGH); | |
digitalWrite(ledRed_PhotoSensor, LOW); | |
} | |
else if (photoVoltage > 2) | |
{ | |
//rojo | |
digitalWrite(ledGreen_PhotoSensor, LOW); | |
digitalWrite(ledYellow_PhotoSensor, LOW); | |
digitalWrite(ledRed_PhotoSensor, HIGH); | |
} | |
else | |
{ | |
//estropeado o no funciona | |
digitalWrite(ledGreen_PhotoSensor, HIGH); | |
digitalWrite(ledYellow_PhotoSensor, HIGH); | |
digitalWrite(ledRed_PhotoSensor, HIGH); | |
} | |
resultPhotoSensor = ""; | |
resultPhotoSensor.concat(photoVoltage); //valor como string para la pantalla | |
} | |
void showData() | |
{ | |
String dataToShow = resultAirSensorPPM + " " + resultPhotoSensor; //ponemos los datos a mostrar en un string | |
lcd.clear(); | |
writeDataInLcd(0, 0, "LIGHT SPO2"); // ponemos una cabecera para entender los datos en la columna 0 y la fila 0 | |
writeDataInLcd(0, 1, dataToShow); // los datos los mostramos en la columna 0 y la fila 1 | |
} | |
void writeDataInLcd(int columnPosition, int rowPosition, String textToWrite) | |
{ | |
lcd.setCursor(columnPosition, rowPosition); //nos posicionamos | |
lcd.print(textToWrite); //"escribimos" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment