Arduino: Часовник

Публикувано: 27.08.16, 16:52:15 | Автор: ArMeN | Видяна: 247 | Редактирано: -

// macros from DateTime.h
/* Useful Constants */
#define SECS_PER_MIN (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY (SECS_PER_HOUR * 24L)

/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)

void setup(){
Serial.begin (57600);
}

void loop(){
time(millis() / 1000);
delay(1000);
}

void time(long val){
int days = elapsedDays(val);
int hours = numberOfHours(val);
int minutes = numberOfMinutes(val);
int seconds = numberOfSeconds(val);

// digital clock display of current time
Serial.print(days,DEC);
printDigits(hours);
printDigits(minutes);
printDigits(seconds);
Serial.println();

}

void printDigits(byte digits){
// utility function for digital clock display: prints colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits,DEC);
}


Съкратен вариант, измерва секунди и минути...
/* Useful Constants */
#define SECS_PER_MIN (60UL)
/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)

void setup(){
Serial.begin (57600);
}

void loop(){
int minutes = numberOfMinutes(millis() / 1000);
int seconds = numberOfSeconds(millis() / 1000);
Serial.println(minutes);
Serial.println(seconds);
delay(1000);
}


СЪс фор цукъл
/* Useful Constants */
#define SECS_PER_MIN (60UL)
/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)

void setup(){
Serial.begin (57600);
}

void loop(){
for (int i = 0; i < 1800000; i++){
int minutes = numberOfMinutes(i);
int seconds = numberOfSeconds(i);
Serial.println(minutes);
Serial.println(seconds);
delay(1000);
}
}

Не сте влезли в акаунта си за да коментирате.


 

Няма коментари.