Go Down

Topic: How acccurate can I use mye Arduino Mega as a timer? (Read 999 times) previous topic - next topic

ViggoTW

I got my Arduino Mega 2560 R3 half a year ago, so I'm not an expert or anything like that. I've recently started a project where it is necessary for me to time something with high accuracy.
What I'm timing will last for somewhere between five and ten seconds, closer to five. I need accuracy down to approximately +/- 10 milliseconds. What would be the best way to get this?
A loop with a variable that increases with +1, then delay(1)? (I've heard that one should try to avoid the delay-function)
An interrupt of some sort?
Should this be done only by software, or are there some hardware, in addition to the arduino, required?

(sorry for not being more spesific about the project, it's still to early)

craigcurtin


I got my Arduino Mega 2560 R3 half a year ago, so I'm not an expert or anything like that. I've recently started a project where it is necessary for me to time something with high accuracy.
What I'm timing will last for somewhere between five and ten seconds, closer to five. I need accuracy down to approximately +/- 10 milliseconds. What would be the best way to get this?
A loop with a variable that increases with +1, then delay(1)? (I've heard that one should try to avoid the delay-function)
An interrupt of some sort?
Should this be done only by software, or are there some hardware, in addition to the arduino, required?

(sorry for not being more spesific about the project, it's still to early)


conceptually until you tell us more about the project

use an interrupt to fire off the start of your routine and record the millis count - use another interrupt to signal the finish of the measurement and take the millis count there - subtract one from the other

Craig

sbright33

No need to use interrupts for 10ms accuracy.  Just save millis() of start, and millis() of end in loop().  Unless you're trying to do 2 or more things at once?
If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

ViggoTW

This is what I can tell about the project so far:
When I push a button, I want a servo to be turn to a pre-configured angle. When I release it, I want it to go back. Nothing fancy, so I thought a delay() would be enough

I haven't got the servo yet, so this is what I wrote so far.
Code: [Select]
int counter = 0;
boolean flag = LOW;

void setup()
{
  Serial.begin(9600);
  pinMode(22,INPUT); // Button that is to be pushed
}

void loop()
{
  while(digitalRead(22)==HIGH) // WWhile the button is pushed down..
  {
    counter++;
    flag = HIGH;
    delay(1);
  }
 
  while(flag==HIGH)
  {
    Serial.println(counter);
    counter = 0;
    flag = LOW;
  }
}


Turns out this gives me readings that are not accurate at all. sometimes it even misses with +/- 0,5 seconds.

CrossRoads

Why not use the Servo.h library to control where the servo goes to?
Then holding the position while the button is pressed is trivial, whether for a pre-determined amount of time, or only while the button is held.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

TanHadron

Yeah, you should use the millis() and or micros() calls and calculate the difference.
For example:
Code: [Select]

void loop()
{
  unsigned long timeStamp;

  if (digitalRead(22) == HIGH)
  {
    timeStamp = millis();
    while (digitalRead(22) == HIGH);    // do nothing while the button is pushed
    Serial.println(millis() - timeStamp);
  }
}

jack wp

I have used the servo.h library with success.

If you have no other time hungry routines running, the timing will be NO problem.

Jack
Good luck, Jack

ViggoTW

#7
Jul 19, 2013, 06:32 pm Last Edit: Jul 19, 2013, 07:26 pm by ViggoTW Reason: 1
Thanks for your replys, I think I will be able to complete the timer-part now =)

One more thing regarding the servo, though. I am using the Servo.h-library. I'm using a servo that came along with my arduino, called "TowerPro MG995 digi high-speed" and I'm having a problem controlling it.
I made a code that's pretty simple, just to test it out:

Code: [Select]
#include <Servo.h>

Servo servo1;

void setup(void)
{
 servo1.attach(10);
}

void loop(void)
{
 servo1.write(0);
 delay(1000);
 servo1.write(25);
 delay(1000);
 servo1.write(90);
 delay(2000);
}


For some reason, this did not work. It's not accurate at all, and sometimes it makes some really weird noises. :S
Am I correct that I can just hook the cables up directly to the arduino, no resistors required?

EDIT: Also, when powered by the USB, it keeps disconnecting and re-connecting from the computer while the servo is connected. Drawing to much current?

The above code has worked well with me. You are right...you don't need any resistors.

ViggoTW

Hmm. I checked out my servo, and on this site it said:
Operating Speed (4.8V no load) : 0.17sec / 60 degrees

Does this mean that the servo can only turn 60-degrees? Might that be the problem?

CrossRoads

I would read that as taking 0.17 seconds to turn 60 degrees. 0.34 to turn 120.  0.51 to turn 180.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

ViggoTW

Hmm.. So no good answer for why my arduino is connecting/disconnecting from my compter when trying to adjust the servo? =\

GunnerCAF

You can try another USB port and see if it solves the problem.  If you still have this problem try to power the servo or servos from a separate power source. 

This will tell you if it's a current draw issue.  The USB port has a limit on how much power it will deliver.

Dave

jack wp

Try using the servo sweep example. It only moves a short distance at a time, and should require less power to do that. You can increase the delay in the sweep code from 15 to something larger (eg 90), to reduce power supply needs even further. Just as a test.

I do expect you will need an external power supply for the servo, as they can use a lot of power if pushed.

Jack
Good luck, Jack

Go Up