help with understanding millis

Hello.
Let me first explain what I am attempting to do with this sketch.
The sensor is there to detect metal when it does, it triggers the servo to open a door that lets the metal out and then closes it.
What I would like to add now is a timer of sorts so that if the sensor does not detect any metal after say 5 secs, the sketch turns the relay off. until the reset button is hit.
I am looking at using millis for this as from what I understand it will count while the rest of the sketch is running, however the examples I have looked at thus far are not 'clicking' in my head for me to understand how to use it correctly. or where to put it.

my code so far

#include <Servo.h>

Servo door;  //calling servo the door
int sensor = 7;  //sensor is on pin7
int relay = 6;  //relay on pin6

int detect = 0; //setting sensor to open

void setup()
{
  pinMode(sensor, INPUT);
  pinMode(relay, OUTPUT);
  door.attach(5); // servo on pin5
  
}

void loop()
{
  detect = digitalRead(sensor);
  digitalWrite(relay, HIGH);
  if (detect == HIGH) {
     door.write(90);
     delay(2000);
     door.write(45);
    }
}

If you need anymore information to be able to help, I will do my best to provide.
I'm more looking for an example using millis for my use, not someone to write my code for me.

Thanks

you should take a look at the BlinkWithoutDelay example.
You can find it on your IDE or here:

the idea would be something like this:
Define an interval (you said you want 5s).
You would need to save the millis() into a variable at the moment you want the Arduino so start counting the 5s (ex: when the door is opened).
Then on each loop you can check if the difference between the previous millis() (which you save into a variable) and the current millis() is equal or bigger than you interval. In other words: you can on each loop check if 5 seconds have passed.

