/*
#include "SunPosition.h"
void setup() {
Serial.begin(9600);
SunPosition sun(55.75, 37.62, 1658325600);
//sun.compute(55.75, 37.62, 1658325600);
Serial.println(sun.altitude());
Serial.println(sun.azimuth());
Serial.println(sun.declination());
}
void loop() {
}
*/
///////////////////////////////////////////////////////////////
//このプログラムは、Serial Monitorで日の出と日の入りの時間を表示します。必要に応じて、出力を変更することができます。
#include "TimeLib.h"
#include "SunPosition.h"
////////////////////
#include <Arduino.h>
#include <TM1637Display.h>
////////////////////
float latitude = 35.6356964;
float longitude = 140.1504495;
int timeZone = 9;
////////////////////////////////////////////////////////////
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 2000
const uint8_t SEG_DONE[] = {
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
SEG_C | SEG_E | SEG_G, // n
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E
};
TM1637Display display(CLK, DIO);
///////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
setTime(12, 0, 0, 24, 3, 2023); //時,分,秒,日,月,西暦
Serial.println(now());
Serial.print(year());
Serial.print("/");
Serial.print(month());
Serial.print("/");
Serial.println(day());
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.println(second());
Serial.println(weekday()); //the weekday now (Sunday is day 1)
Serial.println(hourFormat12());
delay(2000);
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.println(second());
SunPosition sun(latitude, longitude, now() ,timeZone);
//SunPosition sun(latitude, longitude, 1679400000 ,9);
// int sunrise = sunrise(year, month, day, latitude, longitude, timeZone);
// int sunset = sunset(year, month, day, latitude, longitude, timeZone);
Serial.print("Sunrise: ");
Serial.println(sun.sunrise());
Serial.print(sun.sunrise()/60);
Serial.print(":");
Serial.println(sun.sunrise()%60);
Serial.print("Sunset: ");
Serial.println(sun.sunset());
Serial.print(sun.sunset()/60);
Serial.print(":");
Serial.println(sun.sunset()%60);
}
void loop() {
int k;
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 };
display.setBrightness(0x0f);
// All segments on
display.setSegments(data);
delay(TEST_DELAY);
// Selectively set different digits
data[0] = display.encodeDigit(0);
data[1] = display.encodeDigit(1);
data[2] = display.encodeDigit(2);
data[3] = display.encodeDigit(3);
display.setSegments(data);
delay(TEST_DELAY);
/*
for(k = 3; k >= 0; k--) {
display.setSegments(data, 2, k);
delay(TEST_DELAY);
}
display.clear();
display.setSegments(data+2, 2, 2);
delay(TEST_DELAY);
display.clear();
display.setSegments(data+2, 2, 1);
delay(TEST_DELAY);
display.clear();
display.setSegments(data+1, 3, 1);
delay(TEST_DELAY);
// Show decimal numbers with/without leading zeros
display.showNumberDec(0, false); // Expect: ___0
delay(TEST_DELAY);
display.showNumberDec(0, true); // Expect: 0000
delay(TEST_DELAY);
display.showNumberDec(1, false); // Expect: ___1
delay(TEST_DELAY);
display.showNumberDec(1, true); // Expect: 0001
delay(TEST_DELAY);
display.showNumberDec(301, false); // Expect: _301
delay(TEST_DELAY);
display.showNumberDec(301, true); // Expect: 0301
delay(TEST_DELAY);
display.clear();
display.showNumberDec(14, false, 2, 1); // Expect: _14_
delay(TEST_DELAY);
display.clear();
display.showNumberDec(4, true, 2, 2); // Expect: __04
delay(TEST_DELAY);
display.showNumberDec(-1, false); // Expect: __-1
delay(TEST_DELAY);
display.showNumberDec(-12); // Expect: _-12
delay(TEST_DELAY);
display.showNumberDec(-999); // Expect: -999
delay(TEST_DELAY);
display.clear();
display.showNumberDec(-5, false, 3, 0); // Expect: _-5_
delay(TEST_DELAY);
display.showNumberHexEx(0xf1af); // Expect: f1Af
delay(TEST_DELAY);
display.showNumberHexEx(0x2c); // Expect: __2C
delay(TEST_DELAY);
display.showNumberHexEx(0xd1, 0, true); // Expect: 00d1
delay(TEST_DELAY);
display.clear();
display.showNumberHexEx(0xd1, 0, true, 2); // Expect: d1__
delay(TEST_DELAY);
*/
//! Display a decimal number, with dot control
//!
//! Display the given argument as a decimal number. The dots between the digits (or colon)
//! can be individually controlled.
//!
//! @param num The number to be shown
//! @param dots Dot/Colon enable. The argument is a bitmask, with each bit corresponding to a dot
//! between the digits (or colon mark, as implemented by each module). i.e.
//! For displays with dots between each digit:
//! * 0.000 (0b10000000) 0x80
//! * 00.00 (0b01000000) 0x40
//! * 000.0 (0b00100000) 0x20
//! * 0.0.0.0 (0b11100000)
//! For displays with just a colon:
//! * 00:00 (0b01000000) 0x40
//! For displays with dots and colons colon:
//! * 0.0:0.0 (0b11100000) 0xE0
//! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
//! blank. NOTE: leading zero is not supported with negative numbers.
//! @param length The number of digits to set. The user must ensure that the number to be shown
//! fits to the number of digits requested (for example, if two digits are to be displayed,
//! the number must be between 0 to 99)
//! @param pos The position of the most significant digit (0 - leftmost, 3 - rightmost)
//! void showNumberDecEx(int num, uint8_t dots = 0,
//! bool leading_zero = false, uint8_t length = 4,
//! uint8_t pos = 0);
display.setSegments(data);
// Run through all the dots
for(k=0; k <= 4; k++) {
// display.showNumberDecEx(0, (0x80 >> k), true);
display.showNumberDecEx(357, 0b10000000, false);
delay(1000);
display.clear();
delay(1000);
}
display.clear();
display.showNumberHexEx(0xd1, 0, true, 2); // Expect: d1__
delay(TEST_DELAY);
/*
// Brightness Test
for(k = 0; k < 4; k++)
data[k] = 0xff;
for(k = 0; k < 7; k++) {
display.setBrightness(k);
display.setSegments(data);
delay(TEST_DELAY);
}
// On/Off test
for(k = 0; k < 4; k++) {
display.setBrightness(7, false); // Turn off
display.setSegments(data);
delay(TEST_DELAY);
display.setBrightness(7, true); // Turn on
display.setSegments(data);
delay(TEST_DELAY);
}
*/
// Done!
display.setSegments(SEG_DONE);
while(1);
}
////////////////////////////////////////////////////////////////
/*
#include "TimeLib.h"
String dateString = "2023-03-18 12:34:56";
void setup() {
Serial.begin(9600);
}
void loop() {
int year = dateString.substring(0, 4).toInt();
int month = dateString.substring(5, 7).toInt();
int day = dateString.substring(8, 10).toInt();
int hour = dateString.substring(11, 13).toInt();
int minute = dateString.substring(14, 16).toInt();
int second = dateString.substring(17, 19).toInt();
tm tmTime;
tmTime.tm_year = year - 1900;
tmTime.tm_mon = month - 1;
tmTime.tm_mday = day;
tmTime.tm_hour = hour;
tmTime.tm_min = minute;
tmTime.tm_sec = second;
time_t unixTime = mktime(&tmTime);
Serial.print("Unix time: ");
Serial.println(unixTime);
delay(1000);
}
*/
/*
* TimeSerial.pde
* example code illustrating Time library set through serial port messages.
*
* Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
* you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
T1357041600
*
* A Processing example sketch to automatically send the messages is included in the download
* On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
*/
/////////////////////////////////////////////////////////////
/*
#include "TimeLib.h"
#define TIME_HEADER "T" // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
void setup() {
Serial.begin(9600);
while (!Serial) ; // Needed for Leonardo only
pinMode(13, OUTPUT);
setSyncProvider( requestSync); //set function to call when sync required
Serial.println("Waiting for sync message");
}
void loop(){
if (Serial.available()) {
processSyncMessage();
}
if (timeStatus()!= timeNotSet) {
digitalClockDisplay();
}
if (timeStatus() == timeSet) {
digitalWrite(13, HIGH); // LED on if synced
} else {
digitalWrite(13, LOW); // LED off if needs refresh
}
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 //1687041600
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
time_t requestSync()
{
Serial.write(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}
*/
///////////////////////////////////////////////////////////
/*
#include "TimeLib.h"
#include <stdarg.h>
void setup() {
#if 1
setTime(9, 43, 0, 19, 6, 2016);
#else
setTime(86400);
#endif
Serial.begin(115200);
}
void myPrintf(char *fmt, ...)
{
char buf[128];
va_list args;
va_start(args, fmt);
vsnprintf(buf, 128, fmt, args);
va_end(args);
Serial.print(buf);
}
void loop() {
// myPrintf("%02d:%02d:%02d", hour(), minute(), second() );
// myPrintf(",%02d/%02d/%04d", day(), month(), year() );
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.println();
delay(3000);
}
*/