help with understanding millis


hello.
let me first explain attempting sketch.
the sensor there detect metal when does, triggers servo open door lets metal out , closes it.
what add timer of sorts if sensor not detect metal after 5 secs, sketch turns relay off. until reset button hit.
i looking @ using millis understand count while rest of sketch running, examples have looked @ far not 'clicking' in head me understand how use correctly. or put it.

my code far
code: [select]
#include <servo.h>

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

int detect = 0; //setting sensor 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 need anymore information able help, best provide.
i'm more looking example using millis use, not write code me.

thanks

you should take @ blinkwithoutdelay example.
you can find on ide or here:
http://arduino.cc/en/tutorial/blinkwithoutdelay

the idea this:
define interval (you said want 5s).
you need save millis() variable @ moment want arduino start counting 5s (ex: when door opened).
then on each loop can check if difference between previous millis() (which save variable) , current millis() equal or bigger interval. in other words: can on each loop check if 5 seconds have passed.

it this:
(i'm skipping parts of code, writing relevant "interval checking, nothing door,...)
code: [select]

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

// ...

// open door
   // save time @ door opened
   opentime = millis();

// if door open
   // check interval
   if( millis() - opentime > interval) {
      // close door
   }


good luck!
;)


Arduino Forum > Using Arduino > Programming Questions > help with understanding millis


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support