0

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); 
}
  • Why don't you want to use if/else? Or is this a school assignment? – chrisl Sep 23 '19 at 6:07
  • it is a school assignment actually – Sam Abdul Sep 23 '19 at 6:08
  • 1
    switch case is your friend ;) – ElectronSurf Sep 23 '19 at 6:30
  • 1
    Switch is an alternative to if --- unless are you at the "interrupts" section of your class... – GMc Sep 23 '19 at 8:20
  • 1
    @FilipFranik I agree and did not advocate that. You definitely don't want to include any lengthy operations in an interrupt service routine. My comment was more of a suggestion that switch is an obvious alternative to using an if - but it is also possible that the OP was at the "interrupts" section of their class and thus would require a completely different approach to the serial code as posted in the question - and as you suggest, definitely should not be using delay. Hopefully that was also taught in the class... I should perhaps in hindsight been more clear in my original comment. – GMc Oct 20 at 9:15
0

As the required operation is finite and short, you can just hard code the sequence.

| improve this answer | |
0

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);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.