Go Down

Topic: How to get arduino to print something excatly once every milisecond? (Read 224 times) previous topic - next topic

GrOnThOs

Quote
The normal millis() counter actually increments every 1024 microseconds; slightly longer than a millisecond.
Is this number always the same or depends of arduino board environment temperature?

Robin2

Is this number always the same or depends of arduino board environment temperature?
The count won't change with temperature but the speed of the oscillator probably will.


By the way I'm not convinced that it divides by 1024. I think it divides by 1000. I see this code in the file wiring.c
Code: [Select]
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

bwenox

UKHeliBob, changed to array to unsigned long, but I get the same result.

UKHeliBob

UKHeliBob, changed to array to unsigned long, but I get the same result.
I have lost track of the problem with your current code.

Please explain the current problem and post your complete current code so that we can try it for ourselves.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

bwenox

I have lost track of the problem with your current code.

Please explain the current problem and post your complete current code so that we can try it for ourselves.
Sorry about that.

I'm trying to make Arduino to save some value of rotaryencoder once every 1 ms (or some other time I choose - 3, 5 ... ms)

Problem:
My current code

Code: [Select]

int voltage;
int table[200];
unsigned long timee[200];
int n;
bool finish;
unsigned long ttimee;


void setup() {
  pinMode(9, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);
  Serial.begin(115200);
  digitalWrite(3, 0);
  analogWrite(9, 90);
  n = 0;
  finish = 0;
  ttimee = 0;
}

void loop() {
  if (n < 200) {
    if (millis() - ttimee >= 1) {
      voltage = analogRead(A0);
      table[n] = voltage;
      timee[n] = millis();
      n++;
      ttimee = millis();
    }
  }
  else if (!finish) {
    finish = true;
    analogWrite(9, 0);
    for (n = 0; n < 200; n++)
    {
      Serial.print(timee[n]); Serial.print("\t"); Serial.println(table[n]);
    }

  }
}


skips a ms sometimes.

Go Up