# Demonstration code for several things at the same time

**URL:** <https://forum.arduino.cc/t/demonstration-code-for-several-things-at-the-same-time/217158>\
**Category:** General Guidance\
**Created:** [March 4, 2014, 12:28pm UTC](https://forum.arduino.cc/t/demonstration-code-for-several-things-at-the-same-time/217158 "2014-03-04T12:28:03Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Jiggy-Ninja](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Jiggy-Ninja](https://forum.arduino.cc/u/Jiggy-Ninja)\
**Post date:** [March 9, 2014, 11:34pm UTC](https://forum.arduino.cc/t/demonstration-code-for-several-things-at-the-same-time/217158/4 "2014-03-09T23:34:25Z")

</div>

I prefer using switch statements for may state variables rather than ifs, even if there's only two states something can be in. I find it easier to follow than trying to match an else with the right if to find out what's going on, especially if they're nested.

So instead of:

```cpp
void updateLed_A_State() {

  if (led_A_State == LOW) {
    if (currentMillis - previousLed_A_Millis >= led_A_Interval) {
      led_A_State = HIGH;
      previousLed_A_Millis = currentMillis;
    }
  }
  else {
    if (currentMillis - previousLed_A_Millis >= blinkDuration) {
      led_A_State = LOW;
      previousLed_A_Millis = currentMillis;
    } 
  }    
}

```

I prefer:

```cpp
void updateLed_A_State()
{
  switch( led_A_State )
  {
    case LOW:
      if (currentMillis - previousLed_A_Millis >= led_A_Interval)
      {
        led_A_State = HIGH;
        previousLed_A_Millis = currentMillis;
      }
    
    case HIGH:
      if (currentMillis - previousLed_A_Millis >= blinkDuration) 
      {
        led_A_State = LOW;
        previousLed_A_Millis = currentMillis;
      }
    default:
      ;
  }
}

```

I also prefer a different bracketing style to yours, but that's neither here nor there.

---

_[View the full topic](https://forum.arduino.cc/t/demonstration-code-for-several-things-at-the-same-time/217158)._
