switching on and switching off blinking LED

Hi .Pleases Can anyone help me code for blinking led code?
I have a circuit with two pushbuttons and one LED
I went the code to .When I switch ON pushbutton A the LED should start blanking continuously until I switch ON pushbutton B the LED switches OFF
Have compiled this code
1 int ledPin = 5; //LED
2 int buttonApin = 7;//Pushbutton A
3 int buttonBpin = 8; //Pushbutton B
4 byte leds = 0;
5 void setup(){
6 pinMode(ledPin, OUTPUT);
7 pinMode(buttonApin, INPUT_PULLUP);
8 pinMode(buttonBpin, INPUT_PULLUP);
9 void loop() {
10 if (digitalRead(buttonApin) == LOW)
11 for (int x = 0; x < ; x++) {
12 digitalWrite(ledPin, HIGH);
13 delay(1000);
14 digitalWrite(ledPin, LOW);
15 delay(1000);
16 if (digitalRead(buttonBpin) == LOW)
17 digitalWrite(ledPin, LOW);
18 }
19}
But I have two problems this code
1: when I switch on pushbutton B I can’t switch off the LED
2 The LED blinks 10 time

If responsiveness is a requirement here, you have to get rid of all the calls to delay().
Have a look at the blink without delay example in the IDE.

Please use code tags when posting code, and don't post line numbers

Hi. I manage to change the code using the blink without delay example

I can start the LED blinking by pushing the start pushbutton ,but I can't stop the blinking when I push stop pushbutton

how can the the LED blinking ?

Can any one help me

const int trigger = 7;  //Pushbutton A Start LED Blinking
const int trigger1 = 8; //Pushbutton B Stop LED Blinking
const int ledPin =  5;  //LED   

boolean ledState = LOW;
boolean previousButtonState = HIGH;
boolean previousButtonState1 = HIGH;
boolean buttonState = LOW;
boolean buttonState1 = LOW;


long duration = 0;

unsigned long previousMillis = 0; // constants won't change :        


const long interval = 500;

void setup(){  
  
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(trigger, INPUT_PULLUP);
  pinMode(trigger1, INPUT_PULLUP);
  
}

void loop(){

  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval && buttonState == HIGH ) 
  { 
    previousMillis = currentMillis;    
 
       if (ledState == LOW)
       {
         ledState = HIGH;      
         
       }
       else
       {
         ledState = LOW;
       }
   
    
    digitalWrite(ledPin, ledState);  // set the LED with the ledState of the variable:
  } 
    
  
  if(digitalRead(trigger) == LOW) // Switch ON  Pushbutton A To Start Blinking
  {
    if(previousButtonState == LOW)
  {     
           
    }
    else
    {
      buttonState = HIGH;
    }
    delay(150); //debouncing
    
    if(digitalRead(trigger1) == LOW) // Switch ON  Pushbutton A To Stop Blinking
    {
    if(previousButtonState1 == LOW)
    {     
           
    }
    else
    {
      buttonState1 = HIGH;
    }
    delay(150); //debouncing
    
   
   
    
                
  }
    
}
    
}

Hi AvtarS,

You need to blink your LED only after setting the "start" state. Basically you want logic like this:

Pseudo code:

Is "start" pressed then set "blink state" to "on";
else Is "stop" pressed then set "blink state" to "off";

Is "blink state" set "on" then do LED Blink;

Basically you only do the blink code only when you have a "start" condition. The buttons are used to set the condition. If you press both buttons, only the first condition will be set. You could change the code to only allow a "start" when the start is pressed and the stop is not, like this:

Pseudo code:

Is "start" pressed and "stop" not pressed then set "blink state" to "on";
Is "stop" pressed and "start" not pressed then set "blink state" to "off";

Is "blink state" set "on" then do LED Blink;

That will do what you want.

Hope that helps! :smiley:

Hi  I added   digitalWrite (ledState ,LOW  to switch OFF the LED from blinking  using the pushbutton B but still the LED not stop blinking 

 can this code be modified ?.

thank you

[code

const int trigger = 5;  //Pushbutton A Start LED Blinking
const int trigger1 = 4; //Pushbutton B Stop LED Blinking
const int ledPin =  3;  //LED   (Red)

boolean ledState = LOW;
boolean previousButtonState = HIGH;
boolean previousButtonState1= HIGH;
boolean buttonState = LOW;
boolean buttonState1 = LOW;




long duration = 0;

unsigned long previousMillis = 0; // constants won't change :        


const long interval = 500;

void setup(){  
  
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
 
  pinMode(trigger, INPUT_PULLUP);
  pinMode(trigger1, INPUT_PULLUP);
  
  
}

void loop(){   
 
  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis >= interval && buttonState == HIGH  ) 
  
  { 
    previousMillis = currentMillis;    
 
       if (ledState == LOW)
       {
         ledState = HIGH;      
         
       }
       else
       {
         ledState = LOW;
       }
   
    
    digitalWrite(ledPin, ledState);  // set the LED with the ledState of the variable:
 
  } 
    
  
  if(digitalRead(trigger) == LOW) // Switch ON  Pushbutton A To Start Blinking
  {
    if(previousButtonState == LOW)
  {     
           
    }
    else
    {
      buttonState = HIGH;
    }
    delay(150); //debouncing 
           
    
    if(digitalRead(trigger1) == LOW) // Switch ON  Pushbutton B To stop Blinking
  {
    if(previousButtonState == LOW)
    
  {     
           
    }
    else
    {     
      buttonState1 = HIGH;
      
    }
    delay(150); //debouncing
    
    digitalWrite(ledState , LOW); // set the LED OFF
    
    

  }

  }

  }
]

Well I tried to help you so you could figure out the logic yourself, but I see you want the direct answer.

Ok. Here is a possible solution:

#define LED_PIN      0
#define START_BUTTON 1
#define STOP_BUTTON  2

// start off with the flashState "off"
boolean flashState = false;
int ledState = LOW;

unsigned long prevMillis = 0;
const long interval = 1000;

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_PIN, OUTPUT);
  pinMode(START_BUTTON, INPUT);
  pinMode(STOP_BUTTON, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long currentMillis = millis();

  // check the start and stop buttons
  // if the start is pressed, set the flash state
  if (digitalRead(START_BUTTON) == HIGH)
    flashState = true;
  // if the stop is pressed, clear the flash state
  if (digitalRead(STOP_BUTTON) == HIGH)
    flashState = false;

  if (currentMillis - prevMillis >= interval)
  {
    prevMillis = currentMillis;

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // only flash LED is the flashState is true
    if (flashState == true)
      digitalWrite(LED_PIN, ledState);
  }

}

This is based upon the Blink Without Delay sketch with the addition of 2 buttons; one for "Start" and one for "Stop".

This basically illustrates what I explained in my previous post.

I hope you have learned from this example. Experiment with it. This example functions as you explained. There are many ways to make this better (button debounce, toggle state on button release, making the buttons mutually exclusive, etc.) Also notice that this example starts and stops the flashing and not necessarily turning the LED on when started or off when stopped. I will leave that up to you. :smiley:

This should give you a basis to understand what is going on.

Hope that helps.