Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Часики с секретом
- */
- #define ButtonHours 2
- #define ButtonMinutes 4
- int hoursState = HIGH;
- int minutesState = HIGH;
- // время последнего тика в миллисекундах
- unsigned long previousMillis = 0;
- // таймер в секундах
- unsigned long timer = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(ButtonHours, INPUT_PULLUP);
- pinMode(ButtonMinutes, INPUT_PULLUP);
- }
- void loop() {
- // текущее время в миллисекундах
- unsigned long currentMillis = millis();
- // если разница между текущем временем и времени последнего тика больше или равно 1 секунде,
- // печатаем в консоль, увеличиваем таймер и обновляем время последнего тика
- if (currentMillis - previousMillis >= 1000) {
- printTime(timer);
- timer++;
- previousMillis = currentMillis;
- }
- hoursState = digitalRead(ButtonHours);
- minutesState = digitalRead(ButtonMinutes);
- if (hoursState == LOW) {
- delay(200);
- timer += 3600;
- }
- if (minutesState == LOW) {
- delay(200);
- timer += 60;
- }
- }
- void printTime(unsigned long timeSeconds) {
- // положительные однобайтовыйе числа 0...255
- unsigned char seconds, minutes, hours;
- // массив сиволов 00:00:00, плюс нуль-терминатор
- char buff[9];
- // вычисляем 24 часовой формат
- seconds = timeSeconds % 60;
- minutes = (timeSeconds / 60) % 60;
- hours = (timeSeconds / 3600) % 24;
- // формируем красивую строку и заносим в buff
- sprintf(buff, "%02d:%02d:%02d\n", hours, minutes, seconds);
- // печатаем
- Serial.print(buff);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement