Coding help needed

Hello Guys!

I am new on Arduino and I am working on a project and I need some coding help.

The task is to take a button and a led,
count the time the user presses the button (ex. 1,5s)
and then blink the LED (for 1,5s).

Any ideas on the approach?

Thanks in advance

Save the value of millis() when the button becomes pressed
Save the value of millis() when the button becomes released
See the StateChangeDetection example in the IDE

The difference between the two values is the period in milliseconds for which the button was pressed. Use that value as required to link the LED, preferably using millis() timing rather than delay()

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Thank you for your instant reply! I have to figure out how this work! The truth is that millis function is a bit tricky!

This code will work as expected-

unsigned long previous;
unsigned long timedelay;

void setup()
{

pinMode(13, OUTPUT);
pinMode(12, INPUT);

}

void loop()
{
if(digitalRead(12) == HIGH)
{
previous = millis();
while(digitalRead(12) == HIGH){}
timedelay = millis() - previous;
digitalWrite(13, HIGH);
delay(timedelay);
digitalWrite(13, LOW);
}
}

This code will work if you connect LED to pin13, and one end of switch to 5V and second end of switch to pin12.

ArnavPawar:
This code will work as expected-

unsigned long previous;

unsigned long timedelay;

void setup()
{

pinMode(13, OUTPUT);
pinMode(12, INPUT);

}

void loop()
{
if(digitalRead(12) == HIGH)
{
previous = millis();
while(digitalRead(12) == HIGH){}
timedelay = millis() - previous;
digitalWrite(13, HIGH);
delay(timedelay);
digitalWrite(13, LOW);
}
}




This code will work if you connect LED to pin13, and one end of switch to 5V and second end of switch to pin12.

...but don't forget to stop pin 12 floating.

ArnavPawar:
This code will work as expected-

unsigned long previous;

unsigned long timedelay;

void setup()
{

pinMode(13, OUTPUT);
pinMode(12, INPUT);

}

void loop()
{
if(digitalRead(12) == HIGH)
{
previous = millis();
while(digitalRead(12) == HIGH){}
timedelay = millis() - previous;
digitalWrite(13, HIGH);
delay(timedelay);
digitalWrite(13, LOW);
}
}




This code will work if you connect LED to pin13, and one end of switch to 5V and second end of switch to pin12.

Thank you so much for the help!!!

Do you think that this wiring will work?

The wiring will work, but the code won't.
(Hint: it's a built-in pullup resistor)

Hey we've got two @ArnavPawar members, and they're both reading this topic!

What are the chances of that? :smiley:

For your wiring, it should be changed.

Connect a 10K Ohm resistor. Connect one end of it to 5V and second end of it to pin 2. The code is

unsigned long previous;
unsigned long timedelay;

void setup()
{

pinMode(13, OUTPUT);
pinMode(2, INPUT);

}

void loop()
{
if(digitalRead(2) == LOW)
{
previous = millis();
while(digitalRead(2) == LOW){}
timedelay = millis() - previous;
digitalWrite(13, HIGH);
delay(timedelay);
digitalWrite(13, LOW);
}
}

@dimitri_ch

What if the button is pressed while the led is still on ? Should it still work ? With millis() anything is possible. Well, almost anything.

@ArnavPawar

A 10kΩ resistor, not 10Ω :o You can modify a previous post.
It is possible to edit a previous post and avoid confusion by telling what has changed, for example: 10Ohm [EDIT: 10kOhm]

That is a nice and very short example. +1 karma for you. Could you use indenting spaces next time ? New users tend to make a horrible big mess of the text layout, so our examples should be clean and nice looking.

TheMemberFormerlyKnownAsAWOL:
Hey we've got two @ArnavPawar members, and they're both reading this topic!

What are the chances of that? :smiley:

:smiley: :smiley: :smiley: :smiley:

{Becomes Serious}

Any problems with my code AWOL?

ArnavPawar:
For your wiring, it should be changed.

Connect a 10 Ohm resistor. Connect one end of it to 5V and second end of it to pin 2.

:o

Or just use the built-in one

ArnavPawar:
Any problems with my code AWOL?

Well, let's see:
It's changed since I posted.
Anonymous pins
External hardware, when built-in would work just as well.

But otherwise, pretty much the same standard as your near-namesake.
And just as Tiggerish.

TheMemberFormerlyKnownAsAWOL:
:o

Or just use the built-in one

It has to be an other led! Not the built in!

I consider myself very lucky to have your help guys!

dimitri_ch:
It has to be an other led! Not the built in

I wasn't referring to the LED, just the pullup resistor

(deleted)

ArnavPawar:
For your wiring, it should be changed.

Connect a 10K Ohm resistor. Connect one end of it to 5V and second end of it to pin 2. The code is

unsigned long previous;

unsigned long timedelay;

void setup()
{

pinMode(13, OUTPUT);
pinMode(2, INPUT);

}

void loop()
{
if(digitalRead(2) == LOW)
{
previous = millis();
while(digitalRead(2) == LOW){}
timedelay = millis() - previous;
digitalWrite(13, HIGH);
delay(timedelay);
digitalWrite(13, LOW);
}
}

thanks again! :slight_smile:

Maybe this is the correct wiring!!!!

You don't need the 10k, just INPUT_PULLUP

@smarts_jb what's the problem with the while() loop? It should be used in such code.

And in your second problem, the millis() delay can also be used instead of delay() but delay looks simple if the OP is a beginner.

ArnavPawar:
@smarts_jb what's the problem with the while() loop? It should be used in such code.

Because it is BLOCKING.

ArnavPawarAA Posting in Coding help needed.

:o