Go Down

Topic: Led with Time Nano RTC Led (Read 184 times) previous topic - next topic

sparfux

Aug 05, 2019, 04:39 pm Last Edit: Aug 05, 2019, 05:22 pm by sparfux
Hello
i have a nano rtc (ds3231) and a Lighthouse i will test it
the Led make on at 18.00 and going off at 23.00
plz help me for start the time the time is set in my Nano with rtc but we make the sketch for put the Led on with time

here my sketch

Code: [Select]
enum OutStates {
OSoff,
OSFadeUp,
OSFadeDown,
OSon
};

#define OffTime 4000
#define OnTime 1000
#define DimmStepTime 10
#define fadeAmount 1
//#define MaxPWMVal 1023 //Für ESP8266
#define MaxPWMVal 255
/// #RTC --- TIme
#define DS3231_ADDRESSE 0x68
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
DateTime now = rtc.now();
int LED_Pin = 11;

uint32_t Outputsteptime;
int brightness = 0; // how bright the LED is
OutStates OSState;

void setup() {
Wire.begin();
Serial.begin (9600);
Serial.println ("Programm wurde gestartet");
OSState = OSoff;
Outputsteptime = millis();
pinMode(LED_BUILTIN, OUTPUT);
brightness = 0;
}

void Output() {
switch (OSState) {
case OSoff:
if (millis() > Outputsteptime + OffTime) {
OSState = OSFadeUp;
Serial.println ("OSFadeUp");
}
break;
case OSFadeUp:
if (millis() > Outputsteptime + DimmStepTime) {
brightness += fadeAmount;
Outputsteptime = millis();
}
if (brightness > MaxPWMVal) {
brightness = MaxPWMVal;
OSState = OSon;
Serial.println ("OSOn");
Outputsteptime = millis();
}
analogWrite(LED_Pin, brightness);
break;
case OSFadeDown:
if (millis() > Outputsteptime + DimmStepTime) {
brightness -= fadeAmount;
Outputsteptime = millis();
}
if (brightness < 0) {
brightness = 0;
OSState = OSoff;
Serial.println ("OSOff");
Outputsteptime = millis();
}
analogWrite(LED_Pin, brightness);
break;
case OSon:
if (millis() > Outputsteptime + OnTime) {
OSState = OSFadeDown;
Serial.println ("OSFadeDown");
}
break;
}
}
void leseDS3231zeit(byte *sekunde, byte *minute,byte *stunde, byte *wochentag, byte *tag, byte *monat, byte *jahr) {
      Wire.beginTransmission(DS3231_ADDRESSE);
      Wire.write(0); // DS3231 Register zu 00h
      Wire.endTransmission();
      Wire.requestFrom(DS3231_ADDRESSE, 7); // 7 Byte Daten vom DS3231 holen
     //*sekunde = bcdToDec(Wire.read() & 0x7f);
   //   *minute = bcdToDec(Wire.read());
   //   *stunde = bcdToDec(Wire.read() & 0x3f);
     // *wochentag = bcdToDec(Wire.read());
   //   *tag = bcdToDec(Wire.read());
   //   *monat = bcdToDec(Wire.read());
   //   *jahr = bcdToDec(Wire.read());
     }


   
void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    Serial.println();
Output();
   
  // zeigeZeit(); // Zeit ausgeben
   /// If bedingung !!!!!
    if(now.hour() == 24)
   //if(now.hour >=18 && now.hour<23 = 1);
   if (now.hour() == 18 & now.minute() == 0 & now.second() == 0)
   digitalWrite(LED_Pin,0);           // Turns ON LED 1
   Serial.println("Light ON");
   //delay(2000);                                      // Wait 2 seconds

   digitalWrite(LED_Pin,1);          // Turns OFF LED 1
   Serial.println("Light OFF");
   //delay(2000);
}

Go Up