switch button problem

This is my code:

const int LED = 13;
const int BUTTON = 2;
const int LED1 = 8;
const int BUTTON1 = 3;

int var = 0;

int val = 0;
int old_val = 0;
int state = 0;

void setup(){
pinMode (LED,OUTPUT);
pinMode (BUTTON,INPUT);
pinMode (LED1,OUTPUT);
pinMode (BUTTON1,INPUT);
}

void loop(){
val = digitalRead(BUTTON);

if ((val == HIGH) && (old_val == LOW)){
digitalWrite(LED,HIGH);
delay(5000);

digitalWrite(LED,LOW);
}
val = digitalRead(BUTTON1);
if ((val == HIGH) && (old_val == LOW)){
digitalWrite(LED1,HIGH);
delay(5000);

digitalWrite(LED1,LOW);
}
}

what I want to add to my code is to push the two button both at the same time. Or for example, I push the button1 first and after 2 seconds I will push the other button. Is it possible? Because my output is when I push the first button I will wait 5 seconds again to push the second button.

You need to stop using delay() as that prevents anything happening during delay period. Look at
Several things at the same time for some ideas about how to use millis() for timing.

UKHeliBob:
You need to stop using delay() as that prevents anything happening during delay period. Look at
Several things at the same time for some ideas about how to use millis() for timing.

will you help me construct my sketch making it to millis? I already review that link you gave. Its just so hard to reconstruct my codes.. :smiley:

Can you please describe what you want to do in more detail ?

What should happen when you press button 1 ?
What should happen when you subsequently press button 2 ?
What should happen if you press button 2 first ?
What has the 2 seconds got to do with it ? Perhaps button 2 has to be pressed within 2 seconds of button 1 ?

UKHeliBob:
Can you please describe what you want to do in more detail ?

I have two outputs, each outputs have their own switch and if I pressed the first switch(to ON the output1) and while the OUTPUT1 is ON I can press the second switch to ON the OUTPUT2 so they can both(work) ON at the same time.

So, no 2 second timing period. What are the delay()s for in your original program ?

UKHeliBob:
So, no 2 second timing period. What are the delay()s for in your original program ?

There is no 2 seconds timing period it was just my example. My delays are to ON my outputs for 5 secs.

So, the LED turns on for a period (say 5 seconds) but only after both buttons have been pressed. Button 1 must be pressed first and held whilst button 2 is pressed or held. Is that right ?

UKHeliBob:
So, the LED turns on for a period (say 5 seconds) but only after both buttons have been pressed. Button 1 must be pressed first and held whilst button 2 is pressed or held. Is that right ?

No sir. My OUTPUT is when I press either button 1 or button 2 it will turn on I'd say I pressed the button2 the OUTPUT2 will ON for 5 seconds and while output2 is ON I can press button1 to ON the output1. Sorry sir I can't explain well what I want to explain. Poor english me. HAHA :smiley:

So, 2 inputs and 2 outputs. Pressing button 1 turns on output 1 for a period. Pressing button 2 turns on output 2 for a period.

You need to look into using millis() for timing as in the BlinkWithoutDelay example.
Save the time from millis() that the button becomes pressed and turn on the LED. Then each time through loop() check whether the required period has elapsed since the button became pressd. If not then go round loop() again reading inputs etc. If the period has elapsed then turn off the LED.

You will need to record the time each button is pressed in 2 separate variables

UKHeliBob:
So, 2 inputs and 2 outputs. Pressing button 1 turns on output 1 for a period. Pressing button 2 turns on output 2 for a period.

You need to look into using millis() for timing as in the BlinkWithoutDelay example.
Save the time from millis() that the button becomes pressed and turn on the LED. Then each time through loop() check whether the required period has elapsed since the button became pressd. If not then go round loop() again reading inputs etc. If the period has elapsed then turn off the LED.

You will need to record the time each button is pressed in 2 separate variables

int inPin = 2;
int outPin = 13;

int state = LOW;
int reading;
int previous = HIGH;

long time = 1000;
long debounce = 200;

void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}

void loop()
{
reading = digitalRead(inPin);

if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}

digitalWrite(outPin, state);

previous = reading;
}

how to add button as input here? don't know where to start.

how to add button as input here?

Change

 pinMode(inPin, INPUT);

to

 pinMode(inPin, INPUT_PULLUP);  //turn on the built in pullup resistor

change

if (reading == HIGH && previous == LOW && millis() - time > debounce) {

to

 if (reading == LOW && previous == HIGH && millis() - time > debounce) {

Wire the button to pin 2 so that pin 2 is taken LOW (to GND) when the button is pressed.
When you get that working you can tidy up the program.