Bei millis()
wird ein Zähler jede Millisekunde eins rauf gezählt. Unterschiedliche Aufgaben sind mit statischen Variablen, die zu millis()
eine Differenz bilden, möglich:
const byte tasterPin = 2;
const byte led1Pin = 8;
const byte led2Pin = 12;
const byte led3Pin = 13;
uint32_t jetzt;
void setup()
{
pinMode (tasterPin, INPUT_PULLUP);
pinMode (led1Pin, OUTPUT);
pinMode (led2Pin, OUTPUT);
pinMode (led3Pin, OUTPUT);
}
//
void loop()
{
jetzt = millis();
blink1();
blink2( !digitalRead(tasterPin) );
blink3();
}
void blink1()
{
const uint32_t intervall = 111;
static uint32_t vorhin = 0;
if (jetzt - vorhin >= intervall)
{
vorhin = jetzt;
digitalWrite(led1Pin, !digitalRead(led1Pin));
}
}
void blink2(bool taster)
{
const uint32_t intervall = 500;
static uint32_t vorhin = 0;
if (taster)
{
if (jetzt - vorhin >= intervall)
{
vorhin = jetzt;
digitalWrite(led2Pin, !digitalRead(led2Pin));
}
}
else
{
digitalWrite(led2Pin, LOW);
}
}
void blink3()
{
const uint32_t intervall = 200;
static uint32_t vorhin = 0;
if ( jetzt != vorhin && !(jetzt % intervall) )
{
vorhin = jetzt;
digitalWrite(led3Pin, !digitalRead(led3Pin));
}
}