I want the LED to be turning off and on at various time interval when the button is not pressed and when it is pressed I want the LED to be still.
I tried various methods to achieve this but I couldn't.
i want the led to flicker at following rates : int flicker[] = {180, 30, 89, 23, 255, 200, 90, 150, 60, 230, 180, 45, 90};
I have a button and a LED. Anyone guide me or give the code for this.
Thank you in advance.
do all your flicker and random light codes...
just put in
while (digitalRead(buttonPin) == HIGH){
digitalWrite(LedPin, LOW);
}
does that help?
Don't hold your breath while waiting for someone to write the code for you... The Arduino is a do-it-yourself product and this is pretty-much a do-it-yourself forum.
And, you didn't say what pins your button & LED are connected to, we don't know if the button is active-high or active-low, and you didn't say if those are seconds or milliseconds or if "rates" are one full on/off cycle a half-cycle, etc.
But, I'd start with a [u]while()[/u] loop that runs only while the button is pushed.
And, I'd start with a single-fixed blink rate (or maybe by just turn the LED on when the button is pushed) and then "develop" your code from there.
If you are going to put those times in an array you need to know how to read from an array and how to increment. (I assume you want those blink/flicker times in sequence?) And, you'll have to decide what to do after you've gone through the list of delays/timers.
#define BUTTON_PIN 2
#define DELAY 20
const int ledpin = 13;
const int ledpin1 = 12;
int flicker[] = {180, 30, 89, 23, 255, 200, 90, 150, 60, 230, 180, 45, 90};
void setup()
{
pinMode(ledpin,OUTPUT);
pinMode(ledpin1,OUTPUT);
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH);
Serial.begin(9600);
}
boolean handle_button()
{
int button_pressed = !digitalRead(BUTTON_PIN);
return button_pressed;
}
void loop()
{
boolean button_pressed = handle_button();
if(button_pressed){
digitalWrite(ledpin,HIGH);
digitalWrite(ledpin1,HIGH);
delay(flicker[random(14)]);
digitalWrite(ledpin,LOW);
digitalWrite(ledpin1,LOW);
}
delay(DELAY);
}
This code works for me. Thank you so much.