Pages: [1]   Go Down
Author Topic: How acccurate can I use mye Arduino Mega as a timer?  (Read 793 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Sydney, Australia
Offline Offline
Sr. Member
****
Karma: 6
Posts: 397
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Edison Member
*
Karma: 8
Posts: 1341
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26554
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
God Member
*****
Karma: 25
Posts: 508
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, you should use the millis() and or micros() calls and calculate the difference.
For example:
Code:
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);
  }
}
Logged

Texas
Offline Offline
Edison Member
*
Karma: 25
Posts: 1298
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Good luck, Jack

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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?
« Last Edit: July 19, 2013, 12:26:50 pm by ViggoTW » Logged

India
Offline Offline
Full Member
***
Karma: 0
Posts: 174
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26554
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I would read that as taking 0.17 seconds to turn 60 degrees. 0.34 to turn 120.  0.51 to turn 180.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

USA
Offline Offline
Newbie
*
Karma: 1
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Texas
Offline Offline
Edison Member
*
Karma: 25
Posts: 1298
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Good luck, Jack

Pages: [1]   Go Up
Jump to: