commit 2fed7bba77a776e09dcb72bc695845a842fdc36b Author: coelner Date: Wed Dec 11 09:03:32 2019 +0100 first commit diff --git a/60LED_WS2812B_NTP_Clock.ino b/60LED_WS2812B_NTP_Clock.ino new file mode 100644 index 0000000..d11fba7 --- /dev/null +++ b/60LED_WS2812B_NTP_Clock.ino @@ -0,0 +1,163 @@ +#include +#include "time.h" +#include + +// LDR (GL5516 ) is connected to GPIO 34 (Analog ADC1_CH6 +// over a 56k voltage divider . Dark (0.5MOhm) , 10Lux (5-10kOhm) +const int ldrPin = 34; +// How many leds in your strip? +#define NUM_LEDS 60 +#define BRIGHTNESS 30 +#define COLOR_ORDER GRB +#define DATA_PIN 5 +// Define the array of leds +CRGB leds[NUM_LEDS]; + + +volatile int interruptCounter; +hw_timer_t * timer = NULL; +portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; + +void IRAM_ATTR onTimer() { + portENTER_CRITICAL_ISR(&timerMux); + interruptCounter++; + portEXIT_CRITICAL_ISR(&timerMux); +} + + +volatile int currentSec = 59; +volatile int currentMin = 1; +volatile int currentHour = 1; + +// variable for storing the potentiometer value +volatile int ldrValue = 0; + +const char* ssid = "koelner Gastzugang"; +const char* password = "deltaforce!"; + +const char* ntpServer = "pool.ntp.org"; +const long gmtOffset_sec = 3600; +const int daylightOffset_sec = 3600; +struct tm timeinfo; + +void printLocalTime() +{ + if (!getLocalTime(&timeinfo)) { + Serial.println("Failed to obtain time"); + return; + } + Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); +} + +void syncNTP() { + Serial.println(WiFi.getMode()); + WiFi.mode(WIFI_STA); + delay(1000); + WiFi.enableAP(false); + WiFi.enableSTA(true); + //connect to WiFi + Serial.printf("Connecting to %s ", ssid); + WiFi.begin(ssid, password); + int i = 0; + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + if (i == 10) { + break; + } + i++; + } + Serial.println(" CONNECTED"); + + //init and get the time + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + printLocalTime(); + //inverted logic - remaining time + currentSec = 60 - timeinfo.tm_sec; + currentMin = 60 - timeinfo.tm_min; + currentHour = 12 - (timeinfo.tm_hour % 12); + Serial.println("NTP mapped: " + String(currentHour) + ":" + String(currentMin) + ":" + String(currentSec)); + //disconnect WiFi as it's no longer needed + WiFi.disconnect(true); + WiFi.mode(WIFI_OFF); +} +void setup() +{ + //interrupt for one second@80Mhz! + timer = timerBegin(0, 80, true); + timerAttachInterrupt(timer, &onTimer, true); + timerAlarmWrite(timer, 1000000, true); + timerAlarmEnable(timer); + Serial.begin(9600); + + FastLED.addLeds(leds, NUM_LEDS); + + FastLED.setBrightness( BRIGHTNESS ); + FastLED.clear(); + + syncNTP(); + //currentSec = 40; currentMin = 1; currentHour=0; +} +void LDR() { + //brigthness measurement + ldrValue = analogRead(ldrPin); + if (ldrValue == 0) { + FastLED.setBrightness( BRIGHTNESS ); + } + else { + FastLED.setBrightness( int(BRIGHTNESS - BRIGHTNESS / log(ldrValue)) ); //need some logarithm + Serial.println("Brightness: " + String( int(BRIGHTNESS - BRIGHTNESS / log(ldrValue*10)))); + } + +} +void loop() +{ + while (interruptCounter == 0) { + delay(1); + } + if (interruptCounter > 0) { + + portENTER_CRITICAL(&timerMux); + interruptCounter--; + portEXIT_CRITICAL(&timerMux); + currentSec--; + } + + if (currentSec < 0) { + currentSec = 59; + currentMin--; + LDR(); + if (currentMin < 0) { + currentMin = 59; + currentHour--; + if (currentHour < 0) { + currentHour = 11; + timerAlarmDisable(timer); + //ESP.restart(); + //syncNTP(); + timerAlarmEnable(timer); + } + } + Serial.println("Ring Index: " + String(currentHour * 5) + ":" + String(currentMin) + ":" + String(currentSec)); + } + + //reset color + leds[(currentSec + 1) % NUM_LEDS] = CRGB(30, 48, 40); + leds[(currentMin + 1) % NUM_LEDS] = CRGB(30, 48, 40); + //3 dots hour + leds[(currentHour * 5 - 1) % NUM_LEDS] = CRGB::Gold; + leds[(currentHour * 5) % NUM_LEDS] = CRGB::Gold; + leds[(currentHour * 5 + 1) % NUM_LEDS] = CRGB::Gold; + //1 dot hour marking + for (int dot = 0; dot < NUM_LEDS; dot++) { + if (dot % 5 == 0) { + leds[dot] = CRGB::OrangeRed; + } + } + leds[0] = CRGB::Blue; //define high noon + //leds[NUM_LEDS / 2] = CRGB::Green; //define half noon + + leds[(currentSec + 0) % NUM_LEDS] = CRGB::Black; + leds[(currentMin + 0) % NUM_LEDS] = CRGB::Orange; + FastLED.show(); +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29