Go Down

Topic: Creating a function (Read 1 time) previous topic - next topic

vceklic

I have not given you enough explanation on what I am trying to do. I have 10 batteries that I am trying to work with hence batPrevMillis[batteryNumber] array to hold previous time for particular battery. I am just trying to pass time to function and do nothing until that time has passed. Example that you show does that but, when time passed I need to reset timer so that when I do some other operations I would call the function with same or different waitTime variable as previous time, so

batPrevMillis[batteryNumber] += waitTime;

does not do the trick.

Problem is that it is not always the same starting point. Any ideas?

Robin2

#16
Jun 10, 2015, 08:37 pm Last Edit: Jun 10, 2015, 08:39 pm by Robin2
Example that you show does that but, when time passed I need to reset timer so that when I do some other operations I would call the function with same or different waitTime variable as previous time, so

batPrevMillis[batteryNumber] += waitTime;

does not do the trick.

Problem is that it is not always the same starting point. Any ideas?
I don't understand and I am not sure what "example" you refer to. Please post the latest version of the code that is causing the problem.

The code quoted above assumes that the wait period is the same for each battery. But it should keep the times for each battery separate - in your Array.

If that is not what you want please explain clearly what is required.

It may be that your assumption that a "generic timer" is needed is not correct. Please explain what your project is trying to do.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

vceklic

OK, I can use array to store values for times but it is generated at runtime, so that is why I pass it into the function.

I will try to explain:

In void loop() if condition is met, I call function waitTime
then in each pass I check if given time is passed and just return false if time not passed, else return true and reset timer for that battery to 0.

Next pass into void loop() I do noting with this function since condition is not met.

Again, when condition (that might be after long time few hours or days) is met, I repeat the process, but this time waitTime might not be the same as last run. I store battery settings in array, so if needed I can add more parameters to it.

Hopefully this explains it a bit more.
Thanks and best regards.

 

vceklic

Thank you for helping. I have created small isolated program to simulate and I think I got it. I just hope that it would survive millis rollover. Here is the code:

long unsigned prevTime[] = {0UL, 0UL, 0UL}; //previous times for batteries
long unsigned waitTime[] = {5000UL, 8000UL, 22500UL};  //simulated times for batteries
int wt[] = {0,0,0}; //flag for battery timer status {0 = timer not running, 1 = timer running}

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  for(int x = 0; x <3; x++){
    if(wt
  • == 0){ //if timer is not runnig

      prevTime
  • = millis(); //get current value of millis

      Serial.println("Curent millis: " + String(prevTime
  • )); //debuging

      wt
  • = 1; //set timer to running

    }
    if(idleTime(x, waitTime
  • )){

      Serial.println("Time has passed for battery " + String(x));     
    } 
  }
}


bool idleTime(int y, unsigned long waitTime){
   if( millis()- prevTime[y] >= waitTime){ //check if time defined has passed
     wt[y] = 0; //reset timer flag to not running
     return true;
   } 
   return false;
 }

Delta_G

Please use code tags.  The code in your last post got mangled with a bunch of little box symbols so I can't read it.  When you post code use the little icon above that looks like </> or manually add them so your code goes in a code box.  

Like this:
Code: [Select]


if(youAddCodeToAPost){
    pleaseUseCodeTags();
}

|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

vceklic

Sorry about that. Here is code:

Code: [Select]

long unsigned prevTime[] = {0UL, 0UL, 0UL, 0UL}; //previous times for batteries
long unsigned waitTime[] = {5000UL, 8000UL, 22500UL, 32000UL};  //simulated times for batteries
int wt[] = {0,0,0,0}; //flag for battery timer status {0 = timer not running, 1 = timer running}

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
 
  
  
  for(int x = 0; x <3; x++){
    if(wt[x] == 0){ //if timer is not runnig
      prevTime[x] = millis(); //get current value of millis
      Serial.println("Curent millis: " + String(prevTime[x])); //debuging
      wt[x]= 1; //set timer to running
    }
    if(idleTime(x, waitTime[x])){
      Serial.println("Time has passed for battery " + String(x));     
    }  
  } 
  
    if(wt[3] == 0){
        prevTime[3] = millis(); //get current value of millis
        Serial.println("Curent millis: " + String(prevTime[3])); //debuging
        wt[3]= 1; //set timer to running
    }
    if(idleTime(waitTime[3])){
        Serial.println("Time has passed for random item");     
      }
  
}


bool idleTime(int y, unsigned long waitTime){
   if( millis()- prevTime[y] >= waitTime){ //check if time defined has passed
     wt[y] = 0; //reset timer flag to not running
     return true;
   }  
   return false;
 }
 
 bool idleTime(unsigned long waitTime){
   if( millis()- prevTime[3] >= waitTime){ //check if time defined has passed
     wt[3] = 0; //reset timer flag to not running
     return true;
   }  
   return false;
 }


Robin2

I think the code in Reply #20 should work.

I now have a better idea of how you want to use the function.

It is probably just personal preference but I think I would have created a batteryCheck() function that includes the timing question. Something like


Code: [Select]
void loop() {
   for (byte batt = 0; batt < 3; batt ++) {
      batteryCheck(batt);
   }
}
void batteryCheck(byte battNum) {
    if( millis()- prevTime[battNum] >= waitTime[battNum]) {
       prevTime[battNum] += waitTime[battNum];
       wt[battNum] = 0;
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up