noiasca:
so als Grundgerüst mit unsyncronisierter Top-Leuchte
// Lighthouse with 8 LEDs and one top PWM
// https://forum.arduino.cc/index.php?topic=711838
// by noiasca
define DEBUG_UART 1
const uint8_t pwmPin = 3; // UNO PWM pins 3, 5, 6, 9, 10, 11
const uint8_t ledPin[] = {2, 4, 5, 6, 7, 8, 9, 10};
const size_t totalNoLeds = sizeof(ledPin);
uint32_t currentMillis = 0;
void doRunning() // switch on / off one LED after the other
{
static uint8_t actual = totalNoLeds - 1; // start with the last LED
static uint32_t previousMillis = 0;
const uint8_t myIntervall = 5; // intervall in seconds
if (currentMillis - previousMillis >= myIntervall * 1000UL)
{
previousMillis = currentMillis;
digitalWrite(ledPin[actual], LOW);
actual++;
if (actual >= totalNoLeds) actual = 0;
digitalWrite(ledPin[actual], HIGH);
if DEBUG_UART
Serial.print("actual Pin=");
Serial.println(ledPin[actual]);
endif
}
}
void doPWM()
{
static uint32_t previousMillis = 0;
const uint16_t myIntervall = 10; // speed of PWM
static int8_t dir = 1; // direction upwards(1) or downwards (-1)
static uint8_t newPwm;
if (millis() - previousMillis >= myIntervall)
{
previousMillis = millis();
if (dir == 1 && newPwm >= 254) // end of scale
dir = -1; // go downwards
if (dir == -1 && newPwm <= 54) // lower end of pwm scale
dir = 1; // go upwards
newPwm += dir;
analogWrite(pwmPin, newPwm);
if DEBUG_UART
Serial.print("newPwm=");
Serial.println(newPwm);
endif
}
}
void setup() {
if DEBUG_UART
Serial.begin(115200);
endif
for (uint8_t i = 0; i < totalNoLeds; i++) {
pinMode(ledPin[i], OUTPUT);
}
pinMode(pwmPin, OUTPUT);
}
void loop() {
currentMillis = millis();
doPWM(); // check, if PWM needs to be changed
doRunning(); // check, if one of the running LEDs need to be changed
// do what ever you want unblocked here
}
Grundgerüst? Das ist genau die Funktion die ich brauche vielen dank, jetzt muss ich es nur noch verstehen. :D