Software Section

                                                                                                                           <Back to Topics

Code to Detect Obstacle using IR(Infrared) Sensor

The explanation on functionality of a ,  and relevant circuit diagrams are provided in the electronics section of this web site, please go through this link -->  Detecting obstacle with IR (Infrared).

 The code provided below is written for microcontroller atmega8535 which has a crystal of 8Mhz, this means a microcontroller having a lower crystal may result in slower execution of program (program runs slower than what it does with 8Mhz crystal).

 

#include "E:\WinAVR-20080512\avr\include\avr\io.h"

/* function prototype */

void millisecond_delay(unsigned int count);

int  main()

{

unsigned int i = 0;

unsigned int r_count = 0;

/* Initialize  timer (wave generation OCR2 )*/

OCR2 = 119;

TCCR2 = 0x19;

/* Initialize LED */

DDRB = DDRB & 0x01;

/* Initialize IR receiver */

DDRD = DDRD & 0xFB;

/* Continuos Loop */

while(1)

    {

    /* Activate OCR2 wave generation for 10 milliseconds */
    DDRD = DDRD | 0x80;

    for (i = 0; i <10 ; i++)

      {

      /* Delay one millisecond */

      millisecond_delay(1);

      if (((PIND >> 2) & 0x01) == 0)

        {

        /* increment counter when a signal is received from the IR receiver module */

        r_count++;

        }

      }

    /* Deactivate OCR2 wave generation */

    DDRD = DDRD &  0x7F;

    /*

    if (r_count >5)

      {

      /* ON LED */

      PORTB = PORTB & 0x1;

      /* Delay while the LED is ON */

      millisecond_delay(200);

    • /* OFF LED */
    • PORTB = PORTB & 0xFE;

      }

    /* Initialize IR receiver count */

    r_count = 0;

    /* Delay to keep maintain the OFF state of the IR transmitter */

    millisecond_delay(90);

    }

return 0;

}

 

void millisecond_delay(unsigned int count)

{

unsigned int i =0;

unsigned int j = 0;

do

    {

    /*One  millisecond delay

    The delay is measured for 8 Mhz crystal */

    for (i=0;i<=400;i++)

      {

      /* delay */ 

      }

      j++;

    }while(j<count);

}

 

Result :

Program runs continuously as long as the microcontroller is powered ON and the LED blinks when ever an obstacle is detected.

The circuit diagram is explained in the Electronics section,

 please go through the link IR obstcale detection circuit.

Best Viewed in Internet Explorer

With other browsers you may experience improper view

List of Topics --> Software

Simple Program to Blink an LED (using embedded C language)

Code to Control a Stepper Motor

Code to Detect Obstacle using IR(Infrared) Sensor

Articles on C

Data types (under construction)

 

List of Topics --> Electronics

Controlling  a Stepper Motor

Drive a Stepper Motor (using IC STA401A)

Detecting obstacle with IR (Infrared) Sensor

Use Motor as Dynamo, Motor as a Brake, Dynamometer Test

 

[Home] [Robotics] [Electronics] [Software] [Contact Me]