element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum timelaps
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 9 replies
  • Subscribers 419 subscribers
  • Views 955 views
  • Users 0 members are here
  • arduino
Related

timelaps

e14 Contributor
e14 Contributor over 12 years ago

Hi

I'm a arduino rookie image

I'm learning using the starter kit arduino uno

I did the tutorial with the tmp sensor

What I want to do is to compute the time difference between the first led 2 and the last one 4

here is the code that I did:

Thanks for any HELP!

 

cheers

D

 

||||||||||||||||||||||||||||||||||||||||||||||||||

 

const int sensorPin = A0;

const float baselineTemp = 23;

//unsigned start, finished, elapsed;

 

 

 

 

void setup (){

  Serial.begin(9600); // open serial port

 

 

  for (int pinNumber = 2; pinNumber< 5; pinNumber++) {

    pinMode(pinNumber, OUTPUT);

    digitalWrite(pinNumber, LOW);

  

  }

}

 

 

void loop (){

  int sensorVal = analogRead(sensorPin);

  elapsed=finished-start;

  Serial.print("MILL elapsed");

  Serial.print(elapsed);

 

 

 

  Serial.print("Sensor Value: ");

  Serial.print(sensorVal);

 

 

  //convert adc readgin to voltage

  float voltage = (sensorVal/1024.00) * 5.00;

  Serial.print(", Volts:");

  Serial.print(voltage);

  Serial.print(", Degrees in C: ");

 

 

  //conver voltage to degrees;

  float temperature = (voltage - 00.5)*100;

  Serial.print (temperature);

 

 

  //convert C to F

  float F =(temperature *1.8 + 32);

  Serial.print(", Volt: ");

  Serial.println (F);

 

 

  delay(500);

 

 

  if(temperature < baselineTemp) {

    digitalWrite(2, LOW);

    digitalWrite(3, LOW);

    digitalWrite(4, LOW);

 

 

  }

  else if (temperature >=baselineTemp+1 && temperature < baselineTemp+2) {

    digitalWrite(2, HIGH);

    digitalWrite(3, LOW);

    digitalWrite(4, LOW);

    //start=millis();

    //Serial.print(start);

    //Serial.print("START  ");

   

  }

  else if (temperature >=baselineTemp+2 && temperature < baselineTemp+3) {

    digitalWrite(2, HIGH);

    digitalWrite(3, HIGH);

    digitalWrite(4, LOW);

 

 

  }

  else if (temperature >=baselineTemp+3 && temperature < baselineTemp+4) {

    digitalWrite(2, HIGH);

    digitalWrite(3, HIGH);

    digitalWrite(4, HIGH);

    //finished=millis();

  }

}

  • Sign in to reply
  • Cancel
  • jw0752
    jw0752 over 12 years ago

    Hi Dani,

    I am also a new guy so my advice will not be the final word but my first project involved working with time too. The adruino has an internal counter that you can access using  millis().  I would read millis() and assign its value to a variable. later when I wanted to know the elapsed time I would use a statement like Elapse =  millis() - variable. This will give you a value for the difference in the start and the finish. Millis() is an unsigned long so you should use similarly assigned variable when you work with it. The counter in the arduino runs at 1 k hz so you get a thousand counts per second. If we read millis() at one point and then subratct that first reading from a second and the value is 60,000 we would know that one minute has passed. Check the Arduino home site as they have more information on using millis(). Good luck

    John

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to jw0752

    Hi

    thanks

    here is the problem:

    I have a loop

    I need to take the time between led1 ON and led 2 ON (it is just an example)

    the problem is because the variableSTART = millis() is inside the loop, I  overwrite the value inside the  variable every time the led1 is ON

    so that isn't working

    any idea ?

    cheers

    d

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jw0752
    jw0752 over 12 years ago in reply to e14 Contributor

    Hello again Dani;

    Based on what you have explained I would say to start an incremental variable inside the loop.

    X= 1

    Each time you go through the loop increment the variable by 1

    x= x+1

    Put in a statement like

    If ( x = 1)  { start = millis(); }

    In this way the start will be assigned the value of millis() only on the first pass of the loop, on all subsequent passes the value of x will not be 1 and the "If" statement will fail and drop through to the next statement in the program.

    There are nice ways to set up the incrementing variable. I am very green to "C" programming so some of the others may have a better way of doing this.

    Hopefully I understood your question.

     

    John

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jw0752
    jw0752 over 12 years ago in reply to e14 Contributor

    2nd Reply;

    Back Again,

     

    I looked at your program a little more and you may want to make sure your variables start and finish are assigned as unsigned long and you may want to set their registers to 0 at the beginning of the program.

     

    unsigned long start = 0;   //  value of start time

    unsigned long finish = 0;  //  value of finish time

     

    Keep in mind that your elapsed time will then be   finish - start / 1000 with the answer now in seconds.

     

    If you are primarly interested in the elepsed time you can save a step by  using a statement like

     

    elapse = millis() - start    and not have to bother with a variable for the finish time.

     

    I see that you have done some of this at the top of your loop.  I find it is usefull to draw a logic flow chart for problems like this. I strongly suspect that you need a "do" , "while", or other loop inside your main loop so that you can complete your timing process, print your data, time, temp, and etc before you leave the internal loop and return to the beginning of the void loop().

     

     

    John

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mcb1
    mcb1 over 12 years ago in reply to jw0752

    dani/John

     

    One option is to seperate the code into a number of routines.

    The main loop simply 'calls' the others for reading the temp, and displaying the leds, etc.

     

    There is an example I did here which you can download.

    image

    http://www.theshedmag.co.nz/online/downloads/arduino-downloads

     

    The sketch has a Timer(), SingleWipe(), Check_Buttons() function which are 'called' as required.

     

    void loop() 
    {
      Check_Buttons();                                        //used for Int wiper
      Timer();                                                //See what is needed to be done
    }
    
    void Timer()
    {
      // This controls the various routines and calls them as necessary
      TimerTime = millis() - LastWipeTime;
      
      if (((millis()-ButtonPressTime) > ButtonTimeout) && (IntWipe == 1))        // 30 Sec timeout for no button press, but only after stopped, or 1 start.
      {
        IntWipe = 0;
        LastWipeTime = 0;
        WipeInterval = 0;   
      }
          // The SingleWipe() will be called when the start button is pressed, we need to turn the relay off.
      if ((TimerTime > RelayOnTime) && WiperRelayState == true)    // RelayOnTime has been exceeded AND WipeRelay is HIGH
      {
        SingleWipe();                                          // It will turn off the relay and not modify any variables
      }
         
      // Time to wipe but only if there is a WipeInterval AND the relay is off.      
      if ((TimerTime > WipeInterval) && (IntWipe == 2) && WiperRelayState == false)       
      {
        SingleWipe();
      }
      
    }
    
    void Check_Buttons()
    {
      if (millis() - LastButtonCheck > 5)                      // Reads button state every 5mS and then updates button counts
      {
        StartButtonState = digitalRead(StartButton);
        StopButtonState = digitalRead(StopButton);
        LastButtonCheck = millis(); 
        
        if (StartButtonState == LOW)
        {
          
          StartCount ++;                                      // Increment the Count by 1
          if (StartCount > 10)                                // the button should be LOW for 10x5mS = 50mS
          {
            ButtonPressTime = millis();                      // used for the Button press Timeout
            StartCount = 0;
            if (WiperRelayState == false)                    // No point in returning until its finished doing a wipe.
            {
              switch (IntWipe)
              {
                case 0:
                  // not been pressed or fully stopped.
                  IntWipe =1;
                  SingleWipe();
                break;                                            // Escape from the switch, so we don't do the next one
                
                case 1:
                  // single press or stop button pressed.
                  IntWipe = 2;
                  WipeInterval = (millis() - LastWipeTime);       // Time will be in mS
                  SingleWipe();
                break;                                            // Escape from the switch, so we don't do the next one
                
                case 2:
                  // 2nd press
                  WipeInterval = (millis() - LastWipeTime);       // Time will be in mS
                  SingleWipe();
                break;                                            // Escape from the switch, so we don't do the next one
              
              }
            }
          }
        }
        else                                                  // StartButton is HIGH
        {
          StartCount =0;
        }
        
        if (StopButtonState == LOW)
        {
          
          StopCount ++;
          if (StopCount >10)                                  // the button should be LOW for 50mS
          {
            ButtonPressTime = millis();                       // used for the Button press Timeout
            IntWipe = 1;
            StopCount = 0;
            WipeInterval = 0;
            LastWipeTime = millis();                          // Set the wipe timer, so when the start is pressed the interval will be right
          }
        }
        else                                                  // StopButton is HIGH
        {
          StopCount =0;
        }
      }
    }
    
    void SingleWipe()
    {
      if (WiperRelayState == false)                          // Not much point in wiping if we already doing a wipe.
      {
        LastWipeTime = millis();
        WiperRelayState = true;
        digitalWrite(WiperRelay, HIGH);
        digitalWrite(LEDPin, HIGH);
      }
      else
       {
        WiperRelayState = false;
        digitalWrite(WiperRelay, LOW);
        digitalWrite(LEDPin, LOW);
       }
       return;
    }

     

    The above sketch was one of two seperate programs using the same Arduino and running at the same time, hence no delay(xxx) are used.

    Once you start using seperate routines, you'll wonder why you didn't earlier.

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jw0752
    jw0752 over 12 years ago in reply to mcb1

    Hi Mark;

     

    Thank you very much for your input. I looked over your example program and for the first time I can see what is meant by subroutines and how to use them. While I remember programming in this fashion 45 years ago when I learned Fortran and Cobol a lot of water has subsequently passed under the bridge. I want to encourage Dani to continue to the goal of getting the program to do exactly what it is suppose to do. Only by trial and error do we really learn a subject to its depths. Programming is fascinating as there are always many ways to accomplish a given task. Most of the time the joy is in the process with a momentary reward when we get it right.

     

    John

     

    PS.  Dani you will note Mark's excellent use of // notes explaining what each statement is doing. You really can't have too many notes if you hope to understand your program easily when you look at it in the future or if you hope to have another programmer understand it. As I myself am very green in programming C+ for arduino I write more notes on my programs than code.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to jw0752

    Hi

    thanks!

    tons of INFO here.

    I will try as soon as possible text a lot

     

    Cheer

    Dani

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mcb1
    mcb1 over 12 years ago in reply to e14 Contributor

    You really can't have too many notes if you hope to understand your program easily when you look at it in the future

    Agreed.

    I find it is usefull to draw a logic flow chart for problems like this

    I also agree and you really don't need to panic about using the correct shape when doing your sketch.

    I often do it before or during some of my programs.

     

    mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to jw0752

    hi thanks all tons of info as usual

     

    I tested the idea to use the boolean operation in order to count up and down

    and it works perfectly

    NICE one

    I will try the face led example as well.

     

    thanks for the suggested book.

     

    cheers

    D

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube