Arduino Uno Code - If.........then......if........action

Hi Guys,

Any help on my project would be greatly appreciated.

I have a series of switches and I want to be able to code the following

if switch 1 was pressed and switch 2 is pressed then turn on LED.

I have stumbled across some old forums about very similar topics but haven't found a clear answer yet.

The closest I have got is the following:

A post by 'Jassper' on this thread:
http://webcache.googleusercontent.com/search?q=cache:5n8Be6c5me8J:www.arduino.cc/cgi-bin/yabb2/YaBB.pl%3Fnum%3D1233604194+arduino+if+then+if&cd=2&hl=en&ct=clnk&gl=uk&source=www.google.co.uk

"
So the logic is different depending on;
Press Button 1, release button 1 then press button 2

  • or -
    Press button 1 HOLD, press button 2

If you must hit button one first, release, then see if button 2 is pressed, then you need a delay to give yourself time to press button 2,
Code:
if(Button1)
{
// Do something;
// Wait to see if button 2 is pressed within 5 sec
delay(5000);
if(Button2)
{
// do more;
}
}

Here if button 2 is not pressed within 5 sec after button 1 is press, it will skip over the second if statement.

Or, alternativly, you can set/check a variable.
Code:
if(Button1)
{
// Set variable button 1 was pressed
iB1WasPressed = 1;
}

if(Button2 && iB1WasPressed)
{
// Button 2 pressed, after button 1 was press and released
// Set var back to 0
iB1WasPressed = 0;
// Do more;
}
"

The latter part of this code is what I want to achieve, but how would I set variables for this?
Apologies I am a newb.

Thanks guys!

Mack

The first sample just won't work. delay() is not the method to use because the Arduino cannot do anything during the delay.

The second sample is the way to go. All you are missing is a way to reset your iB1WasPressed back to false after 5 seconds. Look at the BlinkWithoutDelay tutorial sketch to see how to use millis() to perform some action after a period of time has elapsed. In your case, instead of repeatedly blinking an LED, you'll be resetting a variable 5 seconds after a button has been released.

For most programs like this you want to create what is called a "state machine".

Your program can be in one of several states: Idle, Button1 pressed, LED on, etc. The actions to check for depend on what the current state is. Each action might move the program to a different state.

void setup() {state = IDLE;}

void loop()
   {
   switch (state)
       {
   case IDLE:
      if (Button1 is pressed) state = BUTTON1PRESSED;
     break;

   case BUTTON1PRESSED:
      if (Button2 is pressed)
        {
        Turn on LED;
        state = LEDON;
        }
      break;

    case LEDON:
       break;
      }

   }

I'd do it using millis(), which is the call that returns how long the arduino has been on for as an unsigned long (good for around 50 days).

Nice simple code.

so:

long lastPressed

int buttonOne = 2;
int buttonTwo = 3;
int onboardLED = 13;

void setup()
{
  pinMode(buttonOne, INPUT);
  pinMode(buttonTwo, INPUT);
  pinMode(onboardLED, OUTPUT);
  delay(5000);  // this prevents you from being able to press button two right at the start of the sketch and still turn on the led because millis() is less than 5000.  see code below.
}

void loop()
{
  if (digitalRead(buttonOne) == 1) lastPressed = millis();  // set lastPressed to the current run time of the arduino
  if (digitalRead(buttonTwo) == 1 && (millis() - lastPressed) < 5000) digitalWrite(onboardLED, HIGH);  //  is the current run time less than 5 seconds after the we last pressed button one?  If so, turn on the LED.
}

Thanks a lot guys for this help,

I will experiment!

Mack

I will experiment!

Music to my ears!

Someone who doesn't expect completed, application-specific, working code.
Thank you - you've made my day and reaffirmed my faith.

AWOL:

I will experiment!

Music to my ears!

Someone who doesn't expect completed, application-specific, working code.
Thank you - you've made my day and reaffirmed my faith.

hear hear, well said :slight_smile:

Even if you need some base code like I gave above, it should only be enough to get you going.

You guys are really very helpful. Sometime, peoples comes asking questions getting replies but majority do not post about their completed projects. Mostly, they say we did it and get away never come again.
I really respect you guys ..You help others without any return.
My regards to you.

Thanks for your help guys,

I have managed to get the basic features I want in my project now, I'll carry on with the tweaking.

If you are interested the results of my work so far watch this video

As a reward for your help :stuck_out_tongue: its very exciting.

Mack