On off blinking LED

Hello,

I am just starting off on a new project and need to simply make some LED strip lights stay a solid colour and when a button is pressed they blink for 5 to 10 seconds and return back to the solid colour. pretty straight forward just creating a indicator pretty much. Ive only been using the button code in the examples of the arduino software this one but havnt managed to edit it to get what I want, any help would be greatly appreciated.

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

thanks

  • Liam

Have a look at how timing is managed using millis() in several things at a time. You may also find some useful stuff in planning and implementing a program.

Some of the functions in those programs can probably be used to do what you want.

...R

I think the simplest way would be to put a loop inside your if-condition. If the button is pushed, you go into a loop and blink the LED.

In this case, I'd suggest a [u]for-loop[/u]. With a for-loop, you can count the number of loops and exit the loop when the count is reached. So if you blink twice per second, 20 loops is 10 seconds and you exit the loop and stop blinking.

From what you've described so far, you can use delay() like the regular blink LED example. But, since the program "stops" during the delay time and your program can't read the button or do anything else during that time, most real-world programs avoid delay(). So, take a look at Robin's links if you want to avoid delay().

Ive had a look at it and I edited the code to add in to make the LED blink when the button is pressed but It doesn’t seem to be working, here is my code.

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     

 digitalWrite(ledPin, HIGH); 
 
}
void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000); 

  } 
  else {
    
  }
}

How is the button wired?
It is usually better to have

pinMode(buttonPin, INPUT_PULLUP);

which enables the internal pull-up resistor. That means the pin is normally HIGH and you use the button to connect it to GND (and bring it LOW) when pressed.
The you would use

if (buttonState == LOW) {

The way your code is presently written it assumes you have an external pull-down resistor. Without that the pin state can be uncertain.

…R

Thanks for the reply, I have it wired like this: http://arduino.cc/en/tutorial/button

When I made the changes you mentioned the LED just blinks and nothing happens when pressed.

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);     

 digitalWrite(ledPin, HIGH); 
 
}
void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {     
  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000); 

  } 
  else {
    
  }
}

If you are using the code in Reply #5 and that circuit you should + remove all the red wires + remove the resistor and replace it with a piece of wire.

and if you add

Serial.println(buttonState);

immediately after the buttonState = line you will be able to see what is happening

By the way you will also need

Serial.begin(9600);

in setup()

...R

I added the parts to the code and removed the red wires and the resistor, the code looks like this now but still dosnt work as I want it to.

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);     

 digitalWrite(ledPin, HIGH); 
 
}
void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {     
  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000); 

  } 
  else {
    
  }
}

That code (Reply #7) looks OK. Now you should have some diagnostic information to share with us. Describe exactly what happens when you try it.

Also make a pencil drawing of your wiring connections and post a photo of the drawing.

To help effectively we need to have as much information about the project as you have.

...R

have you a make or break button??

if you have a make button,than if you push,the led stays of,and monitor gives a 1,else monitor gives a 0,and led flash.

program work here fine

When I turn plug in the arduino the code starts up and the LED starts blinking on it, heres a photo of the setup just like the link but without the red wires, is that correct?

I think its a make button its just a standard click one.

1) If you touch the green wire going into the board to the white wire going into the board then that will approximate the switch. 2) Maybe you need to re-situate, re-orient, the switch in the board (turned 90 degrees CW/CCW from its present.)

And we have the resistor-less LED here And it's in "Pin13", of all places When will they issue low-current bulbs in these "kits"?

I asked you to make a pencil drawing of the circuit because it would be easier to understand than your photo. I presume @RunwayPancake is correct in assuming you have no resistor to reduce the current in the LED. That omission would be much more obvious in a drawing.

You should put a resistor - maybe 470ohms or 1000ohms - between one end of the LED and the Arduino. It doesn't matter which end, although it is better practice to put it on the positive connection as that better protects the Arduino from short circuits. Without the resistor too much current will flow and will probably damage the Arduino or the LED.

If you don't already have one buy a cheap digital multimeter. You can then easily check how the switch works - which legs are connected when you press the button. It sounds like the switch is connected so that it is always ON instead of OFF.

...R

you have not a resistor of 10k to GND,on the button???? http://arduino.cc/en/tutorial/button

gonnie08: you have not a resistor of 10k to GND,on the button???? http://arduino.cc/en/tutorial/button

With the following line in setup () pinMode(buttonPin, INPUT_PULLUP); nothing else is needed, the input is "pulled up" (weakly). So, tapping a jumper wire stuck in "pin 2" to Gnd will effect the switch.

And, for now, get rid of the extra LED and just go with the pin 13 LED already.

so I added the 1000k resisitor and now the LED isn't blinding me everytime

I'm amazed you can see it all ;)

(1000 Ohm, not 1000kOhm)

I removed my previous post as I got the button working!!

Okay so I tested the orientation of the switch and its wired like the diagram now so when the button is held it makes the LED flash and when the buttons released then the LED turns off which is great I added the resistor to the LED and now it doesn't blind me each time haha!! I apologise for the awful photo I was in a bit of a rush earlier. Now all I need is for the LED to stay on constantly and when the button is clicked then blink for 5-10 seconds then go back to staying on!!

|500x308