Часы на ардуино и max7219 + температура и влажность
Полезные ссылки для сборки проекта :
MAX7219 матричный модуль http://ali.pub/3rg1tg
Arduino nano http://ali.pub/3rg1vs
DHT 11 датчик температуры и влажности http://ali.pub/3rg1wk
DS1307 AT24C32 модуль часов в режиме реального времени http://ali.pub/3rg1z3
Скетч:
#include <wire.h> // Подключаем библиотеку Wire
#include <timelib.h> // Подключаем библиотеку TimeLib
#include <ds1307rtc.h> // Подключаем библиотеку DS1307RTC
#include <spi.h>
#include <adafruit_GFX.h>
#include <max72xxpanel.h>
#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 4
int t;
int h;
int a;
int b;
int c;
int d;
int e;
int f;
int pinCS = 9;
int numberOfHorizontalDisplays = 4; // теперь у нас по-горизонтали 4 матрицы
int numberOfVerticalDisplays = 1; // а по-вертикали, по-прежнему, одна
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
String tape;// текст, который будет плыть
int wait = 100; // время между крайними перемещениями букв
int spacer = 1; // расстояние между буквами
int width =5 + spacer; // размер шрифта
int chk;
String utf8rus(String source)
{
int i, k;
String target;
unsigned char n;
char m[2] = { '0', '\0' };
k = source.length(); i = 0;
while (i < k) {
n = source[i]; i++;
if (n >= 0xC0) {
switch (n) {
case 0xD0: {
n = source[i]; i++;
if (n == 0x81) { n = 0xA8; break; }
if (n >= 0x90 && n <= 0xBF) n = n + 0x30;
break;
}
case 0xD1: {
n = source[i]; i++;
if (n == 0x91) { n = 0xB8; break; }
if (n >= 0x80 && n <= 0x8F) n = n + 0x70;
break;
}
}
}
m[0] = n; target = target + String(m);
}
return target;
}
void setup() {
matrix.setIntensity(1); // яркость
matrix.setRotation( 0, 1 );
matrix.setRotation( 1, 1 );
matrix.setRotation( 2, 1 );
matrix.setRotation( 3, 1 );
chk = DHT.read(DHT11_PIN);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
void loop() {
tmElements_t tm;
RTC.read™;
t = DHT.temperature;
h = DHT.humidity;
a = tm.Hour;
b = tm.Minute;
c = tm.Second;
d = tm.Day;
e = tm.Month;
f = tmYearToCalendar(tm.Year);
String t1 = String(t, DEC);
String h1 = String(h, DEC);
String a1 = String(a, DEC);
String b1 = String(b, DEC);
String c1 = String(c, DEC);
String d1 = String(d, DEC);
String e1 = String(e, DEC);
String f1 = String(f, DEC);
//tape = (utf8rus(a + ":" + b + ":" + c));
tape = (utf8rus("Время " + a1 + ":" + b1 + ":" + c1 + " Дата " + d1 + "."+ e1 + "." + f1 + " Температура " + t1 + "'C Влажность " + h1 + "%"));
for ( int i = 0 ; i < width * tape.length() + matrix.width() — 1 — spacer; i++ )
{
matrix.fillScreen(LOW);
int letter = i / width;
int x = (matrix.width() — 1) — i % width;
int y = (matrix.height() — 8) / 2; // center the text vertically
while ( x + width — spacer >= 0 && letter >= 0 ) {
if ( letter < tape.length() ) {
matrix.drawChar(x, y, tape[letter], HIGH, LOW, 1);
}
letter--;
x -= width;
}
matrix.write();
delay(wait);
}
