From b36af6240325886405f06f29864afd5a18b9fdc9 Mon Sep 17 00:00:00 2001 From: coelner Date: Thu, 12 Dec 2019 19:06:56 +0100 Subject: [PATCH] fix wrong modulo --- 60LED_WS2812B_NTP_Clock.ino | 96 ++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/60LED_WS2812B_NTP_Clock.ino b/60LED_WS2812B_NTP_Clock.ino index d11fba7..9e0d16a 100644 --- a/60LED_WS2812B_NTP_Clock.ino +++ b/60LED_WS2812B_NTP_Clock.ino @@ -1,20 +1,22 @@ #include #include "time.h" #include +#include +#define MOD(a,b) ((((a)%(b))+(b))%(b)) // 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 BRIGHTNESS 50 #define COLOR_ORDER GRB #define DATA_PIN 5 // Define the array of leds CRGB leds[NUM_LEDS]; -volatile int interruptCounter; +volatile signed int interruptCounter; hw_timer_t * timer = NULL; portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; @@ -32,6 +34,7 @@ volatile int currentHour = 1; // variable for storing the potentiometer value volatile int ldrValue = 0; +WiFiManager wifiManager; const char* ssid = "koelner Gastzugang"; const char* password = "deltaforce!"; @@ -88,6 +91,7 @@ void setup() timerAttachInterrupt(timer, &onTimer, true); timerAlarmWrite(timer, 1000000, true); timerAlarmEnable(timer); + Serial.begin(9600); FastLED.addLeds(leds, NUM_LEDS); @@ -96,7 +100,7 @@ void setup() FastLED.clear(); syncNTP(); - //currentSec = 40; currentMin = 1; currentHour=0; + //currentSec = 40; currentMin = 1; currentHour = 2; } void LDR() { //brigthness measurement @@ -105,59 +109,65 @@ void LDR() { FastLED.setBrightness( BRIGHTNESS ); } else { - FastLED.setBrightness( int(BRIGHTNESS - BRIGHTNESS / log(ldrValue)) ); //need some logarithm - Serial.println("Brightness: " + String( int(BRIGHTNESS - BRIGHTNESS / log(ldrValue*10)))); + FastLED.setBrightness( int(BRIGHTNESS - BRIGHTNESS / log(ldrValue * 10)) ); + //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--; - } + portEXIT_CRITICAL(&timerMux); - 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); + 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(String(interruptCounter) + " | 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); + leds[((currentHour + 1) * 5 - 1) % NUM_LEDS] = CRGB(30, 48, 40); + leds[((currentHour + 1) * 5 + 1) % NUM_LEDS] = CRGB(30, 48, 40); + + //1 dot hour marking + for (int dot = 0; dot < NUM_LEDS; dot++) { + if (dot % 5 == 0) { + leds[dot] = CRGB::OrangeRed; } } - 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; + //3 dots hour + if (currentHour == 0) { + leds[59 % NUM_LEDS] = CRGB::Gold; } - } - leds[0] = CRGB::Blue; //define high noon - //leds[NUM_LEDS / 2] = CRGB::Green; //define half noon + else { + leds[(currentHour * 5 - 1) % NUM_LEDS] = CRGB::Gold; + } + leds[(currentHour * 5) % NUM_LEDS] = CRGB::Gold; + leds[(currentHour * 5 + 1) % NUM_LEDS] = CRGB::Gold; - leds[(currentSec + 0) % NUM_LEDS] = CRGB::Black; - leds[(currentMin + 0) % NUM_LEDS] = CRGB::Orange; - FastLED.show(); + 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(); + } }