"
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.
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.
}
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.