ArduinoLearn/Difficulty 01/serial-read-blink/serial-read-blink.ino

42 lines
778 B
C++

/*
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();
}
}