ich habe da was fertiges, das ich so oder in ähnlicher Form schon öfters gepostet habe.
Hier ein Beispiel mit 3 Objekten - mit unterschiedlichen Blinkmustern.
Das was du suchst ist die RhythmLed
/* Blink a LED in a specific rhythm
Turns on and off light emitting diodes (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.
noiasca
2019-05-05
*/
class BlinkLed {
byte state = 1; // 1 blink, 0 off
unsigned long previousMillis;
uint16_t on = 180;
uint16_t off = 320; // 180/320 is according ECE
const byte ledPin;
public:
BlinkLed(byte attachTo):
ledPin(attachTo)
{}
void begin()
{
pinMode(ledPin, OUTPUT);
}
void set(uint16_t _on, uint16_t _off) // modify on/off times during runtime
{
on = _on;
off = _off;
}
void setState(byte _state) // 1 Switch on blinking; 0 switch off blinking
{
state = _state;
}
void tick()
{
if (state)
{
uint32_t currentMillis = millis();
if (digitalRead(ledPin) && currentMillis - previousMillis >= on) {
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(ledPin, LOW);
}
if (!digitalRead(ledPin) && currentMillis - previousMillis >= off) {
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(ledPin, HIGH);
}
}
}
};
class RhythmLed {
byte state = 1;
unsigned long previousMillis;
const byte ledPin;
uint8_t lengthOfPattern;
// the intervall pattern
// ON OFF ON OFF
// 1 2 3 4
uint16_t intervall[5] = {0, 150, 60, 20, 270}; // ECE 2
//uint16_t intervall[3] = {0, 180, 320}; // ECE 1
//uint16_t intervall[7] = {0, 25, 25, 25, 25, 25, 375}; // HELLA 3
//uint16_t intervall[9] = {0, 40, 40, 40, 40, 40, 40, 40, 220 }; // HELLA 4
//uint16_t intervall[5] = {0, 150, 60, 20, 400}; // wie gewünscht vom TO
public:
RhythmLed(byte attachTo):
ledPin(attachTo)
{}
void begin()
{
pinMode(ledPin, OUTPUT);
lengthOfPattern = sizeof(intervall) / sizeof(intervall[0]);
}
void setState(byte _state)
{
state = _state;
}
void setIntervall(uint16_t _intervall1, uint16_t _intervall2, uint16_t _intervall3, uint16_t _intervall4) // Modify the intervalls to your needs
{
intervall[1] = _intervall1;
intervall[2] = _intervall2;
intervall[3] = _intervall3;
if (lengthOfPattern>3) intervall[4] = _intervall4; // if someone declares a dual-intervall, this line would crash the sketch, therefore this if on the index length ;-)
}
void tick()
{
uint32_t currentMillis = millis();
if (state > 0) // iterate through the states 1 to n, we don't use state 0 because state 0 is reserved to "switch off" the light
{
if (currentMillis - previousMillis > intervall[state])
{
if (state % 2 == 1) // all uneven states are ON, at the end of an intervall we switch to the oposite pin state
{
digitalWrite(ledPin, LOW);
}
else
{
digitalWrite(ledPin, HIGH);
}
state++;
if (state > lengthOfPattern - 1) state = 1;
previousMillis = currentMillis;
}
}
}
};
BlinkLed myBlinkLed(9); // a simple blink led
RhythmLed topRKL(13); // define an output PIN for your LED
RhythmLed outOffSyncRKL(10); // eine Rundumkennleuchte die wegen eines "Massefehlers" am Originalfahrzeugs eine leicht verschobene Blinkfrequenz hat ;-)
void setup()
{
myBlinkLed.begin();
topRKL.begin();
outOffSyncRKL.begin();
outOffSyncRKL.setIntervall(250, 90, 30, 400); // Modify the intervalls for this LED
}
void loop()
{
myBlinkLed.tick(); // each object has to be called once in the loop
topRKL.tick();
outOffSyncRKL.tick();
// put here other code which needs to run:
}