Help. Two Button LED control !

Hi there,

I was trying to control one LED With Two buttons as follows

1- when you push the ON button the LED will lit for 2 seconds then off 5 seconds and then On for 10 seconds then stays off.

the problem I couldn't activate the Off button the LED On- Off Cycle.

I hope some one will help.

Post what you have tried. Use code tags or else no dice.

this is the code I Wrote

int ledPin = 13;
int buttonApin = 9;
int buttonBpin = 8;

byte leds = 0;

void setup() 
{
 pinMode(ledPin, OUTPUT);
 pinMode(buttonApin, INPUT_PULLUP);  
 pinMode(buttonBpin, INPUT_PULLUP);  
}

void loop() 
{
 if (digitalRead(buttonApin) == LOW)// when you push button A
 {
   digitalWrite(ledPin, HIGH);
   delay(2000);
   digitalWrite(ledPin, LOW);
   delay(5000);
   digitalWrite(ledPin, HIGH);
   delay(10000);
   digitalWrite(ledPin,LOW);               
 }                                                     // the end of the cycle of button A
 if (digitalRead(buttonBpin) == LOW) // when you push button B
 {
   digitalWrite(ledPin, LOW);
 }
}

the problem is I can't make button B is the Master button
I mean it can control without waiting for cycle A to be completed.

No code tags, no dice. Like I said.
Read the how to use this forum sticky.

You need to stop using delay() for timing because nothing else can happen until the delay() is finished. So, in your program once buttonApin goes LOW buttonBpin will not be read for 17 seconds whilst the LED does its thing.

So, what can you do ? The answer is to use millis() for timing. millis() provides a snapshot of the time now, so if you save the time something happened (ledPin HIGH, for instance) you can check each time through loop() whether the time now minus the time started is greater than say 2 seconds. If not, got round loop() again until it is, reading buttonBpin each time and reacting to it. If the period has elapsed take ledPin LOW, save the time and start timing again and so on until the actions are complete.

This is commonly known as the BlinkWithoutDelay principle. The name is taken from the example sketch of that name in the IDE. A more complicated example can be found at Several things at the same time

Thank you UKHeliBob, i will try.

I just posted this example in another thread. It may help you understand the principle of BWD applied to reading an input whilst timing LED on/off periods.

const byte buttonPin = A1;
const byte ledPin = 13;
boolean flashing = false;
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;
unsigned long currentTime;
unsigned long onoffStarted;
long flashPeriod = 1000;

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != previousButtonState && currentButtonState == LOW)  //button has become pressed
  {
    flashing = !flashing;
  }
  previousButtonState = currentButtonState;          //save the button state for the next check

  if (flashing)
  {
    currentTime = millis();
    if (currentTime - onoffStarted > flashPeriod)  //if the flash period ended ?
    {
      digitalWrite(ledPin, !digitalRead(ledPin));  //flip the state of the LED
      onoffStarted = currentTime;                  //save the time when it occured
    }
  }
  else
  {
    digitalWrite(ledPin, HIGH);  //force the LED off if not flashing
  }
}

Thank You good example