press button to trigger, waits 3 minutes until it lights led

hello,

Im looking at writing a code for a prop. I basically want the actor to press a button to start the loop, but it will wait 3 minutes before it starts loop.

using fear function wrong?

thank you

"using fear function wrong?"

What does that mean?

You can use a millis timer or something.. or use a real RTC for more accurate time.

And also, what does "start the loop" mean?

Then what happens?

I'd put a button to ground, input pullup, then still in setup(), while (digitalRead(theButton)) {} to hold it until the button goes low, then a delay().

setup() starts, does its usual stuff, sits in the while() until the button is pressed, does the delay(), heads off to loop().

And in loop() do whatever the prop's supposed to do.

Hey, sorry my phone typo it fear instead of ‘Delay’

I basically want this to happen:

Actor pressed button, sequence triggers but there is a 2 minute delay before 1 relay turns on for 5 seconds and then goes off. Then a 2nd relay turns on for 5 seconds and turns off, then the 1sr relay comes back on and then a few seconds later the 2nd relay comes back on.

I can do it with the delay function but it freaks out as if the delay is to long and won’t upload to board.

Apparently millis is the way to go. I understand what millis does in a basic sense, but not sure how to write it.

With delay function I would write as an example:

Delay (5000);
digitalWrite (relayA, HIGH);
Delay (3000);
digitalWrite (relayA,LOW);
digitalWrite (relayB,HIGH);
delay (5000);
And so on...

Instead if where I put delay, would I just put:

millis (5000); ?

Apologies on sloppy typing I’m on my phone :confused:

sporter19:
Hey, sorry my phone typo it fear instead of ‘Delay’

Understandable; they're very similar :wink:

sporter19:
I can do it with the delay function but it freaks out as if the delay is to long and won’t upload to board.

I'd be inclined to investigate why it "freaks out" (whatever that means) and sort that out, since this project seems like a case where using delay() is ok.

sporter19:
Instead if where I put delay, would I just put:

millis (5000); ?

If only....

Hello,

It works fine with the delay part between the relays only... but I think the problem is the first part.

Button pressed
Delay (145000);
digitalWrite (relayA,High);
Delay (5000); etc

It’s the initial delay I think is to long... if I take this out it works but I need the gap of 2mins 45seconds until the relays trigger (it fits with the actors scene and gives them timing cues)

Image it’s like a timer...

I press the button and once 2mins 45 seconds passes after the button was pressed, a light switches on to tell the actor “your out of time”

sporter19:
Delay (145000);

Says here:

the number of milliseconds to pause. Allowed data types: unsigned long.

Then it says here:

Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won’t store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).

sporter19:
it freaks out ... and won’t upload to board.

Of course if you're actually typing:

Delay (145000); // capital-D

... not:

delay (145000); //small-d

... it won't compile so won't upload.

No I’m typing the code properly, it’s just on my shit phone and it’s hard to write the code on it otherwise I would type it in the correct way.

Unsigned long buttonPress = 0;
Unsigned long eventStart = 145000;
Int button = 11;
Int relayA = 2;
Int relayB =3;

Void setup

Pinmode (relayA,OUTPUT);
Pinmode (relayB,OUTPUT);
Pinmode (button,INPUT);

Void loop

If button = High
Millis () = buttonPress //so millis resets to 0 then count to eventStart

I now think I should be saying:

If button = HIGH & buttonPress > eventStart
digitalWrite (relayA,HIGH)

Sorry I know it’s missing all the ‘;{}’ it’s a pain to do on my phone but hopeful that gives you the principle... I just don’t think how I’m constructing the if statement is correct at all or event close

Edit: that if statement is no way near right, the > I don’t think should be here, is it a ++ or something?

It’s like knowing how to say hello my name is Bob in french but not knowing how to write it :frowning:

I can do it with the delay function but it freaks out as if the delay is to long and won't upload to board.

Please post the code which demonstrates this behaviour.

The code written with long sequences of delay() and digitalWrite() should work just fine for your purposes.

If you want an example of millis()..

check out the basic blink without delay example in the IDE

I can’t upload it as I’m on my phone right now...

It’s basically saying there’s like to much usage or not enough memory or something because if that super long delay (14500);

I’m think I should now try this, still not sure if the language or construction of the language is correct but here goes:

No I’m typing the code properly, it’s just on my shit phone and it’s hard to write the code on it otherwise I would type it in the correct way.

Unsigned long addOn = 1;
Unsigned long buttonPress = 0;
Const Unsigned long eventStart = 145000;
Int button = 11;
Int relayA = 2;
Int relayB =3;

Void setup

