//--------------------------------------------------------------------------------
// start the main function here
//--------------------------------------------------------------------------------
int main(void)
{
 float battery_low = 0;

 init_devices();
  
 packet_valid = 0;// initialise valid packet variable to 0
 
 // initially calculate the battery voltage 
 UCSRB = 0x00; 
 ADMUX = 0x26; //select ADC input 6
 battery_voltage = ADC_conversion(); 
 UCSRB = 0x98; 
 flag2 = 0;

 while(1)
 {
   // flag2 will rise once in 10sec and calculates the Battery voltage
   // and enables the UART communication
   if(flag2 == 1)
    {  	
	  ADMUX = 0x26;                           //select ADC input 6
      battery_voltage = ADC_conversion();  
      UCSRB = 0x98;                           
      flag2 = 0;
    }

   // if battery voltage is less then battery threshold value ,buzzer beeps
   battery_low = (battery_voltage * 0.041) + 0.7;  // battery voltage caluculation
   if(battery_low < BATTERY_TRHRESHOLD)
   { buzzer_on();  _delay_ms(200); buzzer_off();_delay_ms(200);}
  
   // following function beeps the buzzer twice after sucessfully reciving 10 packets
   // from the master(FireBird-V Hexapod robot)
   if(flag1 == 0)
   { 
    if(communication_proper_count > 10)
     { 
	   flag1 = 1;
	   buzzer_on();  _delay_ms(400); buzzer_off();_delay_ms(400);
	   buzzer_on();  _delay_ms(400); buzzer_off();_delay_ms(400);
     }  
   }

 }

}
/*
* Function Name: main
* Input: None
* Output: int to inform the caller that the program exited correctly or
* incorrectly (C code standard)
* Logic: constantly monitors the sensor value and blows buzzer above a defined threshold value. 
* Example Call: Called automatically by the Operating System
*
*/
void main()
{
	unsigned int sensor2_value=0; //variable to store proximity sensor2 value
 	unsigned int sensor3_value=0; //variable to store proximity sensor3 value
 	unsigned int sensor4_value=0; //variable to store proximity sensor4 value
 	unsigned char i=0;
 	output_enable = 0;
 	start_conv = 0;    //de-assert all control signals to ADC
 
 	buzzer = 1; 	//buzzer off
 	lcd_init(); 	//Initialize LCD
 	pca_init();		//initialize PCA for velocity control
 	lcd_clear();  	//clears LCD
 	forward();
 	left_motor_velocity(0x2F);			
	right_motor_velocity(0x2F);
 while(1)
 {
	sensor2_value=ADC_conversion(3);
	sensor3_value=ADC_conversion(0);
	sensor4_value=ADC_conversion(7);
	
	lcd_print(1,1,sensor2_value,3);
	lcd_print(1,5,sensor3_value,3);
	lcd_print(1,9,sensor4_value,3);
	
	if(sensor2_value<50 || sensor3_value<50 || sensor4_value<50) //obstacle detected
	{
	stop();
	delay_ms(1000);
	buzzer=0;	//buzzer on
	delay_ms(2000);	 
	buzzer=1;	//buzzer off	
	break; 
	}			
 }
} //main ends here
void main()
{
 unsigned char i=0;
 output_enable = 0;
 start_conv = 0;    //de-assert all control signals to ADC
 
 buzzer = 1;  //buzzer off
 lcd_init(); //Initialize LCD
 pca_init(); //Initialize PCA to genarate PWM

forward();
left_motor_velocity(0x4F);  //0x00 will give full (100% duty cycle) velocity, while 0xFF will give zero (0% duty cycle) velocity. Any value in between 0x00 and 0xFF will give intermediate velocity.			
right_motor_velocity(0x4F); //0x00 will give full (100% duty cycle) velocity, while 0xFF will give zero (0% duty cycle) velocity. Any value in between 0x00 and 0xFF will give intermediate velocity.


 while(1)
 {
  for(i=0;i<8;i++)	 //Doing ADC conversion
  {
  data_array[i] = ADC_conversion(i);
  }

  left_whiteline = data_array[4];   
  center_whiteline = data_array[5];
  right_whiteline = data_array[6];
  front_sharp_sensor = data_array[2];

  flag = 0; //reset the flag to 0
     
   //check if robot's center white line sensor is on the white line
  if(center_whiteline < 25)
  {
   flag = 1;  //set the flag to 0 so that further white line sensor comparision is disabled
   left_motor_velocity(0x4F);  //left and right motor is at same velocity
   right_motor_velocity(0x4F);
  }

  //robot is drifting towards left side, increase velocity of the left wheel and decrease velocity of the right wheel
  if ((left_whiteline > 25) && (flag == 0))
  {
   flag = 1;  //set the flag to 0 so that further white line sensor comparision is disabled
   left_motor_velocity(0x3F);   //increase left motor velocity
   right_motor_velocity(0x5F);	//decrease  right motor velocity
  }

  //robot is drifting towards right side, decrease velocity of the left wheel and increase velocity of the right wheel
  if ((right_whiteline > 25) && (flag == 0))
  {
   flag = 1;  //set the flag to 0 so that further white line sensor comparision is disabled
   left_motor_velocity(0x5F);	//decrease  right motor velocity
   right_motor_velocity(0x3F);  //increase left motor velocity
  }

  if(front_sharp_sensor >= 110) // obstacle is near the robot, stop and turn on the buzzer
  {
   stop();
   buzzer = 0;  //buzzer on
  }
  
  else
  {
   forward();
   buzzer = 1;  //buzzer off
  }

  if((left_whiteline > 25) && (center_whiteline > 25) && (right_whiteline > 25)) // no whiteline is detected, stop (buzzer will not turn on)
  {
   stop();
  }
  
  lcdprint(data_array);//call this function to print the array onto the screen
  delay_ms(250);
 }
}