Please i need your help. I am trying to put in to my sketch led blink with delay after button is high. Led flashing for 40ms for one and here is that i need delay for 2000ms.
My problem is that i need this delay (2000) only if led is high. I don't want this delay run all the time in my sketch . My apologies for my english.
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);
delay (40);
// turn LED off:
digitalWrite(ledPin, LOW);
}
delay (2000);
}
Thanks for your reply but with this way this delay will run always with the program. I want this delay out of sketch running only if (buttonState == HIGH).
I think i must to explain a little better what i need. I need to blink the led when button pressed and i have delay (2000) to avoid second pressing by mistake. The problem is that if i put delay inside of sketch running all the time and and make my program run very slow. Thanks for your time.
have delay (2000) to avoid second pressing by mistake
It sounds like you want the LED to turn on then off once when the button BECOMES pressed not when the button IS pressed. If so you need to detect this change of state. Look at the StateChangeDetection example in the IDE to see how it is done.
Unfortunately my english don't help me to explain you exactly what i want. I will try to explain my project. I have a motor that i need to cut power for for 40ms (this is first delay) when pressing the button. If i put the second delay (2000) in to the sketch running with this (sketch) and i don't have immediately respond when pressing the button because i'm waiting till 2000 delay to finish. Sketch have to run always without (2000) delay and this delay take place only if i press the button.
const byte buttonPin = 2;
const byte ledPin = 13;
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
currentButtonState = digitalRead(buttonPin);
if (currentButtonState != previousButtonState && currentButtonState == LOW) //the button has been pressed
{
digitalWrite(ledPin, HIGH);
delay (40);
digitalWrite(ledPin, LOW);
}
previousButtonState = currentButtonState; //save the button state for the next check
}
NOTE :
I have changed the type of some variable to save RAM. Not important here but best to get into good habits. I have also used INPUT_PULLUP in pinMode() for the pushbutton to ensure that the pin is always at a known state. Wire the button to take the pin LOW when pressed to use this and note that the check for the button being pressed looks for LOW.
If you needed to you could also get rid of the delay() entirely and still have the LED flash but I left that alone for simplicity at this stage.
Look at how timing is managed in several things at a time without using the delay() function. You may be able to adapt some of the code to do what you want.
It also has a simple system to avoid unwanted button presses.
Thanks everyone for your help.
Finally it works with this sketch.
Thanks again!!
// 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
boolean i = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
while(i == HIGH)
if (digitalRead(buttonPin) == HIGH);// 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);
delay (40);
digitalWrite(ledPin, LOW);
delay (2000);
}
}