Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: millis not accurate? on: June 20, 2011, 09:03:03 pm
I think what he is saying is that, if you take 40 seconds to press the "start" button the clock starts at zero but the trigger is already 40 seconds in.

I don't think that would be correct as trigger() is called just once in setup(), so it would be near zero, if not zero.
2  Using Arduino / Programming Questions / Re: millis not accurate? on: June 20, 2011, 04:28:45 pm
Okay, after using the following code (more of a timer), my clock has fallen behind 40 seconds after 10 hours.

Code:
#define displayPin 2
#define goPin 3

int second = 0, minute = 0, hour = 0;
boolean displayIsPressed = false;
boolean goPinIsPressed = false;
boolean startClock = false;
unsigned long aTime = 0;

unsigned long trigger;
unsigned long secondDelay = 1000;

void setup(){
  Serial.begin(9600);
 
  pinMode(displayPin, INPUT);
  pinMode(goPin, INPUT);
 
  trigger = millis();
}

void loop(){ 
  /**********  CHECK BUTTONS  **********/
  // Display time Button
  if(buttonPressed(displayPin) && !displayIsPressed)
  {
    displayIsPressed = true;
    serialOutputTime();
  }
  else if(!buttonPressed(displayPin))
  {
    displayIsPressed = false;
  }
  // Go button
  if(buttonPressed(goPin) && !goPinIsPressed)
  {
    goPinIsPressed = true;
    startClock = true;
    aTime = millis();
  }
  else if(!buttonPressed(goPin))
  {
    goPinIsPressed = false;
  }
 
 
  if(startClock == true)
  {
    updateTime(aTime);
  }
}

void updateTime(unsigned long adjustment){
  /**********  UPDATE TIME  **********/
  // Move forward one second every 1000 milliseconds
  while(millis() - adjustment - trigger >= secondDelay)
  {
    second++;
    trigger += secondDelay;
  }
  // Move forward one minute every 60 seconds
  if(second > 59)
  {
    minute++;
    second = 0;
  }
  // Move forward one hour every 60 minutes
  if(minute > 59)
  {
    hour++;
    minute = 0;
  }
}

boolean buttonPressed(int aPin){
  if(digitalRead(aPin) == HIGH)
  {
    return true;
  }
 
  return false;
}

void serialOutputTime()
{
  Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
  Serial.print(":"); // a colon between the hour and the minute
  Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
  Serial.print(":"); // a colon between the minute and the second
  Serial.println(second, DEC); // the second, sent to the screen in decimal format
}

This is a much better error, but I would still like to make it a little closer, so I will try adding an adjustment.
By my calculations It seems my crystal is off by 40 seconds every 10 hours or 36000 seconds, so to compensate for this:

40 / (10 * 60 * 60) = 0.00111111111

Now:

0.00111111111 * 1 000 = 1.11111111

So I will adjust my "secondDelay" from 1000 to 998.99999.

Let me know if there are any errors in my math.
3  Using Arduino / Programming Questions / Re: millis not accurate? on: June 19, 2011, 04:20:09 pm
@FalconFour I did try your code but it work about the same as the others. I think that the serial.Print was the culprit, as when I changed it to display every minute things worked out much better. I have now changed it to every 15 mins and things are working out better still.
4  Using Arduino / Programming Questions / Re: millis not accurate? on: June 19, 2011, 11:04:44 am
Using the suggestions in the thread I think I fixed the overflow issue. I also change it to only output the time every minute, so that if printing to the serial causes time loss then it will disappear. Let me know if there are any obvious problems with this, but otherwise I will let it run for a couple of hours.
Code:
#define minButtonPin 2
#define hourButtonPin 3

int second=0, minute=0, hour=1;
boolean minButtonIsPressed = false;
boolean hourButtonIsPressed = false;

void setup(){
  Serial.begin(9600);
 
  pinMode(minButtonPin, INPUT);
  pinMode(hourButtonPin, INPUT);
}

void loop(){
  static unsigned long lastTick = 0; // holds the last time we moved forward one second
 
  /**********  CHECK BUTTONS  **********/
  // Check Minute Button
  if(buttonPressed(minButtonPin) && !minButtonIsPressed)
  {
    //Serial.println("Minute was pressed");
    minute++;
    minButtonIsPressed = true;
    serialOutputTime();
    delay(10);
  }
  else if(!buttonPressed(minButtonPin))
  {
    minButtonIsPressed = false;
    delay(10);
  }
  // Check Hour Button
  if(buttonPressed(hourButtonPin) && !hourButtonIsPressed)
  {
    //Serial.println("Hour was pressed");
    hour++;
    hourButtonIsPressed = true;
    serialOutputTime();
  }
  else if(!buttonPressed(hourButtonPin))
  {
    hourButtonIsPressed = false;
  }
 
  /**********  UPDATE TIME  **********/
  // Move forward one second every 1000 milliseconds
  unsigned long now = millis ();
  if(now - lastTick >= 1000)
  {
    lastTick = now;
    second++;
  }
  // Move forward one minute every 60 seconds
  if(second > 59)
  {
    minute++;
    second = 0;
    serialOutputTime();
  }
  // Move forward one hour every 60 minutes
  if(minute > 59)
  {
    hour++;
    minute = 0;
  }
  // Reset the hour
  if(hour > 12)
  {
    hour = 1;
  }
}

