toos-halloween/Arduino/Blink/Blink.ino

42 lines
1.2 KiB
Arduino
Raw Permalink Normal View History

2023-10-04 13:47:55 +02:00
const int ledPin = 4; // Define the LED pin
const int minOnTime = 100; // Minimum time LED is ON in milliseconds
const int maxOnTime = 3000; // Maximum time LED is ON in milliseconds
const int minOffTime = 50; // Minimum time LED is OFF in milliseconds
const int maxOffTime = 200; // Maximum time LED is OFF in milliseconds
unsigned long previousMillis = 0;
int ledState = LOW;
unsigned long onTime = 0;
unsigned long offTime = 0;
void setup() {
pinMode(ledPin, OUTPUT);
generateRandomTimes();
}
void loop() {
unsigned long currentMillis = millis();
if (ledState == LOW) {
if (currentMillis - previousMillis >= offTime) {
// Turn the LED on
ledState = HIGH;
previousMillis = currentMillis;
generateRandomTimes();
}
} else {
if (currentMillis - previousMillis >= onTime) {
// Turn the LED off
ledState = LOW;
previousMillis = currentMillis;
generateRandomTimes();
}
}
digitalWrite(ledPin, ledState);
}
void generateRandomTimes() {
onTime = random(minOnTime, maxOnTime + 1); // Generate a random ON time
offTime = random(minOffTime, maxOffTime + 1); // Generate a random OFF time
}