#include <Adafruit_NeoPixel.h>
#include <U8g2lib.h>
#include "time.h"
#include "SPI.h"
#include <WiFi.h>
#include <Wire.h>
#define PIN 21
#define NUMPIXELS 12
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//WIFI
// const char *ssid = "Makerfabs";
// const char *password = "20160704";
#define DELAYVAL 500
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Initialize the OLED display using U8G2 library
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 5, /* data=*/ 4);
// NTP時間
const char* ntpServer = "time.google.com";
const long gmtOffset_sec = 28800; // 東八區偏移量
const int daylightOffset_sec = 0;
struct tm timeinfo;
void setup() {
// 初始化OLED顯示屏
u8g2.begin();
u8g2.enableUTF8Print(); // Enable UTF8 support
u8g2.setFont(u8g2_font_unifont_t_chinese1); // Set font to support Chinese characters
// 初始化串口
Serial.begin(115200);
Serial.print("正在連接WiFi");
// 連接WiFi
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" 已連接");
// 配置NTP時間
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.println(F("已獲取NTP時間"));
// 初始化NeoPixel條
pixels.begin();
}
void loop() {
pixels.clear();
// 獲取本地時間
if (!getLocalTime(&timeinfo)) {
Serial.println("獲取時間失敗");
return;
}
int hour = timeinfo.tm_hour;
int min = timeinfo.tm_min;
int sec = timeinfo.tm_sec;
int day = timeinfo.tm_mday;
int mon = timeinfo.tm_mon + 1;
int year = timeinfo.tm_year + 1900;
int wday = timeinfo.tm_wday;
// 確定星期幾
const char* weekdays[] = {"0", "1", "2", "3", "4", "5", "6"}; //"日", "一", "二", "三", "四", "五", "六"
// 更新OLED顯示
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_unifont_t_chinese1);
u8g2.setCursor(0, 15);
u8g2.print("roger!"); //改我愛羅傑! u8g2庫https://drive.google.com/file/d/1nrf34A4qL7WiS8nm78jMgG5wWOl88rkM/view
u8g2.setCursor(0, 35);
u8g2.print("Week"); //改星期
u8g2.print(weekdays[wday]);
u8g2.setCursor(65, 35);
u8g2.print(hour < 10 ? "0" : ""); u8g2.print(hour);
u8g2.print(":");
u8g2.print(min < 10 ? "0" : ""); u8g2.print(min);
u8g2.print(".");
u8g2.print(sec < 10 ? "0" : ""); u8g2.print(sec);
u8g2.setCursor(0, 55);
u8g2.print(year); u8g2.print("/");
u8g2.print(mon < 10 ? "0" : ""); u8g2.print(mon); u8g2.print("/");
u8g2.print(day < 10 ? "0" : ""); u8g2.print(day);
u8g2.sendBuffer();
// 每個LED表示5分鐘
int min_d = min / 5;
// 每個LED表示5秒鐘
int sec_d = sec / 5;
// 設置分鐘的LED顏色從藍色漸變到綠色
for (int i = 0; i < NUMPIXELS; i++) {
int r = 0, g = 0, b = 0;
if (i < min_d) {
g = map(i, 0, NUMPIXELS - 1, 0, 255);
b = map(i, 0, NUMPIXELS - 1, 255, 0);
}
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
// 設置秒鐘的LED顏色從紅色漸變到黃色
for (int i = 0; i < NUMPIXELS; i++) {
if (i < sec_d) {
uint32_t currentColor = pixels.getPixelColor(i);
uint8_t currentRed = (currentColor >> 16) & 0xFF;
uint8_t currentGreen = (currentColor >> 8) & 0xFF;
uint8_t currentBlue = currentColor & 0xFF;
int r = 255;
int g = map(i, 0, NUMPIXELS - 1, 0, 255);
int b = 0;
// 混合顏色
r = (r + currentRed) / 2;
g = (g + currentGreen) / 2;
b = (b + currentBlue) / 2;
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
}
pixels.show();
delay(999);
}