Код: Выделить всё
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <Wire.h>
#include <iarduino_RTC.h>
iarduino_RTC time(RTC_DS3231); //выбор чипа часов реального времени
int pinCS = 10; // пин подключения контакта CS на дисплее
int numberOfHorizontalDisplays = 1; //количество секций дисплея по вертикали
int numberOfVerticalDisplays = 4; //количество секций дисплея по горизонтали
int brightness = 10; // Переменная яркости дисплея при запуске часов (от 0 до 15)
uint8_t PIN_button_SET = 7; // пин кнопки выбора
uint8_t PIN_button_UP = 2; // пин кнопки вверх
uint8_t PIN_button_DOWN = 6;// пин кнопки вниз
uint8_t VAR_mode_SHOW = 1;
uint8_t VAR_mode_SET = 0;
int spacer = 1; //ширина пробела в пиклелях
int width = 5 + spacer; //ширина символа в пиклелях
int flag = 0;
uint32_t timing = 0;
String runningLane = "One moment loading";
int wait = 20; // In milliseconds
int period = 1000;
String tape = "1";
String plus = " +";
String c = "C";
#define ONE_WIRE_BUS 9 // пин подключения датчика температуры DS18B20
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays); // инициализация дисплея
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
pinMode (A7, INPUT); //аналоговый пин датчика освещенности
time.begin();
sensors.begin();
pinMode (PIN_button_SET, INPUT_PULLUP);
pinMode (PIN_button_UP, INPUT_PULLUP);
pinMode (PIN_button_DOWN, INPUT_PULLUP);
matrix.setIntensity(brightness); // Use a value between 0 and 15 for brightness
matrix.setRotation(1); //ориентация екрана
for ( int i = 0 ; i < width * runningLane.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 < runningLane.length() ) {
matrix.drawChar(x, y, runningLane[letter], HIGH, LOW, 1);
}
letter--;
x -= width;
}
matrix.write(); // Send bitmap to display
delay(wait);
}
//watch.settime(0,00,23,9,04,20,2); //установка времени (после установки закоментировать и прошить заново)
}
void write(String str) {
matrix.fillScreen(LOW);
for (int i = 0; i < str.length(); i++) {
matrix.drawChar(i * width + 1, 0, str[i], HIGH, LOW, 1);
}
matrix.write();
}
void loop() {
if (VAR_mode_SHOW == 1) {
if (time.seconds % 2 != 0) {
tape = String (time.gettime("H" ":" "i")); // моргалка on
} else {
tape = String (time.gettime("H" " " "i")); // моргалка off
}
}
if (VAR_mode_SHOW == 2) {
tape = String (time.gettime("d" "." "m")); // точка на дате
}
delay(1);
Func_buttons_control();
temperature ();
write(tape);
ledBrightness ();
}
/*
Обработка нажатия кнопок
*/
void Func_buttons_control() {
uint8_t i = 0;
time.blinktime(VAR_mode_SET);
if (VAR_mode_SET) {
if (digitalRead(PIN_button_UP) == LOW) {
while (digitalRead(PIN_button_UP) == LOW) {
delay(50);
}
switch (VAR_mode_SET) {
/* сек */ case 1: time.settime(-1, (time.seconds == 59 ? 0 : time.seconds = 0) - 1, -1, -1, -1, -1, -1); break;
/* мин */ case 2: time.settime(-1, (time.minutes == 59 ? 0 : time.minutes + 1), -1, -1, -1, -1, -1); break;
/* час */ case 3: time.settime(-1, -1, (time.Hours == 23 ? 0 : time.Hours + 1), -1, -1, -1, -1); break;
/* дни */ case 4: time.settime(-1, -1, -1, (time.day == 31 ? 1 : time.day + 1), -1, -1, -1); break;
/* мес */ case 5: time.settime(-1, -1, -1, -1, (time.month == 12 ? 1 : time.month + 1), -1, -1); break;
/* год */ case 6: time.settime(-1, -1, -1, -1, -1, (time.year == 99 ? 0 : time.year + 1), -1); break;
/* д.н.*/ case 7: time.settime(-1, -1, -1, -1, -1, -1, (time.weekday == 6 ? 0 : time.weekday + 1) ); break;
}
}
if (digitalRead(PIN_button_DOWN) == LOW) {
while (digitalRead(PIN_button_DOWN) == LOW) {
delay(50);
}
switch (VAR_mode_SET) {
/* сек */ case 1: time.settime(-1, (time.seconds == 0 ? 59 : time.seconds = 0 ) - 1, -1, -1, -1, -1, -1); break;
/* мин */ case 2: time.settime(-1, (time.minutes == 0 ? 59 : time.minutes - 1), -1, -1, -1, -1, -1); break;
/* час */ case 3: time.settime(-1, -1, (time.Hours == 0 ? 23 : time.Hours - 1), -1, -1, -1, -1); break;
/* дни */ case 4: time.settime(-1, -1, -1, (time.day == 1 ? 31 : time.day - 1), -1, -1, -1); break;
/* мес */ case 5: time.settime(-1, -1, -1, -1, (time.month == 1 ? 12 : time.month - 1), -1, -1); break;
/* год */ case 6: time.settime(-1, -1, -1, -1, -1, (time.year == 0 ? 99 : time.year - 1), -1); break;
/* д.н.*/ case 7: time.settime(-1, -1, -1, -1, -1, -1, (time.weekday == 0 ? 6 : time.weekday - 1) ); break;
}
}
if (digitalRead(PIN_button_SET) == LOW) {
while (digitalRead(PIN_button_SET) == LOW) {
delay(10);
if (i < 200) {
i++;
} else {
matrix.fillScreen(LOW);
}
}
if (i < 200) { // если кнопка SET удерживалась меньше 2 секунд
VAR_mode_SET++; // переходим к следующему устанавливаемому параметру
if (VAR_mode_SHOW == 1 && VAR_mode_SET > 3) {
VAR_mode_SET = 1; // возвращаемся к первому устанавливаемому параметру
}
if (VAR_mode_SHOW == 2 && VAR_mode_SET > 7) {
VAR_mode_SET = 4; // возвращаемся к первому устанавливаемому параметру
}
} else { // если кнопка SET удерживалась дольше 2 секунд, то требуется выйти из режима установки даты/времени
VAR_mode_SET = 0; // выходим из режима установки даты/времени
}
}
//Если часы находятся в режиме вывода даты/времени
} else {
// Если нажата кнопка SET
if (digitalRead(PIN_button_SET) == LOW) {
while (digitalRead(PIN_button_SET) == LOW) {
delay(10);
if (i < 200) {
i++;
} else {
matrix.fillScreen(LOW);
}
}
if (i < 200) {
matrix.fillScreen(LOW);
VAR_mode_SHOW = VAR_mode_SHOW == 1 ? 2 : 1;
} else {
if (VAR_mode_SHOW == 1) {
VAR_mode_SET = 1;
}
if (VAR_mode_SHOW == 2) {
VAR_mode_SET = 4;
}
}
}
if (digitalRead(PIN_button_DOWN) == LOW) {
VAR_mode_SHOW = VAR_mode_SHOW == 1 ? 3 : 1;
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
tape = plus + String round (tempC) + c;
}
if (digitalRead(PIN_button_UP) == LOW) {
VAR_mode_SHOW = VAR_mode_SHOW == 1 ? 4 : 1;
tape = String (time.gettime("i" ":" "s"));
}
}
}
/*
Автоматическое отображение температуры каждую минуту
*/
void temperature () {
if (time.seconds > 25 && time.seconds < 35) {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
tape = plus + String round (tempC) + c;
}
}
/*
Установка автоматической регулировки яркости
*/
void ledBrightness () {
if (millis () - timing >= period) {
timing += period;
int val = map (analogRead(A7), 0, 1023, 0, 100);
constrain (val, 0, 100);
if (val <= 5) {
brightness = 0;
}
if (val > 5 && val <= 60) {
brightness = 2;
}
if (val > 60 && val <= 80) {
brightness = 5;
}
if (val >= 80 && val <= 100) {
brightness = 13;
}
matrix.setIntensity(brightness);
}
}