Products > Embedded Computing

[arduino] can't subtract two time stamps

(1/17) > >>

I am trying to calculate a speed based on how many pulse's a proximity sensor next to a toothed wheel has generated in an amount of time.

--- Code: ---uint32_t varLastSpeedTime;
volatile int16_t vTachoSpeedCounts;

void lcdSpeedPrint() {
  uint32_t tempvarTime =  millis();
  uint32_t vtSpeed = (vTachoSpeedCounts * tachoStep * 600) / (millis() - varLastSpeedTime);
  int32_t tvTimeDifference = ((int32_t)tempvarTime - (int32_t)varLastSpeedTime);
  char text[6] = {0, 0, 0, 0, 0, 0};
  char counter = 0;
  char temp;
  lcd.setCursor(0, 2);
  lcd.setCursor(0, 3);
  lcd.print(tvTimeDifference, DEC);

  varLastSpeedTime = millis();
  vTachoSpeedCounts = 0;

--- End code ---

The difference between the current time and the last time always results in some randomly low number lik 6 or 9 or 7.

Initially "vTachoSpeedCounts" was called "varTachoSpeedCounts" and it failed to update. I don't know if anything starting with "var" has some other meaning in Arduino but the colour of the text never changed. Unfortunately colour highlighting on the arduino IDE is very limited and you can't tell the difference between things like variables and defined text.

First thing I'd do is replace all but the first millis() call with tempvarTime. I guarantee the last millis() call isn't returning the same number as the first.

Nope, not that the calculation the only other call to millis is in is being used. I gave up displaying the speed and just wanted to see the time difference come out right. As the function is called around every 2s I should get something just over 2000.

vTachoSpeedCounts will count up if I don't reset it, but when I do and put a signal in at 20Hz I get 0,

this is absurd and annoying because it's not like I can't write a program to do this, I'm trying to get a hitachi controller based display to work on a SAMD so that I can for now ditch this arduino crap but converting peter fleury's library from AVR to ARM is a good bit harder than converting from one AVR to another.

Of course it doesn't matter what the variable names are.

And it is evident that your function is being called about every 10 ms not every 2 seconds as you state. Which is why the count is zero for a 20 Hz signal.

That's your problem.

why is it evident that it is being called every 10ms? This is just one function in a program. As you can see I am putting the current millis() on the display, this does indeed every 2s update (very visibly) by another 2000 and a bit).

If I increase my signal to 1kHz I get "8" as an answer.

--- Code: ---void tachoUpdate() {
  vTachoSpeedCounts ++;
  tachoDistanceCounts ++; 

--- End code ---

And yes so long as the name started with var it never updated but will sort of now. It's crazy


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version