user50273: I want to code something like a "timer" (don't know what to call it) in an if else statement in my arduino uno. 
Basically I have 2 buttons. If the button1 is pressed and button2 is also pressed in a certain amount of time (let's say 3 seconds), the code will execute. If button1 is pressed but button2 is not pressed within 3 seconds then it will loop back up. How do I go about writing this code?
I've included a flowchart to make my question clearer. 
Thanks all :)
if(digitalRead(button1) == LOW)       // this is where I need help 
{
  if(digitalRead(selectbutton) == LOW)
  { 
    for(x=1; x<=30; x++)
    {
      digitalWrite(dir, HIGH);
      delayMicroseconds(10);
      time_now = millis();
      digitalWrite(led, HIGH);
      while(millis() < time_now + 50)
      {
      }

      time_now = millis();
      digitalWrite(led, LOW);
      while(millis() < time_now + 50)
      {
      }
    }
    x=0;
  }
}


else if(digitalRead(button2) == LOW)    //same here >_<
{
  if(digitalRead(selectbutton) == LOW)
  {
    for(x=1; x<=10; x++)
    {
      digitalWrite(dir, LOW);
      delayMicroseconds(10); 
      time_now = millis();
      digitalWrite(led, HIGH);
      while(millis() < time_now + 150)
      {
      }

      time_now = millis();
      digitalWrite(led, LOW);
      while(millis() < time_now + 150)
      {
      }
    }
    x=0;
  }
}

}
 

user50183: Your flowchart seems to be in conflict with the code.
button1 and button2 are self-explainatory, but what is the selectButton?
Here's some code which may accomplish what you're trying to do:
uint8_t bool_waitforbutton2 = 0;
uint32_t num_timeatbutton1 = 0;
void loop() {
  //your other looping code here
  if(digitalRead(button1) == LOW) {
    num_timeatbutton1 = millis();
    bool_waitforbutton2 = 1;
  }
  if(bool_waitforbutton2 == 1 && digitalRead(button2) == LOW){
    bool_waitforbutton2 = 0;
    //Your code here
  }
  if (millis() > (num_timeatbutton1 + 3000)) {
    bool_waitforbutton2 = 0;
  }
}

This would give you 3000 ms to press button2 after you've let go of button1 to execute your code, otherwise the "readyness-flag" bool_waitforbutton2 is reset.

user7508: A flowchart is not bad, but it's not really the most appropriate kind of
diagram for this sort of problems. You should try to think in terms of
finite state machines instead, which is a very useful concept
whenever you try to implement “reactive” systems, i.e. systems that have
to react to some kind of input.
Here is your problem, as I understand it, translated into a state
diagram:

This system has two states:

“waiting”, when it waits for the first button to be pressed
“stand by” when the first button has been pressed and it waits for the
second.

Pressing button 1 makes a transition from “waiting” to “stand by”. The
transition back to the initial “waiting” state can be triggered by two
distinct events:

3 seconds spent in the “stand by” state
the user pressing button 2.

Note that in the second case, the system runs your code while making the
transition.
Once you have the state diagram, and assuming you have read (and
understood) the finite state machine tutorial I linked to, translating
the diagram into code is almost trivial.

50273: <p>I want to code something like a "timer" (don't know what to call it) in an if else statement in my arduino uno. </p>

<p>Basically I have 2 buttons. If the button1 is pressed and button2 is also pressed in a certain amount of time (let's say 3 seconds), the code will execute. If button1 is pressed but button2 is not pressed within 3 seconds then it will loop back up. How do I go about writing this code?</p>

<p>I've included a flowchart to make my question clearer. </p>

<p>Thanks all :)</p>

<pre><code>if(digitalRead(button1) == LOW)       // this is where I need help 
{
  if(digitalRead(selectbutton) == LOW)
  { 
    for(x=1; x&lt;=30; x++)
    {
      digitalWrite(dir, HIGH);
      delayMicroseconds(10);
      time_now = millis();
      digitalWrite(led, HIGH);
      while(millis() &lt; time_now + 50)
      {
      }

      time_now = millis();
      digitalWrite(led, LOW);
      while(millis() &lt; time_now + 50)
      {
      }
    }
    x=0;
  }
}


else if(digitalRead(button2) == LOW)    //same here &gt;_&lt;
{
  if(digitalRead(selectbutton) == LOW)
  {
    for(x=1; x&lt;=10; x++)
    {
      digitalWrite(dir, LOW);
      delayMicroseconds(10); 
      time_now = millis();
      digitalWrite(led, HIGH);
      while(millis() &lt; time_now + 150)
      {
      }

      time_now = millis();
      digitalWrite(led, LOW);
      while(millis() &lt; time_now + 150)
      {
      }
    }
    x=0;
  }
}
</code></pre>

<p>}</p>

<p><a href="https://i.stack.imgur.com/L162c.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/L162c.jpg" alt="enter image description here"></a> </p>
 50183: <p>Your flowchart seems to be in conflict with the code.
<code>button1</code> and <code>button2</code> are self-explainatory, but what is the <code>selectButton</code>?</p>

<p>Here's some code which may accomplish what you're trying to do:</p>

<pre><code>uint8_t bool_waitforbutton2 = 0;
uint32_t num_timeatbutton1 = 0;
void loop() {
  //your other looping code here
  if(digitalRead(button1) == LOW) {
    num_timeatbutton1 = millis();
    bool_waitforbutton2 = 1;
  }
  if(bool_waitforbutton2 == 1 &amp;&amp; digitalRead(button2) == LOW){
    bool_waitforbutton2 = 0;
    //Your code here
  }
  if (millis() &gt; (num_timeatbutton1 + 3000)) {
    bool_waitforbutton2 = 0;
  }
}
</code></pre>

<p>This would give you 3000 ms to press <code>button2</code> after you've let go of <code>button1</code> to execute your code, otherwise the "readyness-flag" <code>bool_waitforbutton2</code> is reset.</p>
 7508: <p>A flowchart is not bad, but it's not really the most appropriate kind of
diagram for this sort of problems. You should try to think in terms of
<a href="https://hackingmajenkoblog.wordpress.com/2016/02/01/the-finite-state-machine/" rel="nofollow noreferrer">finite state machines</a> instead, which is a very useful concept
whenever you try to implement “reactive” systems, i.e. systems that have
to react to some kind of input.</p>

<p>Here is your problem, as I understand it, translated into a state
diagram:</p>

<p><a href="https://i.stack.imgur.com/HJicg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HJicg.png" alt="state diagram"></a></p>

<p>This system has two states:</p>

<ul>
<li>“waiting”, when it waits for the first button to be pressed</li>
<li>“stand by” when the first button has been pressed and it waits for the
second.</li>
</ul>

<p>Pressing button 1 makes a transition from “waiting” to “stand by”. The
transition back to the initial “waiting” state can be triggered by two
distinct events:</p>

<ul>
<li>3 seconds spent in the “stand by” state</li>
<li>the user pressing button 2.</li>
</ul>

<p>Note that in the second case, the system runs your code while making the
transition.</p>

<p>Once you have the state diagram, and assuming you have read (and
understood) the finite state machine tutorial I linked to, translating
the diagram into code is almost trivial.</p>
