Go Down

Topic: basic arithmetic doesn't work (Read 1 time) previous topic - next topic

robtillaart

#15
Mar 23, 2018, 04:55 pm Last Edit: Mar 23, 2018, 04:56 pm by robtillaart
Depends on whether the pin is configured for PWM. If so, about 5.1us (based on 20,000 iterations), else about half. bitRead(port,pin) is only 2 clock cycles. Every other permutation is between.
simple test of digitalRead - the original code of OP is not using bitRead()

Code: (snippet) [Select]

  for (int pin = 2; pin < 14; pin++)
  {
    start = micros();
    int x = digitalRead(pin);
    stop = micros();

    Serial.print(pin);
    Serial.print("\t");
    Serial.println(stop - start);
  }


output, so all >= 4 average ~6uSec
2 4
3 8
4 4
5 8
6 8
7 8
8 4
9 4
10 8
11 4
12 8
13 4


so even a fall through should get a non zero result
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

GrooveFlotilla

With  duration = micros()-sofar; simply try re-typing it and see what happens and then throw a couple of free parentheses into the mix  duration = (micros()-sofar); just for fun. See what happens.
The smart money is on "nothing"
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

DKWatson

Wasn't offering to take bets.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

DKWatson

The other thing that should be done is to separate the calculation from recording the time.

sofar = micros();
while...
sofar2 = micros();
duration = (sofar2 - sofar);

No bets.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

robtillaart

The other thing that should be done is to separate the calculation from recording the time.

sofar = micros();
while...
sofar2 = micros();
duration = (sofar2 - sofar);

No bets.
I expect the compiler will optimize that away if sofar2 is only used in the print statement.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

DKWatson

Sometimes the trees get in the way of the forest. Change int sofar; to unsigned long sofar;
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

DKWatson

duration should be unsigned long as well.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

cattledog

Quote
Sometimes the trees get in the way of the forest.
Quote
I'm trying to make an echolocation device. Here's my code:
Helping the OP to a situation where the duration is always reported as 4 or 8 microseconds is not really going to be of much use to them.

If of the OP is still anywhere around, they are best directed to the standard pulseIn code used to demonstrate the HC SR04 or to the NewPing library which has non blocking methods, or to the interrupt techniques posted in an earlier reply.

Go Up