c - Execute a function for x seconds every y seconds on Arduino -


i execute function x seconds every y seconds on arduino.

i'm trying control resistance heat water in constant rate (1o celsius per minute), thought was: i'll measure rate when it's running whole minute , i'll adjust desired rate.

let's heats 5o celsius per minute, activate resistance 60/5 seconds, not @ once, thinking activating resistance 1 second every 12 seconds keep rate constant no matter (if day cold, hot, if change equipment etc.)

do guys think possible? if not, ideas how can make work? saw timer.h library doesn't seems solve problem =/

thanks in advance, please let me know if information can useful!

i think easiest approach use millis() function. here's 1 example, depend on how control operating.

const int ontime=1000; // in ms const int offtime=12000; // in ms const int resistorpin=7; // change necessary boolean currentlyon=false; unsigned long starttime;  void setup(){   pinmode(resistorpin,output);   digitalwrite(resistorpin,low);   starttime=millis(); // initialize }  void loop(){   if (currentlyon && millis()>starttime+ontime){ // switch resistor off     digitalwrite(resistorpin,low);     currentlyon=false;     starttime=millis(); // reset timer   }   if (!currentlyon && millis()>starttime+offtime){ // switch resistor on     digitalwrite(resistorpin,high);     currentlyon=true;     starttime=millis(); // reset timer   }   delay(10); } 

i have no idea if going work far controlling temperature, since expect there significant non-linearities involved in that, @ least resistor want. if want control temperature, suggest feedback loop in measure temperature , adjust accordingly (maybe pid controller?). that's of course more complicated.

edit: few additional thoughts: reason suggested millis()-based approach because allows have other code executing while wait, making more versatile. if don't need that, can go super-simple , use delay():

void loop(){   digitalwrite(resistorpin,high);   delay(ontime);   digitalwrite(resistorpin,low);   delay(offtime); } 

lastly, hook resistor pwm pin , use analogwrite() instead, keeping resistor activate @ low level.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -