Need explanation regarding Interrupts

I just bought a new ultrasonic sensor and i'm struggling at understanding the code.

#define TIMEOUT_OVERFLOW 1000
 
int TrigPin = 2;
int EchoPin = 3;
unsigned long ultrasoundDuration;
 
void setup()
{
  Serial.begin(9600);
 
}
 
void loop()
{
  unsigned char pin = 0;
  unsigned int time_flag = 0;
 
  pinMode(TrigPin, OUTPUT); //pin is output
  pinMode(EchoPin, INPUT); // pin is now input
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(2);
  digitalWrite(TrigPin, LOW);
  delayMicroseconds(10);
  digitalWrite(TrigPin, HIGH);
 
  // wait for a LOW pulse
  //ultrasoundDuration = pulseIn(EchoPin, HIGH);// will not work for HIGH pulse
 
  TCCR1A = 0x00;
  TCNT1 = 0x0000;
  TCCR1B = 0x01;
  pin = digitalRead(EchoPin);
  while(pin)
  {
    pin = digitalRead(EchoPin);
    time_flag++;
    if(time_flag > TIMEOUT_OVERFLOW)
      break;
  }
  TCCR1B = 0x00;
  ultrasoundDuration = TCNT1;
  ultrasoundDuration = ultrasoundDuration / 16;
  // get results
  Serial.print(ultrasoundDuration);
  Serial.print(" us, ");
  //Serial.print(ultrasoundDuration/148, DEC); //divide with 148 and you get inches
  //Serial.print(" inches, ");
  Serial.print(ultrasoundDuration*0.017, DEC); //divide with 58 and you get cm
  Serial.print(" cm");
  Serial.println();
  delay(100);
}

Whats "pin" doing in the whilestatement? And what does TCCR1A = 0x00, TCNT1 = 0x0000 and TCCR1B = 0x01 mean? Is there a tutorial about this? Couldnt find anything because I didnt know what I had to look for.

sincerely

bluebubble

There aren't any interrupts actually happening here. The ATmega328P microcontroller has three Timer/Counters. This code uses Timer/Counter1 to measure the time between sending the ping and receiving the echo. TCNT1 is the timer register in the MCU, TCCR1A and TCCR1B are control registers associated with the timer. See the ATmega328P datasheet, section 16; there are also good writeups on timers on Nick Gammon's site and on Dean Camera's site.

bluebubble:
I just bought a new ultrasonic sensor and i'm struggling at understanding the code.

Whats "pin" doing in the whilestatement? And what does TCCR1A = 0x00, TCNT1 = 0x0000 and TCCR1B = 0x01 mean? Is there a tutorial about this? Couldnt find anything because I didnt know what I had to look for.

sincerely

bluebubble

I would suggest using a library to control the sensor as this code seems incomplete. To toot my own horn, I suggest using the NewPing library: http://arduino.cc/forum/index.php/topic,106043.0.html It's quite easy to implement and it works. Also, I have working interrupt code in my development version that I'll be releasing soon. Not that you really need to use interrupts to use a ping sensor, it doesn't sound like you really need it anyway, unless I'm missing something.

Tim

thanks for your help. I tried your library teckel, but it din't work. I also read both articels about the timer, but I still don't understand the purpose of

  TCCR1A = 0x00;
  TCNT1 = 0x0000;
  TCCR1B = 0x01;
  
  (later)
  TCCR1B = 0x00;

  ultrasoundDuration = TCNT1;

what do those 0x00 do?

0x as a prefix denotes a hexadecimal number. 0xff is 255 decimal. 0x00 is just zero.

  TCCR1A = 0x00;  //sets the counter for normal mode (as opposed to CTC or a PWM mode)
  TCNT1 = 0x0000;  //TCNT1 is the actual counter value, set it to zero
  TCCR1B = 0x01;  //sets the counter to count at the system clock frequency (e.g. 16MHz) and starts it counting
  
  (later)
  TCCR1B = 0x00;  //stops the counter

  ultrasoundDuration = TCNT1;  //assigns the count value to ultrasoundDuration