I'm fairly new to arduino and have run into a problem on my current project.
I have a loop that basically is a thermostat function and sends values with pubsubclient.
This part works fine.
However, i have also a function (ledfadeon())that is called on sunrise to start fading in a LED light.
This function uses a for loop. My problem is when i call this function, nothing else can happen in the main loop until the for loop of the dimming function ends.
How can i run this dimming loop in "parallel" to the main loop? Or any way to avoid this problem?
Any help is highly appreciated!
Here are the for loop functions:
void ledfadeON(){
for (int i=0; i < 1024; i++){
for (j=0; j<sunrisespeed; j++)
{
analogWrite(pulsepin, i);
delay(1);
}
Serial.println(i);
}
dimmedon = true;
dimmedoff = false;
}
void ledfadeOFF(){
for (int i=1023; i > 0; i--){
for (j=0; j<sunrisespeed; j++)
{
analogWrite(pulsepin, i);
delay(1);
}
Serial.println(i);
}
dimmedoff = true;
dimmedon = false;
digitalWrite(pulsepin, LOW);
}
And here is my main loop:
void loop() {
DS18B20.requestTemperatures();
temp = (DS18B20.getTempCByIndex(0)*1000)+273150;
if (OnOff == 1) { //Enable / Disable thermostat function
thermostat();
}
else {
digitalWrite(relayPin,LOW);
}
timer.run();
today[3] = day(epochtime);
today[4] = month(epochtime);
today[5] = year(epochtime); // store today's date (at noon) in an array for TimeLord to use
tardis.SunRise(today); // calculate sunrise time
sunrisehour = today[tl_hour];
sunriseminute = today[tl_minute];
mSunrise = today[2] * 60 + today[1];
tardis.SunSet(today); // calculate sunset time
sunsethour = today[tl_hour];
sunsetminute = today[tl_minute];
mSunset = today[2] * 60 + today[1];
minOfDay = hour(epochtime) * 60 + minute(epochtime);
if (minOfDay < mSunrise || minOfDay >= mSunset){
isDay = 0;
isNight = 1;
if (!dimmedoff){
[b]ledfadeOFF();[/b]
Serial.println("Started Sunset Sequence");
client.publish(mqttDimmingTopic, String("Started Sunset Sequence").c_str(), true);
}
}
else {
isDay = 1;
isNight = 0;
if (!dimmedon){
[b]ledfadeON()[/b];
Serial.println("Started Sunrise Sequence");
client.publish(mqttDimmingTopic, String("Started Sunrise Sequence").c_str(), true);
}
}
if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!");
delay(1000);
return;
}
else {
}
if (!client.connected()) {
Serial.println("Connecting to MQTT server");
if (client.connect(sensorHostname)) {
client.subscribe(inTopic);
} else {
Serial.println("Could not connect to MQTT server");
digitalWrite(2,HIGH); //Disable onboard LED when mqtt server connection has failed to initialize.
delay(1000);
return;
}
}
if (client.connected()) {
client.loop();
digitalWrite(2,LOW); //Enable onboard LED when mqtt server connecton is present.
if (millis() - lastUpdate >= updateInterval) {
if (measureAndPublish()) {
lastUpdate = millis();
} else {
// retry earlier on error
lastUpdate = millis() - updateInterval + retyInterval;
}
}
}
}