unsigned long onperiod = 1000; // variable - set this to desired on time
unsigned long offperiod = 2000; // variable - set this to desired off time
byte led = 2; // led connected to this pin
/*
- I call this "millistime" because I use the variable "time1" often in my other code
- millistime1 can be assigned to a trigger
*/
byte ledstate = LOW;
unsigned long millistime1;
unsigned long millistime2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("power on");
pinMode(led,OUTPUT);
}
void loop() {
millistime2 = millis() - millistime1;
if(millistime2 == offperiod && ledstate == LOW){
Serial.print("ON FOR:");
Serial.print(onperiod);
Serial.println(" MILLIS");
millistime1 = millis();
ledstate = HIGH;
digitalWrite(led,HIGH);
}
if(millistime2 == onperiod && ledstate == HIGH){
Serial.print("OFF FOR:");
Serial.print(offperiod);
Serial.println(" MILLIS");
millistime1 = millis();
ledstate = LOW;
digitalWrite(led,LOW);
}
}