Pages: [1]   Go Down
Author Topic: Timing the duration a test has been running  (Read 1116 times)
0 Members and 1 Guest are viewing this topic.
Detroit, MI
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am looking for the code or library that will allow me to start a timer that will time how long a test is true. For example, I want a timer to start and continue to run as long as the voltage < voltageValue in the first 'if' statement in the code below. Does anyone have any ideas how to do this?

Previously I have tried to do this with the millis(), but since my project will be running for hours at a time, with the voltages changing at irregular intervals, the numbers that the board will have to store will become extremely large. Is there some other way to do what I want to do?

note: start Timing (t); is just a place holder. This is where I want my timer to begin timing.

Code:
if (voltage<voltageValue){
    start Timing (t);
    if ((voltage < voltageValue) && (t > Tup))  // has been up for more than 30 seconds
    {
      stop Timing (t);
      Action = 0; 
      goto Reset;    // Reset
    }
    else if ((voltage > voltageValue) && (t < Tup))  // has sat down before 30 seconds
    {
      stop Timing (t);
      Tdown = t * 30;              // changes up time to new value
      goto beginning ;         //goes to 'beginning' of do loop
    }           
  }
Logged

University of Detroit Mercy
Project D
Pat, Meghan, and Wes

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Previously I have tried to do this with the millis(), but since my project will be running for hours at a time, with the voltages changing at irregular intervals, the numbers that the board will have to store will become extremely large. Is there some other way to do what I want to do?

Millis() is the proper 'tool' to use. You don't need to store each unique elapsed times separably, just keep adding the elapsed time the test was true into a single accumulated total time.

Lefty
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check my stopwatch class - http://playground.arduino.cc/Code/StopWatchClass - could do the job.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Detroit, MI
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a project that I am trying to complete that needs to be able to record the time lapsed that a test has been running. I have utilized a delay (1000) and a counter t++ as a makeshift timer. Will anyone out there please take a look at this code and let me know what they think. The compiler says that this code checks out, but when I hoop up my arduino board, piezo buzzer, and pressure sensor the system is not doing of these activities. Where is my problem????

Code:
void setup()
{
  Serial.begin(9600);
  pinMode(9, OUTPUT);  // Piezo Buzzer
}
void loop ()
{
  int counter = 0; 
Reset:
  int sensorValue = analogRead(A0);        // reads value from force sensor located in A0
  int voltage = sensorValue * (5.0 / 1023.0);  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  int voltageValue = 2.0;      // sets the pressure limit, so that when pressure is higher than this value considered "high pressure" sets threshold for high and low
  int Tup=30000;                  // Initial Tup is 30 sec
  int Tdown=900000;              // Initial Tdown is 15 min (900 sec)
  int Action;             // When action == 1 carry out the do loop
  int t=0;                // Time counter

  if (voltage >= voltageValue) {
    Action=1;
    Serial.println(voltage);      // print out voltage
    do{
beginning:
      delay (1000);        // start Timing (t);
      if ((voltage> voltageValue) && (t > Tdown)) // has been sitting for 15 minutes
      {
        digitalWrite(9,HIGH); // Buzzer goes off for 3 seconds;
        delay (3000);
        digitalWrite(9,LOW);
        if (voltage > voltageValue){
          counter++; // Count missed times
          Action = 0;
          goto Reset; // Reset
        }
        goto Relief;                     // Relief function
      }
      else if ((voltage<voltageValue) && (t < Tdown))  // has gotten up before 15 minutes is up
      {
        Tup = t / 30;                // changes Tup value for next test in Relief function
        t = 0;
        goto Relief;                        // Relief function
      }
      t++;                          // time counter increases by 1
    } 
    while(Action=1);
Relief:
    while (voltage<voltageValue){
      delay (1000);                  // start Timing (t);
      if ((voltage < voltageValue) && (t > Tup))  // has been up for more than 30 seconds
      {
        t = 0;                   // resets time counter
        Action = 0;              // will not return to do loop
        goto Reset;    // Reset
      }
      else if ((voltage > voltageValue) && (t < Tup))  // has sat down before 30 seconds
      {
        Tdown = t * 30;              // changes up time to new value
        t = 0;                          // resets time counter
        goto beginning ; //goes to 'beginning' of do loop
      } 
      t++;                        // increase time counter by 1
    }
  }
}
Logged

University of Detroit Mercy
Project D
Pat, Meghan, and Wes

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The usage of the goto statement is considered bad programming because it corrupts heaps and stacks the computer uses to keep its internal administration.

int Tdown=900000;   

will overflow the int as the max value an int can hold is 32767.

use long as type

What can be useful is to introduce functions that do part of the work. At the highest level you get something like

Code:

void loop()
{
  float voltage = readVoltage();  // hides all the A0 and converting

  if (doAction1() == true)
  {
    doRelief();
  }
}

bool doAction1()
{
  ....
  if (...) return false;
  ...
  return true;
}

void doRelief()
{
  ....
  return;
}

Hope this helps to get you rewriten your code
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: