Cycle on and off leds

Okay, i need some help. Ill try to explain. Im trying to make some leds stay on for a certain time and then go off for a certain time. Over a 24 hour period. I would first like to see this in seconds, so that i know that it works.

This method will take in either 12.12, 18.6, 20.4 or 6.18.
Is then stored in lightcycleon and lightcycleoff. Ive tried using f.eks TimeAlarms. But i cant figure out how id cycle it properly, because the 2 alarms would interrupt eachother… hard to explain. Im not sure if im overthinking or underthinking this.

(Ive removed TimeAlarms and anything like that.) (Everything below is just how far ive got)

int setLedCycle(String cycle)
{
    Serial.println("setLightCycle entered" );
    Serial.println(cycle);
    cycle.toCharArray(paramBuf, BUFFER_SIZE);
    char *pch = strtok(paramBuf, ".");  
    lightcycleon = atoi(pch);  
    Serial.println(lightcycleon);
    pch = strtok (NULL, ".");
    lightcycleoff = atoi(pch);
    Serial.println(lightcycleoff);

}

int setLedState(String lightValue) {
  Serial.println("Test");
    lightState = lightValue.toInt();
    if(lightState==1) {
      digitalWrite(D0, HIGH);
    } else if(lightState==0){
      digitalWrite(D0, LOW);
    }
    return 0;

}

void cycleOn() {
digitalWrite(D0, HIGH);
Serial.println("D0 set to high : LED ON!!");

}

void cycleOff(){
digitalWrite(D0, LOW);
Serial.println("D0 set to LOW : LED OFF!!");

}

Thanks alot for any answer :slight_smile:

AFAICT TimeAlarmsis for points in time but not arbitrary periods between two points in time.

To properly address this we’d need to see how you currently set the alarm times after you received the new settings.

Are you wanting to perform the Actions at Specific Times of the Day (at the Same time each Day)?
Or do you want to perform the Actions based from when the Particle Device was last Reset ?

The TimeAlarm Library is what you want for Specific Times of the Day, see Post 26:
https://community.particle.io/t/help-with-setting-photon-schedules/41267/26

Millis() is what you use for “Runtime” since Reset.

You can try this:

1 Like