#include <math.h>
class Led
{
// Class Variables
int Ledpin; // The pin to control the LED
int State; // The state of the LED
unsigned long PrevMillis; // Last time the LED was updated
long ONPeriod; // How long the LED will be ON
long OFFPeriod; // How long the LED will be OFF
// Class Constructors
public:
Led(int pin, long on, long off)
{
Ledpin = pin;
ONPeriod = on;
OFFPeriod = off;
// Variables that needs to be passed by refrence
State = LOW;
PrevMillis = 0;
}
// Class Methods
void Update()
{
// Initialize time
unsigned long currentMillis = millis();
if ((State == HIGH) && ((currentMillis - PrevMillis) >= ONPeriod)){
PrevMillis = currentMillis;
State = LOW;
}else if ((State == LOW) && ((currentMillis - PrevMillis) >= OFFPeriod)){
PrevMillis = currentMillis;
State = HIGH;
}
digitalWrite(Ledpin, State);
}
};
Led led1(2, 1000, 500);
Led led2(3, 500, 1000);
void setup() {
}
void loop() {
led1.Update();
led2.Update();
}