I have some problem with the line follower code I have written for atmega8 in avr studio 4. Would anyone check the calibration function and initialization and tell if there is any error in the code segment?
I am enclosing the proteus simulation file here too for convenience.


Code:
#define reverse 1
#define forward 2
#define right_brake 3
#define left_brake 4
#define full_brake 5
#define hard_right 6
#define right 7
#define hard_left 8
#define left 9
#define front 10
#define motor_port PORTD
#define left_bit1 PORTD0
#define left_bit2 PORTD1
#define right_bit1 PORTD2
#define right_bit2 PORTD3
#define sw_port PORTB
#define sw_bit PORTB0
#define enable_port PORTB
#define enable_bit_left PORTB1
#define enable_bit_right PORTB2

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

//uint8_t sw_control; //using dis ill turn the switch on and off


void initADC(){

ADMUX|=(1<<REFS0);
ADCSRA|=(1<<ADPS2)|(1<<ADPS1); //|(1<<ADLAR)???
ADCSRA|=(1<<ADEN);
ADCSRA|=(1<<ADSC);


}


uint16_t readADC(uint8_t ch){

ch=(ch & 0x0F)|(ADMUX & 0xF0);
ADMUX=ch;

ADCSRA|=(1<<ADSC);

while(!(ADCSRA & (1<<ADIF)));

ADCSRA|=(1<<ADIF);

return ADC;


}

void initpwm()
{

TCCR1A|=(1<<COM1A1)|(1<<COM1A0)|(1<<WGM11)|(1<<WGM10);
TCCR1B|=(1<<CS12);
TCNT1=0x0000;
}


void set_pwm(int arg_left,int arg_right)
{

OCR1A=left;
OCR1B=right;
}

void calibrate(uint8_t *midpoint){

unsigned char j,i=0;
uint8_t hi[6]={0,0,0,0,0,0}, lo[6]={255,255,255,255,255,255}; //values of lo requires check

int adc_value;
for (i=0; i<100; i++) 
        

{ 
                

for (j=0; j<5; j++)
               

 {
                        

adc_value = readADC(j);
                      



  if (adc_value <lo[j]) lo[j] = adc_value;
                       

 if (adc_value > hi[j]) hi[j] = adc_value;
               

 }
               


 _delay_ms(250); 
        }
        
      


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

 {
               

 midpoint[i] = (lo[i] + (hi[i] - lo[i]) / 2);
      

  }
  





}

//for forward movement of robot
//left motor port bit configuration 1 and 0
//right motor port configuration 0 and 1

void motor_dir(uint8_t dir){

switch(dir){

case forward:  //robot moves forward bt motor reverse 10 and 01 for left nd right
motor_port|=(1<<left_bit1)|(1<<right_bit2);
motor_port &= ~((1<<left_bit2)|(1<<right_bit1));

break;

case reverse:
motor_port|=(1<<left_bit1)|(1<<right_bit1);
motor_port &=~((1<<left_bit2)|(1<<right_bit2));

break;

case left_brake:
motor_port|=(1<<left_bit1)|(1<<left_bit2); //brake left motor
motor_port|=(1<<right_bit2);   //right motor usual direction
motor_port &= ~(1<<right_bit1);

break;

case right_brake:  //brakes only right motor left one active
motor_port|=(1<<right_bit1)|(1<<right_bit2);
motor_port|=(1<<left_bit1);
motor_port &= ~(1<<left_bit2);
break;

case full_brake:
motor_port|=(1<<left_bit1)|(1<<left_bit2)|(1<<right_bit1)|(1<<right_bit2);
break;

}


}


void enable_motor(){

enable_port|=(1<<enable_bit_left)|(1<<enable_bit_right);

}

void disable_motor(){

enable_port &=~((1<<enable_bit_left)|(1<<enable_bit_right));

}

/*void sw(){   //switch on the robot
sw_port|=(1<<sw_bit); 

_delay_ms(500);
}
*/

void steer(uint8_t s){

switch(s){

case hard_right:
motor_dir(full_brake); 
motor_dir(reverse);
set_pwm(10,1024);
break;

case right:
motor_dir(left_brake);
set_pwm(0,1024);
break;

case hard_left:
motor_dir(full_brake);
motor_dir(reverse);
set_pwm(1024,1);
break;

case left:
motor_dir(right_brake);
set_pwm(1024,0);
break;

case front:
motor_dir(forward);
set_pwm(1024,1024);
break;

}


}

/*ISR(INT0_vect){

motor_port ^=((1<<enable_bit_left)|(1<<enable_bit_right)); //turn off motor if on ie toggle

GIFR |=(1<<INTF0); //clear interrupt flag pin 
}
*/
void main(){

uint8_t s=0;
unsigned char i=0;
uint8_t midpoint[5] = { 0,0,0,0,0 };
uint8_t last_direction = front;
DDRC=0x00;
DDRD=0xff;
DDRB=0xff;

initADC();
initpwm();


/*GICR|=(1<<INT0);// interrupt fot disabling motor
sei();*/

//calibrate

enable_motor();

steer(hard_right);
calibrate(midpoint);


disable_motor(); //check??

//sw(); //switch on the motor

if((bit_is_set(PINB,0))) enable_motor();
else disable_motor();

while(1){
for(i=0;i<6;i++){
if(readADC(i)>=midpoint[i])
{
s|=(1<<i);
}
else s &=~(1<<i);

}

if(s==0xC0||s==0xE0||s==0xA0){

steer(hard_left);
last_direction=hard_left;

}
else if(s==0x40||s==0x20||s==0x60){

steer(left);
last_direction=left;

}

else if(s==0x1C||s==0x0C||s==0x14){

steer(hard_right);
last_direction=hard_right;
}

else if(s==0x18||s==0x08||s==0x10){
steer(right);
last_direction=right;
}

else if(s==0xFC||s==0x0C||s==0x1E) {
steer(front);
last_direction=front;
}

else {

steer(last_direction); //is again interrupt required here to disable the motor??


}



}



}