1

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;
  }
}

}

enter image description here

  • Hi, I've edited my post. Hope you can help. :) – BreadPig Nov 13 '18 at 9:23
2

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.

  • sorry for not clarifying, button1 is for choosing the first function, and selectbutton is for confirming to run function 1. Likewise for button2, it is to select function 2, and selectbutton is to confirm. – BreadPig Nov 14 '18 at 1:12
4

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:

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.

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.