Pinmode (relayA,OUTPUT);
Pinmode (relayB,OUTPUT);
Pinmode (button,INPUT);

Void loop
Unsigned long currentTime = millis ();

If (button = High, buttonPress = currentTime){

If (currentTime - buttonPress >= eventStart){
digitalWrite (relayA,HIGH)
Delay (3000);
digtalWrite (relayA,LOW)

Else (currentTime + addOn){

Does this work? Am I getting somewhere now?

So I’m basically trying to say:

If the switch is high, reset the millis to 0
Then check if the current time - 0 = 145000

If it is, then trigger the relay

If its not 145000, add 1 onto current time so it counts up and keeps checking to see if it’s equal to 14500 and when it is, trigger relay.

I know I’m missing some “{}” etc but I’m not worried about them for now, it’s the language and structure of the statements I’m struggling with and my phones annoying trying to put all the brackets and stuff in.

It’s now 7am, I haven’t slept trying my best to understand this... if anyone can please help it would be much appreciated as I’m really trying here. I’ve got like 20 YouTube tutorials up and Arduino forum posts

It's basically saying there's like to much usage or not enough memory or something because if that super long delay (14500);

That's not a "super long delay" by any standard.

You'll have to wait until you post the actual code with the actual error messages, from the actual system, for actual help.

delay() stops regular code execution for the delay interval.

You can replace delay() with a technique called Blink Without Delay, BWD.

When using BWD, your sketch can run other code during the delay time.

Read Robin2’s discussion, Demonstration code for several things at the same time:

Don’t proceed any further until you understand how to use millis() in non blocking timing.

larryd:
Don’t proceed any further until you understand how to use millis() in non blocking timing.

That's an unnecessary restriction in this case, methinks; no reason OP shouldn't get his or her code working fine with delay().

cattledog:
The code written with long sequences of delay() and digitalWrite() should work just fine for your purposes.

My advice would be not to proceed any further until code and errors can be posted correctly from the system where the IDE is, not paraphrasing the errors "It's basically saying there's like to [sic] much usage or not enough memory or something", and posting code like Delay() with capital-Ds.

sporter19:
I can’t upload it as I’m on my phone right now...

In which case, go straight to bed and just forget about it until you can use a proper computer. :astonished:

Paul__B:
In which case, go straight to bed and just forget about it until you can use a proper computer. :astonished:

^this...
guessing is doing noone any god.
post the real code
post the real errors

“ That's an unnecessary restriction in this case, methinks; no reason OP shouldn't get his or her code working fine with delay().”

The reason is to teach new people how to be effective programmers.

Yes, go ahead and encourage the OP to use delay() but then also tell she/he ‘not’ to come back and complain when the sketch does respond as they want.

‘no reason’ if she/he is not serious in learning how to program.
‘no reason’ if it is okay that their sketch is not responsive.

If the OP is serious about this hobby, they need to to learn some basic techniques in programming.

We go through huge efforts to try to teach these techniques to new users and avoid poor programming habits.

Let’s encourage them to learn how to write code properly from the being!

there are tons of examples of what you are looking to do, and lots and lots of ways to do it.

if the ONLY thing you are EVER going to do in that loop is to do sequential delays, then using delay() is acceptable.
frowned upon as larryd elaborated and I believe the vast majority agree with his analysis.

but, it is like counting rolls of coins, by taking them out of the rolls and counting each of 5,000 coins
it 'can' be done, but is usually not the best thing to do.

you can create a simple loop, a variation of BWD.

if timing == low
if switch goes high // read switch before this.
then = millis() // start the counting
timing = high // stops from redoing this if() and sets the timer in action

if timing == high
duration = millis()-then // this gives you a way to see how long since the start

if duration >= 1000 // counts to 1 second
counter++ // increments counter by 1
then = millis() // retstarts the time period

if counter > 120 output1= high
if counter > 150 output1= low, output2 = high
if counter > 160 output1= high, output2 = low
if counter > 180 output1= low, output2 = high
if counter > 240 output1= high, output2 = low

// end of - if timing == high

if counter > 6000 // after the whole thing timed out
timer=low // allows the sketch to look for a re-start
output1= low, output2 = low // set either hi or low, whatever your relay off is
counter = 0 ; // resets counter

// all the logic is complete, then write to the outputs.

digitalWrite(relay1, output1)
digitalWrite(relay2, output2)

=============

this is only one variation of BlinkWithoutDelay
this one shows how to use a counter to allow multiple things to be sequenced.
if you have one BWD start after another ended and need multiples, you can get lost.

in my opinion, this allows you to concentrate on the timing and not on multiple BWD loops.

it is NOT code, you have to add brackets for the if() statements, declare your vairables and fix the rest.