boolean buttonPressed(int aPin){
  if(digitalRead(aPin) == HIGH)
  {
    return true;
  }
 
  return false;
}

void serialOutputTime()
{
  Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
  Serial.print(":"); // a colon between the hour and the minute
  Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
  Serial.print(":"); // a colon between the minute and the second
  Serial.println(second, DEC); // the second, sent to the screen in decimal format
}
5  Using Arduino / Programming Questions / Re: millis not accurate? on: June 19, 2011, 08:04:38 am
@ CrossRoads after letting your thing run overnight (10 hours, it was 8 mins behind).

Trying Coding Badly's code with the while.

As for my hardware, I have an Arduino Uno that was purchased about 7 months ago.

edit: @ Nick Gammon I thought millis would still count during the delay()
6  Using Arduino / Programming Questions / Re: millis not accurate? on: June 18, 2011, 09:02:38 pm
Try this:

  // Move forward one second every 1000 milliseconds
duration = millis()-lastTick;
  if(duration >= 1000)
  {
    lastTick = millis() + duration - 1000;  //attempt to capture the time overage
    serialOutputTime();
    second++;
  }

I just noticed the slight overrun. I have this running and will report back tomorrow morning to see if it is still accurate. Thanks
7  Using Arduino / Programming Questions / millis not accurate? on: June 18, 2011, 04:41:51 pm
After running for 4 hours it is about 3 mins behind. I assume this is a problem with my code:

Code:
#define minButtonPin 2
#define hourButtonPin 3

int second=0, minute=0, hour=1;
boolean minButtonIsPressed = false;
boolean hourButtonIsPressed = false;

void setup(){
  Serial.begin(9600);
 
  pinMode(minButtonPin, INPUT);
  pinMode(hourButtonPin, INPUT);
}

void loop(){
  static unsigned long lastTick = 0; // holds the last time we moved forward one second
 
  /**********  CHECK BUTTONS  **********/
  // Check Minute Button
  if(buttonPressed(minButtonPin) && !minButtonIsPressed)
  {
    //Serial.println("Minute was pressed");
    minute++;
    minButtonIsPressed = true;
    serialOutputTime();
    delay(10);
  }
  else if(!buttonPressed(minButtonPin))
  {
    minButtonIsPressed = false;
    delay(10);
  }
  // Check Hour Button
  if(buttonPressed(hourButtonPin) && !hourButtonIsPressed)
  {
    //Serial.println("Hour was pressed");
    hour++;
    hourButtonIsPressed = true;
    serialOutputTime();
    delay(10);
  }
  else if(!buttonPressed(hourButtonPin))
  {
    hourButtonIsPressed = false;
    delay(10);
  }
 
  /**********  UPDATE TIME  **********/
  // Move forward one second every 1000 milliseconds
  if(millis() - lastTick >= 1000)
  {
    lastTick = millis();
    serialOutputTime();
    second++;
  } 
  // Move forward one minute every 60 seconds
  if(second > 59)
  {
    minute++;
    second = 0;
  }
  // Move forward one hour every 60 minutes
  if(minute > 59)
  {
    hour++;
    minute = 0;
  }
  // Reset the hour
  if(hour > 12)
  {
    hour = 1;
  }
}

boolean buttonPressed(int aPin){
  if(digitalRead(aPin) == HIGH)
  {
    return true;
  }
 
  return false;
}

void serialOutputTime()
{
  Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
  Serial.print(":"); // a colon between the hour and the minute
  Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
  Serial.print(":"); // a colon between the minute and the second
  Serial.println(second, DEC); // the second, sent to the screen in decimal format
}
8  Using Arduino / Installation & Troubleshooting / Re: Open a .pde file from the command line (linux) on: May 30, 2011, 05:36:50 pm
Figured it out: http://gunnewiek.com/2011/open-pde-files-with-arduino-ide-in-linux/
9  Using Arduino / Installation & Troubleshooting / Re: Open a .pde file from the command line (linux) on: May 29, 2011, 06:14:10 pm
even if I do:
arduino /home/phil/programming/Arduino/test/test.pde

I still get a blank sketch

EDIT:

if I do:

echo $0
echo $1
java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel processing.app.Base /home/phil/programming/Arduino/test/test.pde

then run the command: "arduino lol" I get:
/usr/share/arduino/arduino
{blank line here where $1 should be aka lol}

then it opens the program with the proper sketch loaded, so the $1 parameter isn't getting passed properly
10  Using Arduino / Installation & Troubleshooting / [solved] Open a .pde file from the command line (linux) on: May 29, 2011, 01:48:57 pm
I want to be able to do 'arduino test.pde' have have arduino open the .pde file and not a blank file.

I found this: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284958834
But when I added the $1 to the end it didn't change anything.
11  Using Arduino / General Electronics / Controlling lots of LEDs on: April 25, 2011, 09:49:37 pm
I need to control 25 LEDs with my arduino uno individually. If there were 25 different pins then I could just hook up one to each pin, but that is not the case for me. What is the best way to do this?
Pages: [1]