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);
}
}
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.
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 {
}
}
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 {
}
}
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.
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.
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.
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!!