Random flash light with button

Hello all,

I can`t find what I am looking for so that's the reason for my post. I am thinking about doing a lash light LED that is triggered by a button (push button not holding close all the time). So when the button is pressed/closed the LED starts flashing randomly (interval has to be changeable) and than switch off after some time (adjustable). Then when the button is closed again the whole loop starts again.

Did anyone know a project where this is done? Please link??

Thanks

I don't know of a project where this is done, but you should be able to create one yourself pretty easily. Read and study the "button" and "blink without delay" sketches. Hook up your button first, using the built-in LED. Once the button's behavior is to your liking, swap it out for a high brightness LED, and use a transistor to power it from separate batteries.

Thanks for your reply.

I never know how two sketches have to be linked together so that they are working? I am a noob with programming:(

You don't actually link two programs together. Write your code so that variable names are unique, and one program does not change variables used by the other. Keep any shared variables to a minimum. Write your code in small easy to comprehend modules, then cut and paste parts of one program into the other. Test often, and be sure you understand how each part is working before combining.

So for example here's one program:

const byte buttonPin = 2;
boolean buttonState = true;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(buttonPin) {
    buttonState = false;
  } else {
    buttonState = true;
  }
  Serial.println(buttonState);
}

And another program:

const byte ledPin = 13;

// these 2 lines are copied from the first sketch
const byte buttonPin = 2;
boolean buttonState = true;

void setup() {
  pinMode(ledPin, OUTPUT);

  // copied from the first sketch
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {

// this if statement is copied from first sketch
  if (digitalRead(buttonPin) {
    buttonState = false;
  } else {
    buttonState = true;
  }

  digitalWrite(ledPin, buttonState);
}

The second program builds on parts of the first program. This way, you test your button code, and are sure it works before throwing the LED code at it. Then you can test the LED code without worrying if the button code is working or not.

Hello I am now one step ahead with this code:

int ledPin = 13;                  // LED connected to digital pin 13
long randOn = 0;                  // Initialize a variable for the ON time
long randOff = 0;                 // Initialize a variable for the OFF time


void setup()                      // run once, when the sketch starts
{
 randomSeed (analogRead (0));    // randomize
 pinMode(ledPin, OUTPUT);        // sets the digital pin as output
}

void loop()                       // run over and over again
{
 randOn = random (50, 30);    // generate ON time between 0.1 and 1.2 seconds
 randOff = random (50, 300);    // generate OFF time between 0.2 and 0.9 seconds
   digitalWrite(ledPin, HIGH);   // sets the LED on
   delay(randOn);                // waits for a random time while ON
   digitalWrite(ledPin, LOW);    // sets the LED off
   delay(randOff);               // waits for a random time while OFF
}

How could I add the button now? And how can I add that the blinking will end after some time like 30 sec?

No further comments?

ArneDunio:
No further comments?

Have you looked at the button example? Forget trying to tie the two sketches together, just learn how it works and then use the same idea to read a button in your sketch. Don't hack and paste. That's the worst way to learn. Study it until you understand and then apply what you learned.

It would really help to have a similar project to learn from it. That is the way you normally learn fast…
Any ideas?

ArneDunio:
It would really help to have a similar project to learn from it. That is the way you normally learn fast...
Any ideas?

Delta_G:
Have you looked at the button example?

Google the words "arduino" and "flashlight" together.

And uh, yeah. Look at the button example. It's exactly what you want.

Work your way through the first five demos in: IDE/file/examples/digital. When you understand the concepts therein, you will have in your kit the essentials you need for this effort and many more.

When you've got those under your belt, use the change-of-state of the button to start a timer. As long as the timer hasn't timed out, turn on an LED to indicate that fact. Your random lights are then only a step away.

Hello all, I would really appreciate if someone could help. I now have this code:

//set pin numbers
const int ledPin = 2;         //const won't change
const int buttonPin = 4;
 
//variables will change
int buttonState = 0;          //variables for reading the pushbutton status
 
void setup() {
 
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);    //initialize the LED pin as an output
  pinMode(buttonPin, INPUT);  //initialize the pushbutton pin as an output
}
 
void loop() {
 
  buttonState = digitalRead(buttonPin); //read the state of the pushbutton value
 
  if (buttonState == HIGH) {            //check if the pushbutton is pressed
    //if it is, the buttonState is HIGH
    digitalWrite(ledPin, HIGH);         //turn LED on    
    delay(500); 
  }
  else {
 
    digitalWrite(ledPin, LOW);          // turn LED off
    delay(500);
  }
 
}

But it is not blinking just a delay when pushing the button. I want the LED to blink for a while (15sec) randomly and than switch off. If the button is pushed again the same loop.

Please helppppp :frowning:

How is your button connected? How is your LED connected?

// be sure to wire the button to the pin on one side and ground on the other. needs no resistor. 

// const won't change          // set pin numbers and [b]do not waste your very limited RAM[/b]
const byte ledPin = 2;         // never use an int (16 bits, -32768 to +32767),
const byte buttonPin = 4;    // where a byte (8 bits 0 to 255) will do. byte pin numbers!
 
//variables may be changed
byte buttonState = 0;            // the name of the place in RAM where for now, 0 is stored
 
void setup() 
{                  // I like braces where they're easy to line up with a glance is why I move them
  pinMode(ledPin, OUTPUT);    // set the LED pin as an output, the default state is LOW, ground
  pinMode(buttonPin, INPUT_PULLUP);  // set the button pin as an input that is fed weak 5V. If grounded it reads LOW, if not it reads HIGH.
}
 
void loop() 
{ 
  buttonState = digitalRead(buttonPin); //read the state of the pushbutton value
  digitalWrite(ledPin, !buttonState);         // read !buttonState as NOT buttonState, ! is logical NOT, it switches HIGH and LOW.
}

But it is not blinking just a delay when pushing the button. I want the LED to blink for a while (15sec) randomly and than switch off. If the button is pushed again the same loop.

Please helppppp :frowning:

Well yeah but now you know what it takes for just the button.

To watch the button and flash the led at the same time, you don't want to use delay() anywhere because it blocks everything from being done until it is done. Really, if you delay the led you also delay watch the button.

It gets worse before better. No-delays code is so fast it catches the sparks as the button contacts are micro-close with every press and release. You can import debounce code for that. What you want to do is inside of void loop() you have split the job into functions that you write outside of void loop().

// define variables here

void buttonCode()
{
// get some help with this, could be done many ways but --- do not use delay() or blocking code
}

void blinkCode()
{
// get some help with this, could be done many ways but --- do not use delay() or blocking code
}

void loop()
{
buttonCode(); // watches and debounces the button, updates the value of buttonState and saves micros() to a random seed variable

blinkCode(); // makes a few random blinks when buttonState changes from up to down, uses random seed
}

The variables for button state and random seed are information channels for control and data between 2 separate running tasks.

I don`t understand I am a noob with some knowledge.

