read time led blink from serial

This commit is contained in:
Alexander Popov 2023-02-24 20:57:09 +03:00
parent b12135c779
commit 9444a2fe40
Signed by: iiiypuk
GPG Key ID: D8C9B59A9F04A70C

View File

@ -0,0 +1,41 @@
/*
Blink BUILTIN LED example
Author: Alexander Popov
License: Unlicense
*/
int user_input;
int delay_time = 1000;
bool led_state = HIGH;
unsigned long timer = 0;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read blink delay
if (Serial.available()) {
user_input = Serial.parseInt();
if (user_input >= 100) {
delay_time = user_input;
Serial.print("Delay set to ");
Serial.print(user_input);
Serial.println(" ms");
}
else if (user_input > 0 && user_input < 100) {
Serial.println("Error: set delay time >=100ms");
}
}
// blink LED
if (millis() - timer > delay_time) {
led_state =! led_state;
digitalWrite(LED_BUILTIN, led_state);
timer = millis();
}
}