I was wondering if there is any function that measures time from the moment You called it until the programm is ended. For example, millis() gives You time that has passed since the start of the program, but I need something that will give me back time that has passed since I called it.
Yeah I thougt about that but it isnt soultion for the problem I have.
I want my program to last lets say 12 seconds.
If I use millis I will always have constant difference between the time I first called it and the second time I called.
That is true but perhaps I havent been clear enough about what I want.
The problem is that I need a loop condition (perhaps for loop would be the best) which would make my program run for exactly 12 secs. Now what condition would I use? Maybe the only solution would be to run only the program itself, measure how much time is needed for it to finish 1 cycle with millis as siggested before, and then use that info in for loop (example - lets say programm runs for 50ms then for loop would be: for(float i=0; i<12000.0;i+50.0). I thought about that but it doesent seem practical to me...
unsigned long startTime = millis();
while (millis() - startTime < 12000UL)
{
// This loop will execute repeatedly for 12 seconds
// Note: nothing else in the program will happen during these 12 seconds,
// which may or may not be what you want
}
ADDED ...
Or maybe you are after something like this ...
unsigned long startTime;
void setup()
{
//
// All your other setup stuff
//
startTime = millis(); // Check time just before loop() is first executed
}
void loop()
{
if (millis() - startTime > 12000UL)
{
while (1); // Loop forever here to halt the program after 12 seconds
}
//
// All your other loop stuff
//
}
ggmu5:
The event should be the end of the current program...
I strongly suspect that is the wrong way to think of the problem.
You probably need to have the timing process in mind throughout the program so that the timed event is under control all the time - rather than trying to add time at the end.
Can you write down the sequence you want to achieve - each part on a separate line - for example
turn on oven
wait for temperature
insert chicken
note time
check for cooking time expired
remove chicken
Ok I made another topic on my problem but it seems I must explain all so You could understand what Im aiming for. I am trying to make the program that will count the number of exhales of a person in a minute.
I am using didgital sensor DS18B20 to measure the temperature under the persons nostrils. When a person exhales the air is warmer so when the current temperature is higher then previous one that equals to 1 exhale. I have problem thinking of a code that will give me info on serial monitor about how many times per min does a person exhales... I have been busting my head for last couple of days trying to figure it out and this is my latest attempt:
#include <OneWire.h>
#include <DallasTemperature.h>
//broj digitalnog pina
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup(void) {
// inicijaliziranje serijske komunikacije
Serial.begin(9600);
// startanje biblioteke
sensors.begin();
}
// the loop routine runs over and over again forever:
void loop(void)
{ //dobavljanje prve 2 temperature u varijable
sensors.requestTemperatures();
float value1 = sensors.getTempCByIndex(0);
delay(1200);
sensors.requestTemperatures();
float value2 = sensors.getTempCByIndex(0);
//provjera je li izdisaj
if(value2>value1)
{ //pocetak mjerenja vremena
float time1=millis();
delay(1200);
//dobavljanje 3. varijable
sensors.requestTemperatures();
float value3 = sensors.getTempCByIndex(0);
//provjera je li drugi izdisaj
if(value3>value2)
{//kraj mjerenja vremena
float time2 = millis();
//period izmedju 2 izdisaja
float p = time2 - time1;
//frekvencija-broj izdisaja po minuti
float f=60000.0/p;
Serial.println("Broj izdisaja po minuti je:");
Serial.println(f);
}
}
}
The problem with this one is that i get either stupid number like 47.5 or nothing on serial monitor.
What I was trying to do with this program code that I posted is to count the first time person exhales and then use start=millis(); as a start time, and the second time when person exhales I would use finish=millis(); as final time and the difference between those 2 times is one period. For the frequency per minute all I need is to devide 60000.0/period since the millis gives value in millieseconds and I need it in minutes (exhales per min). I need this program to repeat itself.
The other solution I want to try is to put it in for or while loop and run it for 12 seconds and inside that loop to count the number of exhales. Then when the 12s finishes I would multiply the number of exhales by 5 to get number of exhales per min. Also I would need this program to repeat itself.
The comments are in my language so You wont probably understand them but I explained everything here.
I tried using while loop like in first example that Hackscribble posted.
Finally I have got normal numbers but I still have a little issue.
#include <OneWire.h>
#include <DallasTemperature.h>
//broj digitalnog pina
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float sum = 0;
float prevTemp=0;
float f;
void setup(void) {
// inicijaliziranje serijske komunikacije
Serial.begin(9600);
// startanje biblioteke
sensors.begin();
}
// the loop routine runs over and over again forever:
void loop(void)
{
unsigned long startTime = millis();
while (millis() - startTime < 12000UL)
{
sensors.requestTemperatures();
float nowTemp = sensors.getTempCByIndex(0);
Serial.println(nowTemp);
delay(1200);
Serial.println(prevTemp);
if (nowTemp > prevTemp)
{
sum = sum + 1;
}
prevTemp = nowTemp;
}
f = sum*5;
Serial.println("Broj izdisaja po minuti je:");
Serial.println(f);
sum=0;
}
The problem is that now I get temperature values every 1.2sec (I wanted that to get sensor a little time to recover and I will not be testing people that are running so it is physically impossible for someone who doesent run to exhale more frequently than every 1.2secs). The problem part is that it only compares 2nd value with first one (example. I will get values 30.0 30.5 31.0 30.5, my programm will only compare first two 30.5 and 30.0 and second two 31.0 and 30.5...). I need it to compare every neighboring value (30.0 and 30.5, 30.5 and 31.0, 31.0 and 30.t, etc...). I was thinking of implementing a vector values that will store lets say 20 values because there wont be more in 12 secs, and than put the condition in if loop like if (values[i+1]>values*). I have had problems with vectors before because they usually complicate things and the program usually ends up not working properly. Do You guys have any other suggestion besides vector or if its vector whats smart way to implement it?* P.S. Robin2 I have read Your post. Should prevTemp have initial value 0? Also I didnt want to measure for whole minute in the loop like You have put because it is a lot of time to wait for a result, I was thinking about reducing period to 12 secs and then multiply it by 5 to get per min.
I have to analyze the values inside the 12 secs period...I dont have to have 12 values stored at the time I just have 12 secs period in which my sensor gives me lets say 20 values of temperatures...Then I have to analyze those 20 values that were mesured in that period (compare the neighbouring pairs 1. with 2., 2. with 3., 3. with 4.,etc.). Then I should store the number exhales that happened in that period in some variable (sum) and when the 12 sec loop is over I just multiply that sum with 5 to get exhales per minute...That was the idea anyway...
P.S.
And ofcourse when 1 cycle is over I need the program to repeat it self and do the same thing again, I think I came very close to the final solution with this code, but sometimes I still get values on serial monitor that dont actually match the exact number of times I exhaled in that period, although now the numbers on the monitor are pretty realistic and sometimes even correct This is my final version of the code for now:
#include <OneWire.h>
#include <DallasTemperature.h>
//broj digitalnog pina
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float sum = 0;
float f;
void setup(void) {
// inicijaliziranje serijske komunikacije
Serial.begin(9600);
// startanje biblioteke
sensors.begin();
}
// the loop routine runs over and over again forever:
void loop(void)
{
float prevTemp=0;
unsigned long startTime = millis();
while (millis() - startTime < 10000UL)
{
sensors.requestTemperatures();
float nowTemp = sensors.getTempCByIndex(0);
delay(1200);
if (nowTemp > prevTemp)
{
sum = sum + 1;
}
prevTemp = nowTemp;
}
f = (sum-1)*6;
Serial.println("Broj izdisaja po minuti je:");
Serial.println(f);
sum=0;
}
NOTE:
In final formule I have put (sum-1)+6 cause I lowered loop period to 10 secs and minus 1 is cause I have put the inital temperature of prevTemp to 0 so any temp on the sensor will be higher than that but that doesent mean its an exhale so this way I have eliminated that.
ggmu5:
P.S.
Robin2 I have read Your post. Should prevTemp have initial value 0? Also I didnt want to measure for whole minute in the loop like You have put because it is a lot of time to wait for a result, I was thinking about reducing period to 12 secs and then multiply it by 5 to get per min.
It would be very easy to modify my code to do a different time interval. That is why I put the interval in a variable.
As to whether prevTemp should have an initial value - just try it and see. Oftentimes it does not matter because if it starts with the wrong value it gets "fixed" very quickly.
The code suggestion I posted was just intended to give you the idea of how I would do it - something to think about.