I don`t understand why nobody is helping in a decent way. No offence here!! I love all of you guys.

But if I am new in something the best way to learn ( in my eyes) is to copy and learn. The hurdle to get this working are so high in my eyes because the topic is complex. So many projects I lost my interest because debugging was so complicated.

I was always wish to find a project ready made I could follow.

I think what I wanted to do is so simple for you experts. But everyone is only giving tipps.

Would it not the best to have a ready made code from someone and then following how they did it?

Its just like sports. For example pole vault you don`t give a stick to someone who never saw how it is done and then say jump over this 4 m high obstacle with some tipps. He will never succeed. You take the stick and show how to do it....

I now try to find a ready made project.

Thanks all of you for your help I am one step further.

What I threw out is more for other members with more time than I had to spend.

It's also an overview.

What the OP really needs is to do more of the simple work to learn basics, especially the BlinkWithoutDelay sketch in section 2 of the IDE built-in examples. That is a lesson for beginners that is key to what the OP wants to do.

The sketch is already in the IDE. The web page covering it is an Arduino site RESOURCE. If you can learn from examples then learn from what you already have and learn it better making it do what you can.

Sorry, I can't grow neurons and axons in YOUR brain for you. You have to do these things to really learn, the sign that you have is when you ask specific questions rather than "do it for me, I'll figure it out". Sorry no, you haven't figured out what you already got.

But if I am new in something the best way to learn ( in my eyes) is to copy and learn.

Learn but not copy. Copy doesn't help. You need to look at examples and study them and learn how they work. Nobody has denied you that. You are just either too stubborn, too lazy, or too stupid to simply click on the link I gave you and go look at it and try to learn it.

Would it not the best to have a ready made code from someone and then following how they did it?

We've given you TONS of ready made code examples to learn from. It's not our fault that you are too stubborn or lazy to go look at them. It's ridiculous to think that someone should rewrite the whole thing here just because you can't seem to follow a link.

Its just like sports. For example pole vault you don`t give a stick to someone who never saw how it is done and then say jump over this 4 m high obstacle with some tipps. He will never succeed. You take the stick and show how to do it....

Have you ever pole vaulted? That's exactly how you learn. Someone shows you a couple times and gives you a few pointers on how to hold the pole and how to get yourself up and then you try. You inevitably fail and then that same coach helps you by telling you what you did wrong and how to correct it. So now we're up to that point. You've been given examples. You've been shown how it works. You're just standing there too scared to give it a try. Nobody can help you until you get over that.

I think what I wanted to do is so simple for you experts.

I think what you want is easy for you too if you will just get over being scared of it and give it a try. Go read the code examples you've been pointed to. Once you do I'll give you a couple more to look at. Once you learn how they work then what you want to do is actually trivially easy.

You're just not going to get someone to put it all together and wrap it up in a bow for you without you putting in at least some effort. That's just not going to happen. If you want someone to just write it out for you then you go to Gigs and pay someone for it. This isn't a free code writing service.

Now I'm going to ask one more time and if you continue to refuse to answer me then I really will just stop helping and move on to someone else.

Have you looked at the Button Example yet?

If you would have gone there and answered me we could have been done with this half a page ago.

Hello, I am not to lazy to follow but belive it or not. It is not that simple for some one who started.

I went to the button sample and yes It worked. But far from what I want to do.

That is what I have without any button:(

/*
* Blink_Randomly

int ledPin = 13;                  // LED connected to digital pin 13
long randOn = 0;                  // Initialize a variable for the ON time
long randOff = 0;                 // Initialize a variable for the OFF time


void setup()                      // run once, when the sketch starts
{
 randomSeed (analogRead (0));    // randomize
 pinMode(ledPin, OUTPUT);        // sets the digital pin as output
}

void loop()                       // run over and over again
{
 randOn = random (50, 200);    // generate ON time between 0.1 and 1.2 seconds
 randOff = random (30, 150);    // generate OFF time between 0.2 and 0.9 seconds
   digitalWrite(ledPin, HIGH);   // sets the LED on
   delay(randOn);                // waits for a random time while ON
   digitalWrite(ledPin, LOW);    // sets the LED off
   delay(randOff);               // waits for a random time while OFF
}