Millis Timer


i thought had figured out after completing project cant delay using millis work in new project. below smaller version of code run on uno simulator (real board mega) changed pin numbers etc run on uno.

what trying do, use arduino turn on relay connects motor variable frequency drive. after waiting 5 seconds turn vfd on. when switched off, turn vfd off , wait 20 seconds before disconnecting 3phase relay make sure vfd can ramp down first. code repeated multiple times multiple machines connected same vfd.

ok whats happening, first time code run (in simulator) delays seems behave correctly. after delays dont happen @ all.

there not understanding on using millis delay.

thanks help
code: [select]

// initialize inputs:
const int dpi = 2; //drill press
const int pli = 3; //planer



// intialize outputs:

const int dp = 4; // relay 3 drill press
const int pl = 5; // relay 4 planer
const int vfd = 6;
const int dc = 7;

// variables change:
int dpistate = 0;
int plistate = 0;

unsigned long timerdpon = 0;
unsigned long timerdpoff = 0;
unsigned long timerplon = 0;
unsigned long timerploff = 0;



void setup() {
  // put setup code here, run once:
//drive relays high begin

digitalwrite(dp, high);
digitalwrite(pl, high);
digitalwrite(vfd, high);
digitalwrite(dc, high);


// pin outputs ouputs
pinmode(dc, output);
pinmode(dp, output);
pinmode(pl, output);
pinmode(vfd, output);


  // pin inputs inputs:
pinmode(dpi, input_pullup);
pinmode(pli, input_pullup);
}

void loop() {

 
  dpistate = digitalread(dpi);
  plistate = digitalread(pli);

drillpres();
planer();

}



void drillpres(){ 


  if (dpistate == low && plistate == high) {
    // turn dp relay on , turn on inverter
    unsigned long currentdpmillis = millis();
    digitalwrite(dp, low);
    if (currentdpmillis - timerdpon >= 5000){
    digitalwrite(vfd, low);
    unsigned long timerdpoff = millis();
  }
 
  }
 
 else if (plistate == low){
   
  }
  else {
    digitalwrite(vfd, high);
    unsigned long currentdpmillis = millis();
    if (currentdpmillis - timerdpoff >= 5000){
    digitalwrite(dp, high);
    unsigned long timperdpon = millis();
  }
  }
}



void planer(){


  if (plistate == low && dpistate == high) {
    // turn planer relay on , turn on inverter
    unsigned long currentplmillis = millis();
    digitalwrite(dc, low);
    digitalwrite(pl, low);
    if (currentplmillis - timerplon >= 5000){
    digitalwrite(vfd, low);
    unsigned long timerploff = millis();
   
  } 
  }
  else if (dpistate == low){
   
 }
  else {
    digitalwrite(vfd, high);
    unsigned long currentplmillis = millis();
    if (currentplmillis - timerploff >= 5000){
    digitalwrite(pl, high);
    digitalwrite(dc, high);
   unsigned long timerplon = millis();
   
  }
  }
}


hi,

quote
there not understanding on using millis delay.
i don't think millis() misunderstanding. think may variable scope.

take line example:

code: [select]

unsigned long timerdpoff = millis();


by putting "unsigned long" in front of variable name, declaring new, local variable. not assigning millis() global variable declared @ top of code, suspect intended. new local variable has same name global 1 declared earlier, scope limited code block in declared. @ end of execution of block, ceases exist , value lost. inside block, reference make variable name refers local variable, have made hole in scope of global variable.

hope made sense. if not, ask.

paul


Arduino Forum > Using Arduino > Programming Questions > Millis Timer


arduino

Comments