0% found this document useful (0 votes)
5 views7 pages

Arduino LED Matrix Clock & Sensor Code

The document is a code for an Arduino project that integrates WiFi connectivity, LED control, and temperature sensing using an HTU21DF sensor. It features a NeoMatrix display to show time, temperature, or a smiley icon, with adjustable brightness and color settings controlled by buttons. The setup includes hardware configurations, button checks, and a main loop that updates the display based on the selected mode.

Uploaded by

cristian marin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Arduino LED Matrix Clock & Sensor Code

The document is a code for an Arduino project that integrates WiFi connectivity, LED control, and temperature sensing using an HTU21DF sensor. It features a NeoMatrix display to show time, temperature, or a smiley icon, with adjustable brightness and color settings controlled by buttons. The setup includes hardware configurations, button checks, and a main loop that updates the display based on the selected mode.

Uploaded by

cristian marin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <WiFi.

h>

#include <FastLED.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_NeoPixel.h> // 🔹 Necesario para NEO_GRB y


NEO_KHZ800

#include <FastLED_NeoMatrix.h>

#include <NTPClient.h>

#include <WiFiUdp.h>

#include <Adafruit_Sensor.h>

#include <Adafruit_HTU21DF.h>

// ===== CONFIGURACIÓN DE HARDWARE =====

#define LED_PIN 13

#define WIDTH 32

#define HEIGHT 8

#define NUM_LEDS (WIDTH * HEIGHT)

#define LED_TYPE WS2812B

#define COLOR_ORDER GRB

#define INIT_BRIGHT 100

#define LDR_PIN 34

#define BUTTON1_PIN 12

#define BUTTON2_PIN 14

#define BUTTON3_PIN 27

CRGB leds[NUM_LEDS];
FastLED_NeoMatrix matrix = FastLED_NeoMatrix(

leds, WIDTH, HEIGHT,

NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS +


NEO_MATRIX_ZIGZAG,

NEO_GRB + NEO_KHZ800 // 🔹 Constantes definidas por


Adafruit_NeoPixel

);

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP, "[Link]", -18000, 60000); // UTC-5


Colombia

uint8_t mode = 0; // 0 = hora, 1 = temperatura, 2 = icono

uint8_t colorIndex = 0;

uint8_t brightness = INIT_BRIGHT;

// Icono de ejemplo (carita feliz)

const uint8_t smile[8] = {

0b00111100,

0b01000010,

0b10100101,

0b10000001,

0b10100101,

0b10011001,

0b01000010,

0b00111100

};
// ===== FUNCIONES =====

uint16_t XY(uint8_t x, uint8_t y) {

if (y % 2 == 0) return y * WIDTH + x;

else return y * WIDTH + (WIDTH - 1 - x);

void drawIcon(const uint8_t *icon, uint8_t x, uint8_t y, CRGB color) {

for (uint8_t i = 0; i < 8; i++) {

for (uint8_t j = 0; j < 8; j++) {

if (icon[i] & (1 << (7 - j))) {

[Link](x + j, y + i, color);

void checkButtons() {

if (!digitalRead(BUTTON1_PIN)) {

mode = (mode + 1) % 3;

delay(300);

if (!digitalRead(BUTTON2_PIN)) {

colorIndex = (colorIndex + 1) % 7;

delay(300);

if (!digitalRead(BUTTON3_PIN)) {
brightness = brightness > 40 ? brightness - 40 : 255;

[Link](brightness);

delay(300);

CRGB getColorByIndex(uint8_t i) {

switch (i) {

case 0: return CRGB::White;

case 1: return CRGB::Blue;

case 2: return CRGB::Red;

case 3: return CRGB::Green;

case 4: return CRGB::Orange;

case 5: return CRGB::Purple;

case 6: return CRGB::Yellow;

default: return CRGB::White;

// Función para convertir de CRGB (FastLED) a formato 16 bits para


Adafruit_GFX

uint16_t color565(uint8_t r, uint8_t g, uint8_t b) {

return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);

// ===== SETUP =====

void setup() {
[Link](115200);

pinMode(BUTTON1_PIN, INPUT);

pinMode(BUTTON2_PIN, INPUT);

pinMode(BUTTON3_PIN, INPUT);

pinMode(LDR_PIN, INPUT);

[Link]<LED_TYPE, LED_PIN, COLOR_ORDER>(leds,


NUM_LEDS);

[Link](INIT_BRIGHT);

[Link]();

[Link](false);

[Link](INIT_BRIGHT);

if (![Link]()) {

[Link]("⚠️Sensor HTU21D no detectado");

[Link]("FamiliaGomez", "D10546674g"); // ✅ Cambia por tu red


WiFi

while ([Link]() != WL_CONNECTED) {

delay(500);

[Link](".");

[Link]("\n✅ Conectado a WiFi");

[Link]();

}
// ===== LOOP PRINCIPAL =====

void loop() {

checkButtons();

[Link]();

int ldrValue = analogRead(LDR_PIN);

brightness = map(ldrValue, 0, 4095, 40, 255);

[Link](brightness);

[Link](0);

CRGB color = getColorByIndex(colorIndex);

[Link](color565(color.r, color.g, color.b));

// ✅ conversión CRGB → 16 bits

if (mode == 0) {

char buf[6];

sprintf(buf, "%02d:%02d", [Link](),


[Link]());

[Link](0, 0);

[Link](buf);

} else if (mode == 1) {

float t = [Link]();

char tempBuf[10];

sprintf(tempBuf, "%.1fC", t);

[Link](0, 0);

[Link](tempBuf);
} else if (mode == 2) {

drawIcon(smile, 12, 0, color);

[Link]();

delay(1000);

You might also like