read time led blink from serial
This commit is contained in:
parent
b12135c779
commit
9444a2fe40
41
Difficulty 01/serial-read-blink/serial-read-blink.ino
Normal file
41
Difficulty 01/serial-read-blink/serial-read-blink.ino
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user