temperatura 2 — Arduino Uno simulation

An interactive Arduino Uno circuit simulation you can run free in your browser on Velxio, by dave.

Components

Sketch code

// Reloj con menú - RTC DS1307 + DHT22 + BMP280 + OLED SSD1306
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_BMP280.h>
#include <RTClib.h>
#include <DHT.h>

#define ANCHO_PANTALLA 128
#define ALTO_PANTALLA   64
#define DIR_OLED      0x3C
#define PIN_RESET     -1

// DHT22
#define PIN_DHT 7
#define DHTTYPE DHT22

// Botones (con pull-up interno)
#define PIN_MENU  8
#define PIN_UP    9
#define PIN_DOWN  10
#define PIN_OK    11

Adafruit_SSD1306 display(ANCHO_PANTALLA, ALTO_PANTALLA, &Wire, PIN_RESET);
Adafruit_BMP280 bmp;
RTC_DS1307 rtc;
DHT dht(PIN_DHT, DHTTYPE);

// Menú
enum Pantalla { P_RELOJ, P_TEMP_HUM, P_BMP, P_AJUSTE };
Pantalla pantalla = P_RELOJ;
int opcionMenu = 0;
const char* itemsMenu[] = {"Reloj", "Temp/Hum", "BMP280", "Ajustar Hora"};
const int numItems = 4;
bool enMenu = false;

// Ajuste de hora
int ajusteCampo = 0; // 0=seg,1=min,2=hora,3=dia,4=mes,5=anio
DateTime fechaAjuste;

unsigned long ultimoBoton = 0;
const unsigned long debounce = 250;

// Buffer para el reloj (parpadeo de segundos)
bool mostrarSegundos = true;
unsigned long ultimoTick = 0;

void setup() {
  Serial.begin(9600);

  // Inicializar OLED
  if (!display.begin(SSD1306_SWITCHCAPVCC, DIR_OLED)) {
    Serial.println(F("Error: No se encuentra OLED SSD1306"));
    for (;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.cp437(true);

  // Inicializar BMP280
  if (!bmp.begin(0x76)) {
    Serial.println(F("Error: No se encuentra BMP280"));
    for (;;);
  }
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
                  Adafruit_BMP280::SAMPLING_X2,
                  Adafruit_BMP280::SAMPLING_X16,
                  Adafruit_BMP280::FILTER_X16,
                  Adafruit_BMP280::STANDBY_MS_500);

  // Inicializar RTC DS1307
  if (!rtc.begin()) {
    Serial.println(F("Error: No se encuentra DS1307"));
    for (;;);
  }
  if (!rtc.isrunning()) {
    Serial.println(F("RTC no está funcionando. Ajustando a la compilación."));
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  // Inicializar DHT22
  dht.begin();

  // Botones con pull-up interno
  pinMode(PIN_MENU, INPUT_PULLUP);
  pinMode(PIN_UP,   INPUT_PULLUP);
  pinMode(PIN_DOWN, INPUT_PULLUP);
  pinMode(PIN_OK,   INPUT_PULLUP);

  splashScreen();
  delay(1500);
}

void splashScreen() {
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(8, 10);
  display.print(F("RELOJ"));
  display.setTextSize(1);
  display.setCursor(4, 40);
  display.print(F("Temp/Hum + Men\u00fa"));
  display.display();
}

void loop() {
  // Leer botones
  bool btnMenu = (digitalRead(PIN_MENU) == LOW);
  bool btnUp   = (digitalRead(PIN_UP)   == LOW);
  bool btnDown = (digitalRead(PIN_DOWN) == LOW);
  bool btnOk   = (digitalRead(PIN_OK)   == LOW);

  unsigned long ahora = millis();
  if ((btnMenu || btnUp || btnDown || btnOk) && (ahora - ultimoBoton > debounce)) {
    ultimoBoton = ahora;

    if (btnMenu) {
      enMen

More projects by dave