STOPWATCH
Код:
#include
#include
#include
#include "GyverButton.h"
#define OLED_DC 26
#define OLED_CS 31
#define OLED_RESET 24
#define BTN1 2
#define BTN2 12
#define BuzzerPin 15
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
GButton butt1(BTN1, LOW_PULL, NORM_OPEN);
GButton butt2(BTN2, LOW_PULL, NORM_OPEN);
unsigned long stopwatchtimer = 0;
unsigned long stopwatchdeltatime = 0;
bool stopwatch_pause = false;
// Функция обработки действия кнопок
void buttons();
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
}
void buttons()
{
// Обновляем состояние кнопок
butt1.tick();
butt2.tick();
// Сбрасываем время
if (butt1.isClick())
{
stopwatchtimer = millis();
stopwatch_pause = true;
tone(BuzzerPin, 4000, 200);
}
// Ставим на паузу
if (butt2.isClick())
{
stopwatch_pause = !stopwatch_pause;
// Снимаем с паузы и пересчитываем время
if (stopwatch_pause)
{
stopwatchtimer += (((millis() - stopwatchtimer) / 1000) - (stopwatchdeltatime)) * 1000;
}
tone(BuzzerPin, 2000, 200);
}
}
void loop()
{
buttons();
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
display.println F("Stopwatch");
// Если пауза не стоит, выводим время
if (stopwatch_pause)
{
stopwatchdeltatime = (millis() - stopwatchtimer) / 1000;
}
// Дописываем нолик часам
if (stopwatchdeltatime / 60 / 60 < 10)
{
display.print F("0");
}
// Выводим часы
display.print (stopwatchdeltatime / 60 / 60);
display.print F(":");
// Дописываем нолик минутам
if (stopwatchdeltatime / 60 % 60 < 10)
{
display.print F("0");
}
// Выводим минуты
display.print ((stopwatchdeltatime / 60) % 60);
display.print F(":");
// Дописываем нолик секундам
if (stopwatchdeltatime % 60 < 10)
{
display.print F("0");
}
// Выводим секунды
display.println (stopwatchdeltatime % 60);
// Выводим статус паузы
if (!stopwatch_pause)
{
display.println F("Pause");
}
// Отрисовываем кадр
display.display();
}
Add a Comment
Вы должны зайти как в для комментирования записи