Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Simple Analog Clock Using a SSD1306 OLED I2C Display
- * Time is made from NTP Local Time using the ezTime library
- * Default 30 minute NTP re-sync by the ezTime library
- * This sketch will run on an ESP32 or ESP8266
- *
- * Provide official timezone names for "myTZ.setLocation(F("TZ_Name"));"
- * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
- *
- * NOTE: Only tested on ESP32
- */
- #ifdef ESP32
- #include <WiFi.h>
- #else ifdef ESP8266
- #include <ESP8266WiFi.h>
- #endif
- #include <ezTime.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- /*Define I2C Address and OLED screen size*/
- Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);
- const char* ssid = "Your_ssid";
- const char* password = "Your_password";
- const int radius = 35;
- Timezone myTZ; //Lets us use local time and auto daylight time correction
- int lastSec;
- void setup() {
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { /*I2C Address for OLED*/
- Serial.println(F("SSD1306 allocation failed"));
- for(;;);
- }
- delay(1000);
- display.clearDisplay(); /*Clear OLED display*/
- display.setTextSize(1); /*Text size set*/
- display.setCursor(0,0); /*OLED cursor set*/
- display.setTextColor(WHITE); /*text color set white*/
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED){;}
- waitForSync(); //Default method for NTP sync every 30min
- myTZ.setLocation(F("Pacific/Auckland")); //Sets our Timezone object
- }
- void loop() {
- events(); //This enables the 30 minute NTP update cycle
- if(lastSec != myTZ.second()){
- lastSec = myTZ.second();
- display.clearDisplay();
- display.drawCircle(64, 32, radius-4, SSD1306_WHITE);
- //Draw Clock Second Hand
- float angle = myTZ.second()*6 ;
- angle=(angle/57.29577951) ;
- int x2=(64+(sin(angle)*(radius-4)));
- int y2=(32-(cos(angle)*(radius-4)));
- display.drawLine(64,32,x2,y2,WHITE);
- // Draw Clock Minute Hand
- angle = myTZ.minute() * 6 ;
- angle=(angle/57.29577951) ;
- x2=(64+(sin(angle)*(radius-8)));
- y2=(32-(cos(angle)*(radius-8)));
- display.drawLine(64,32,x2,y2,WHITE);
- // Draw Clock Hour Hand
- angle = myTZ.hour() * 30 + ((myTZ.minute() / 12) * 6 );
- angle=(angle/57.29577951) ;
- x2=(64+(sin(angle)*(radius-14)));
- y2=(32-(cos(angle)*(radius-14)));
- display.drawLine(64,32,x2,y2,WHITE);
- display.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment