#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(-1);
#define DIS_WIDTH 128 // OLED display width, in pixels
#define DIS_HEIGHT 64 // OLED display height, in pixels
#define IMG_HEIGHT 32
#define IMG_WIDTH 32
int NTC = 34; // Pin analógico donde está conectado la NTC
int LDR = 35; // Pin analógico donde está conectado la LDR
int led1 = 19; // Pin de led rojo
int led2 = 18; // Pin de led azul
int NTC_value;
int LDR_value;
const float BETA = 3950;
const float GAMMA = 0.7;
const float RL10 = 50;
void setup(){
Serial.begin(9600);
pinMode(NTC, INPUT);
pinMode(LDR, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
// Inicializar el display proporcionando el tipo de display y su dirección I2C.
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 está fallando"));
for (;;); // No proceder, hacer un bucle infinito
}
}
void loop()
{
NTC_value = analogRead(NTC); // Leer el valor de NTC
float celsius = 1 / (log(1 / (4095. / NTC_value - 1)) / BETA + 1.0 / 298.15) - 273.15;
LDR_value = analogRead(LDR); // Leer el valor de LDR
float voltage = LDR_value / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
int pwm1 = map(celsius, -24, 80, 0, 255); // PWM resolución 8 bit 0 - 255
int pwm2 = map(lux, 0.1, 100000, 0, 255); // PWM resolución 8 bit 0 - 255
analogWrite(led1, pwm1);
analogWrite(led2, pwm2);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Velocidad 1: ");
display.println(celsius);
display.println("Velocidad 2: ");
display.println(lux);
display.display();
// Esperar unos segundos antes de continuar mostrando la velocidad
delay(100);
}