aha also doch Stepper. Ist zu spät jetzt.
// Lighthouse with 8 LEDs and one top PWM
// https://forum.arduino.cc/index.php?topic=711838
// by noiasca
// 2020-11-04 V3: Motor switch on/off
// 2020-11-03 V2: LDR
#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}; // Pins simulating a "running light"
const size_t totalNoLeds = sizeof(ledPin);
const uint8_t sensorPin = A1; // LDR for day/night/
const uint8_t adcDark = 30; // threashold LDR for dark/night
const uint8_t adcBright = 100; // threashold LDR for bright/day
uint32_t currentMillis = 0; // stores actual timestamp
const uint8_t motorPin = 11;
bool isDark()
{
static bool result = false;
int sensorValue = analogRead(sensorPin);
if (sensorValue < adcDark) result = true;
else if (sensorValue > adcBright) result = false;
return result;
}
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; // stores timestamp/millis of last change
const uint8_t myInterval = 5; // interval in seconds
if (isDark())
{
#if DEBUG_UART
Serial.println("dark/night");
#endif
if (currentMillis - previousMillis >= myInterval * 1000UL)
{
previousMillis = currentMillis;
digitalWrite(ledPin[actual], LOW); // switch off old (=current) LED
actual++; // goto next LED
if (actual >= totalNoLeds) actual = 0; // handle rollover to zero
digitalWrite(ledPin[actual], HIGH); // switch on new LED
#if DEBUG_UART
Serial.print("actual Pin=");
Serial.println(ledPin[actual]);
#endif
}
}
else
{
#if DEBUG_UART
Serial.println("bright/day"); // if day and pin is on switch pin off
#endif
if (digitalRead(ledPin[actual]) == HIGH)
digitalWrite(ledPin[actual], LOW);
}
}
void doPWM() // Pulse/fade the LED up and down
{
static uint32_t previousMillis = 0; // stores timestamp/millis of last change
const uint16_t myInterval = 10; // speed of PWM
static int8_t dir = 1; // direction upwards(1) or downwards (-1)
static uint8_t newPwm;
if (millis() - previousMillis >= myInterval)
{
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; // this will increase (+1) or decrease (-1) the PWM
analogWrite(pwmPin, newPwm);
#if DEBUG_UART
Serial.print("newPwm=");
Serial.println(newPwm);
#endif
}
}
void doMotor() // switch on / off one Motor
{
static bool previousState = false;
if (isDark())
{
if (previousState == LOW)
{
previousState = HIGH;
digitalWrite(motorPin, HIGH);
// oder eben Stepper hier einschalten dann aber auch aufpassen,
#if DEBUG_UART
Serial.println("motor on");
#endif
}
}
else
{
if (previousState == HIGH) {
previousState = LOW;
digitalWrite(motorPin, LOW);
// oder eben Stepper hier ausschalten
#if DEBUG_UART
Serial.println("motor off");
#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);
pinMode(motorPin, 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
doMotor(); // check, if the motor/LED should be switched on/off
// do what ever you want unblocked here
}
Tommy wird schon recht haben. Das wird blockieren wenn du nicht die Mobatools verwendest