#include <main.h>

unsigned long counter = 0;
unsigned ms_count=0;
unsigned press = 0;
unsigned values[4];

#define LCD_ENABLE_PIN PIN_C0
#define LCD_RS_PIN PIN_C2
#define LCD_RW_PIN PIN_C1
#define LCD_DATA4 PIN_C4
#define LCD_DATA5 PIN_C5
#define LCD_DATA6 PIN_C6
#define LCD_DATA7 PIN_C7

#include <lcd.c>
/***************************Globals***********************************/

unsigned int read_potA(void);    //They 3 unsigned ints are to set the pot value reders.(Task1)
unsigned int read_potB(void);
unsigned int read_potC(void);
unsigned int valueA;             //Theys are the return values of the pots witch will be githern within there sections.(Task1)
unsigned int valueB;
unsigned int valueC;
unsigned int button1(void);


void main()
{
	setup_adc_ports(sAN0 | sAN1 | sAN2, VSS_VDD);
	setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_0);

   lcd_init();
 
	while(TRUE)
	{
	   lcd_gotoxy(2,1);		// This section starting with lcd_gotoxy are setting the position of the texst sentraley within the LCD screen.(Task1)
	   printf(lcd_putc,"Pot A Value =%3u",read_potA());
	   
	   lcd_gotoxy(2,2);
	   printf(lcd_putc,"Pot B Value =%3u",read_potB());		//All printf's are desplaying the texst to the LCD screen.(Task1)
	   
	   lcd_gotoxy(22,1);
	   printf(lcd_putc,"Pot C Value =%3u",read_potC());
	   
	 if(input(PIN_A4))			//This activated the movement of the moter clockwize when the button is pressed.(Task 2 i)
	   {
	      output_high(PIN_A6);
	      output_low(PIN_A7);

	   }
/*******************************************************************Attempt at button press for task 2******************************************/	   
/*	   if(input(PIN_A4))			//This activated the movement of the moter when the button is pressed.(Task 2 i)
	   {
	      while(input(PIN_A4)==1)
	      output_high(PIN_A6);
	      output_low(PIN_A7);
	      
	      if(input(PIN_A4))
	      {
		 while(input(PIN_A4))
		 output_low(PIN_A6);         //This stopes the movement of the moter. (Task 2 ii)
	         output_low(PIN_A7);
	      }

	   }*/
	   
/*
Task 2 rif raff
	   
output_low(PIN_A6);         //This stopes the movement of the moter. (Task 2 ii)
output_low(PIN_A7);
	   
output_low(PIN_A6);         //This makes the moter move anti clock wize (Task 2 iii)
output_high(PIN_A7);
	   
output_low(PIN_A6);         //This stopes the movement of the moter. (Task 2 iV)
output_low(PIN_A7);
/*******************************************************************************************************************************************/
	   
	   if(input(PIN_A5))		//desplayes the value of Veriable resistor on the LED,s for 2 seconds (Task 3)
	   {
	      output_B(read_potA());    //Desplayes pot A's value in LEDs (Task 3)
	      delay_ms(2000);
	      output_B(read_potB());    //Desplayes pot B's Values in LEDs (Task 3)
	      delay_ms(2000);
	      output_B(read_potC());	//Desplayes pot C's values in LEDs (Task 3)
	      delay_ms(2000);
	   }
	 }
}

unsigned int button1(void) // This is a attempt as getting the button 1 to count up by one on eatch press.(Task2)
{
   values[press]=read_adc(adc_read_only);
   press++;
   if(press>5) press = 0;
   set_adc_channel(press);
   delay_us(1);
   read_adc(adc_start_only);
}

unsigned int read_potA(void)  //The following 3 read pot functions are reading the value of the veriable resistor so that it can be printed to the screen. (Task1)
{			      
   unsigned int valueA;
   set_adc_channel(0);
   delay_us(20);
   valueA=read_adc();
   return(valueA);
}

unsigned int read_potB(void) //This section reads the pot value of B (Task1)
{
   unsigned int valueB;
   set_adc_channel(1); 
   delay_us(20);
   valueB=read_adc();
   return(valueB);
}

unsigned int read_potC(void) //This section reads the pot value of C (Task1)
{
   unsigned int valueC;
   set_adc_channel(2); 
   delay_us(20);
   valueC=read_adc();
   return(valueC);
}
