Hey Guys,
im trying to dimm two rows off high power LEDs via PWM at set timepoints (yes this is indeed another aquarium topic). To mark the start of certain events I would like to use a DS3231.
I got this to work great using Delays but when i try to replace this with Millis it is not working, can you point me in the right direction?
// Date and time functions using just software, based on millis() & timer
int ledPin1 = 6;
int ledPin2 = 5;
int brightness = 0;
int fadeAmount = 5;
int myhour = 0;
int myminute = 0;
int CO2Pin1 = 9;
#include <Arduino.h>
#include <Wire.h> // this #include still required because the RTClib depends on it
#include "RTClib.h" // misschien aanpassen naar jouw lib naam!
unsigned long co2Millis; //Waarde voor CO2 check
unsigned long fadeLEDMillis; //Waarde voor LED fade
unsigned long currentMillis; //Current value
const unsigned long intervalUp = 1000;
const unsigned long intervalDown = 1000;
const unsigned long CO2AAN = 1000;
RTC_Millis rtc;
void setup () {
pinMode(CO2Pin1, OUTPUT);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
Serial.begin(57600);
// following line sets the RTC to the date & time this sketch was compiled
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
DateTime now = rtc.now();
myhour = now.hour();
myminute = now.minute();
Serial.print(myhour);
Serial.print(":");
Serial.println(myminute);
}
void loop () {
unsigned long currentMillis = millis();
if(myhour == 10 && myminute == 00) { //Infaden LED
sunrise();
}
if(myhour == 11 && myminute == 00) { //Licht steady aan
steadysun();
}
if(myhour == 19 && myminute == 00) { //LED uitfaden
sunset();
}
if(myhour == 9 && myminute == 00) { //CO2 aan
co2aan();
}
delay(10000);
}
void sunrise() // Functie voor starten LED
{
for(brightness = 0; brightness < 175; brightness = brightness + fadeAmount) {
if (currentMillis - fadeLEDMillis >= intervalUp){
analogWrite(ledPin1,brightness);
Serial.print("OMHOOG");
Serial.print(brightness);
analogWrite(ledPin2,brightness);
Serial.print("OMHOOG");
Serial.print(brightness);
fadeLEDMillis = currentMillis; //IMPORTANT to save start time of current LED
}
}
}
void steadysun() //Functie voor aanhouden LED
{
analogWrite(ledPin1,175);
analogWrite(ledPin2,100);
}
void sunset() // Functie voor uitschakelen LED
{
for(brightness = 175; brightness >= 0; brightness = brightness - fadeAmount) {
if (currentMillis - fadeLEDMillis >= intervalDown){
analogWrite(ledPin1,brightness);
Serial.print("NAAR BENEE");
Serial.print(brightness);
analogWrite(ledPin2,brightness);
Serial.print("NAAR BENEE");
Serial.print(brightness);
fadeLEDMillis = currentMillis; //IMPORTANT to save start time of current LED according to https://forum.arduino.cc/index.php?topic=503368.0
}
}
}
void co2aan()
{
analogWrite(CO2Pin1,HIGH);
if (currentMillis - co2Millis >= CO2AAN){
analogWrite(CO2Pin1,LOW);
co2Millis = currentMillis; //IMPORTANT to save start time of current LED
}
}