It would be something like this:
(i'm skipping parts of code, just writing the relevant for the "interval checking, but nothing for the door,...)

// create variables
long openTime;
int interval = 5000;

// ...

// open the door
   // save time at which door was opened
   openTime = millis();

// if the door is open
   // check for interval
   if( millis() - openTime > interval) {
      // close the door
   }

Good luck!
:wink:

I'm a newbie myself, I understand it is frustrating coming to the forum for assistance and being told that you need to read a tutorial and figure it out. If you understood the tutorial you wouldn't have asked the question (a reasonable assumption.) Instead of telling you what you already know that you don't understand, I thought I'd learn with you and maybe help you get to the final solution a little quicker.

Eventually you'll probably want to get rid of the delay between the servo positions and use millis, but the code below should get you started. I've added the reset button to pin 8, but it can be any digital pin, it should probably be debounced which I prefer to do with hardware instead of software because it makes the sketch so much simpler and my wheelhouse is hardware not software. If you're more intimidated by hardware than software or you want to learn more about programming just for the sake of learning there's a tutorial on debouncing, I believe there's a debounce library that is supposed to make software debouncing more managable as well.

#include <Servo.h>

Servo door;  //calling servo the door
int sensor = 7;  //sensor is on pin7
int relay = 6;  //relay on pin6
int resetButton = 8;  //reset button on pin8

int detect = 0; //setting sensor to open
int previousDetect;
boolean blockRelay = false;

void setup()
{
  pinMode(sensor, INPUT);
  pinMode(relay, OUTPUT);
  pinMode(resetButton, INPUT);
  door.attach(5); // servo on pin5
  previousDetect = HIGH;  //set previous detect to high for first cycle

}

void loop()
{
  detect = digitalRead(sensor);

  //turn on relay only if relay has not been blocked by null detection time out
  if (blockRelay = false){
    digitalWrite(relay, HIGH);
  }
  if (detect == HIGH) {
    door.write(90);
    delay(2000);
    door.write(45);
  }
  unsigned long nothingToDetectTime;

  //if the sensor doesn't detect anything and previously had detected something
  //set nothing to detect time to millis
  if (detect == LOW && previousDetect == HIGH){
    nothingToDetectTime = millis();
  }

  //if 5000 milliseconds have passed since nothing to detect time was set
  //and detect is not currently high turn off relay and block it from coming back on
  //until reset button is pressed
  if (((nothingToDetectTime + 5000) < millis()) && detect == LOW){
    digitalWrite(relay, LOW);
    blockRelay = true;  //block relay so relay won't be turned on in next cycle
  }

  //if reset button is pressed allow relay to turn on in next cycle
  int resetButtonState = digitalRead(resetButton);
  if (resetButtonState == HIGH){
    blockRelay = false;
  }

  previousDetect = detect;
}

kornphlake:
I'm a newbie myself, I understand it is frustrating coming to the forum for assistance and being told that you need to read a tutorial and figure it out. If you understood the tutorial you wouldn't have asked the question (a reasonable assumption.)
Instead of telling you what you already know that you don't understand, I thought I'd learn with you and maybe help you get to the final solution a little quicker.

So you think it is better to give the answer to someone than to tell them where they can find the info and learn on their own?!
If you stick around for a while in the forum, you will notice that most people will ask before they try to learn on their own. And this thread is a great example. There are probably HUNDREDS of threads in this forum alone explaining how millis() works, telling people where they can find more information about it and even some code examples and solutions to problems.

I think reading (watching,...), understanding, and then writing your own code has much better results (maybe not so immediate, but if you want no work at all just buy a ready made product! :wink: )...
:wink:

if (blockRelay = false){

Oh dear.

If there's a web page that will tell something needed then I hope you don't expect it needs to be replicated here instead of someone here giving a link to there?

If you understood the tutorial you wouldn't have asked the question (a reasonable assumption.)

It would be reasonable to assume that specific questions should be asked about what is not understood.

If you can't read words the you should be learning the alphabet, not sentence structure and grammer let alone "I'm totally new to code, help me with my GPS Ethernet car autopilot novel".

woodharry:
Hello.
Let me first explain what I am attempting to do with this sketch.
The sensor is there to detect metal when it does, it triggers the servo to open a door that lets the metal out and then closes it.
What I would like to add now is a timer of sorts so that if the sensor does not detect any metal after say 5 secs, the sketch turns the relay off. until the reset button is hit.
I am looking at using millis for this as from what I understand it will count while the rest of the sketch is running, however the examples I have looked at thus far are not 'clicking' in my head for me to understand how to use it correctly. or where to put it.

my code so far

#include <Servo.h>

Servo door;  //calling servo the door
int sensor = 7;  //sensor is on pin7
int relay = 6;  //relay on pin6

int detect = 0; //setting sensor to open

void setup()
{
  pinMode(sensor, INPUT);
  pinMode(relay, OUTPUT);
  door.attach(5); // servo on pin5
 
}

void loop()
{
  detect = digitalRead(sensor);
  digitalWrite(relay, HIGH);
  if (detect == HIGH) {
     door.write(90);
     delay(2000);
     door.write(45);
    }
}




If you need anymore information to be able to help, I will do my best to provide.
I'm more looking for an example using millis for my use, not someone to write my code for me.

Thanks

Here is an approach to doing more than one thing at the same time, each on a timed basis. It is very clear.

That is the start, not all of it but once you learn the above it will be far easier to take the next step.

AWOL:
Oh dear.

  • 2 ...
  if (((nothingToDetectTime + 5000) < millis()) && detect == LOW){

In the first post of this Thread I wrote an extended demo of the concept in the Blink Without Delay example sketch. The demo also illustrates how to organize code into separate short functions rather than jumble everything together in loop().

...R

boguz:
So you think it is better to give the answer to someone than to tell them where they can find the info and learn on their own?!

I think it's better to give someone help with the specific question they've asked instead of a vague and condescending response that generates frustration instead of understanding.

If you stick around for a while in the forum

not very likley honestly.

There are probably HUNDREDS of threads in this forum alone explaining how millis() works, telling people where they can find more information about it and even some code examples and solutions to problems.

The same question being asked hundreds of times should be a testiment to the fact that the concept of millis() is difficult for many people to understand and the available materials are not adequate to effectively teach the concept. I'm not a professional educator, and based on the implied criticisim I'm not much of a programmer either, so I'm probably way off base.

AWOL:

if (blockRelay = false){

Oh dear.

Is there a problem with the arbitrary name I assigned the booelan integer? Did I mis-spell false? Should I have included a semi-colon after the parenthesis? Is if supposed to be capitalized? Hang on, now I see it, I should have used == instead of =. I didn't walk on water on my way home from fishing this afternoon either.

See, this is exactly the type of teaching style that is incredibly frustrating to a beginner, instead of identifying my mistake you just write F at the top of the paper and hand it back to me. How am I to know what I'm supposed to improve if my mistakes are explained as "oh dear." Yeah, I'll figure it out on my own eventually, but I'd arrive at that eventuality without the help of the community... so why do I need the community's help? See my comment above regarding my plans to "stick around for a while."

kornphlake:
See, this is exactly the type of teaching style that is incredibly frustrating to a beginner...

It is the teaching style that is frustrating to some beginners. The problem for those providing help through a forum is that there is no way to know that a beginner is not being helped unless the beginner specifically says so.

...instead of identifying my mistake you just write F at the top of the paper and hand it back to me.

AWOL clearly indicated the mistake was isolated to seven tokens. That is hardly analogous to "write F at the top of the paper and hand it back".

How am I to know what I'm supposed to improve if my mistakes are explained as "oh dear."

Get down off your high horse and ask for an explanation.

Yeah, I'll figure it out on my own eventually...

Or, you could just ask for an explanation.

It's like telling someone it is unwise to jaywalk across a very busy road, pointing out the traffic and being asked why you say they don't walk right. Yeah, go ahead. Try running.

It's funny how many 'get' the use of unsigned math with millis() in less than days after they drop their misunderstandings while some others never completely 'get it' because of what they hold on to (there must be a rollover problem to code around!) or it's just not something that can be landed at the airport on a spoon.

Perhaps BWD is a new programmer's take as long as you want, open book IQ test.
Given 15 lines of code and free access to references, can you figure out HOW it works?
If you don't know the commands, can you look them up and figure those out on the way?
Can you actually determine what to look up and sit still long enough to puzzle it out?

If not then you haven't bothered to learn the fundamentals of code or learning, assuming that if you can get here then you should also be able to do those things. It's hard to learn spelling and grammar when you skipped ABC's.

Maybe there should be a Romper Room section of the forum though I can only imagine a few of the AVRfreaks might actually die laughing given their attitudes now.

kornphlake:

AWOL:

if (blockRelay = false){

Oh dear.

Is there a problem with the arbitrary name I assigned the booelan integer? Did I mis-spell false? Should I have included a semi-colon after the parenthesis? Is if supposed to be capitalized? Hang on, now I see it, I should have used == instead of =. I didn't walk on water on my way home from fishing this afternoon either.

See, this is exactly the type of teaching style that is incredibly frustrating to a beginner

I'm not a professional educator (well, not in software anyway) so I make no apologies - it says "forum" at the top of the page, not "classroom".
Attention to detail is a key skill to be acquired if you ever want to make any headway in software, and after I pointed out a problem, you just demonstrated a logical, stepwise approach to finding that problem.
Well done, A-. (It would've been an A, but you misspelled "boolean").

There's a mistake in the Blink Without Delay example - can you find it?
Edit: sp. "at least one mistake" yadda, yadda.