/* ------------------------------------------------------------------------
File: atodtest.c
This program demonstrates A to D converter and LEDs.

This program uses the Engr/CS 101 Version 3 robotics library for
Version 6 of the controller board.

Keil C headers are not guarded.  If robotics library is used, its header file 
should be the only included file.
---------------------------------------------------------------------------*/

#include "robotics6b.h"

/* Constant declarations */
#define DELAY_TIME 20   /* about 1 sec */

void main (void)
{ 
   /* Variable declarations */  
   unsigned char channel0, channel1;  /* A to D channels */

   /* Initialization */
   InitializeMotors();
   InitializeIO();

   while (1)
   {
      Delay (DELAY_TIME);
      Beep (1);
      channel0 = GetAtoD (0);
      channel1 = GetAtoD (1);
      Delay (DELAY_TIME*2);

      if (channel0 > channel1 + 20)
      {
         Beep(2);
         SetOutputPort (11, LOW);
         SetOutputPort (12, HIGH);
      }
      else if (channel1 > channel0 + 20)
      {
         Beep(3);
         SetOutputPort (11, HIGH);
         SetOutputPort (12, LOW);
      }
      else
      {
         Beep(4);
         SetOutputPort (11, HIGH);
         SetOutputPort (12, HIGH);
      }

      Delay (DELAY_TIME*2);
      if ((channel0 < 128) && (channel1 < 128))
      {
         Beep(2);
         SetOutputPort (13, HIGH);
         SetOutputPort (14, HIGH);
      }
      else if ((channel0 >= 128) && (channel1 >= 128))
      {
         Beep(3);
         SetOutputPort (13, LOW);
         SetOutputPort (14, LOW);
      }
      else if ((channel0 < 128) && (channel1 >= 128))
      {
         Beep(4);
         SetOutputPort (13, HIGH);
         SetOutputPort (14, LOW);
      }
      else  /* channel0 >= 128 && channel1 < 128 */
      {
         Beep(5);
         SetOutputPort (13, LOW);
         SetOutputPort (14, HIGH);
      }
   }
}
