I want to let the led blink for 3 seconds then 2 seconds then one second using the button one time and without using if/else
void setup() {
// put your setup code here, to run once:
pinMode(5, INPUT);
pinMode(6, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalRead(5, HIGH);
digitalWrite(6, HIGH);
delay(3000);
digitalWrite(6, LOW);
digitalWrite(6, HIGH);
delay(2000);
digitalWrite(6, LOW);
digitalWrite(6, HIGH);
delay(1000);
}
3 Answers 3
As the required operation is finite and short, you can just hard code the sequence.
answered Sep 23, 2019 at 11:27
You could you simple inversion of boolean variable to set state of LED.
const int LedPin = 5;
bool ledState = true;
// in your loop
// bool will be transformed from true to 1 (HIGH)
digitalWrite(LedPin, (int)ledState);
// invert LED state to be false
ledState = !ledState;
// false -> 0 (LOW)
digitalWrite(LedPin, (int)ledState);
answered Oct 19, 2020 at 7:00
Ternary operator maybe helpful. [Reference]
answered Nov 18, 2020 at 11:52
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.sempaiscuba– sempaiscuba2020年11月18日 12:53:19 +00:00Commented Nov 18, 2020 at 12:53
lang-cpp
switch case
is your friend ;)