// SerialInput_2 (Arduino)
//
// Illustrates reading from the serial port.
//
// Opens the serial port (Serial.begin), flushes the serial port buffer by continually 
// reading (Serial.read) until Serial.available is 0.
//
// The program then continually loops.  If Serial.available is 2, the two characters are
// read and parsed into a cmd and a number.
//
// If the command is an 'A', the asssociated A/D is read and this is sent to the PC.
// If the command is an 'F', pin 13 is flashed the specified number of times.
//
// copyright, Peter H Anderson, Nov 5, '09

#define DIR_OUT(pin) pinMode(pin, OUTPUT)
#define DIR_IN(pin)   pinMode(pin, INPUT)
#define O_HIGH(pin) digitalWrite(pin, HIGH)
#define O_LOW(pin) digitalWrite(pin, LOW)
#define IN(pin) digitalRead(pin)


#define TRUE !0
#define FALSE 0

void flush_buffer(void);
int is_num(char ch);

void setup()
{
  Serial.begin(9600);
  delay(100); 
  flush_buffer();
  DIR_OUT(13);
}

void loop()
{
  int  adval, num, times, n;
  char cmd;
  
  while(1)
  {
     if (Serial.available()>=2)
     {
       
        cmd = Serial.read();
        times = Serial.read() - '0';
     
        //Serial.println(cmd, DEC);
        //Serial.println(times, DEC);
     
        if ((cmd == 'A') || (cmd == 'a'))
        {
           adval = analogRead(num);
           delay(50);
           Serial.println(adval, DEC);
        }
     
        else if ((cmd == 'F') || (cmd == 'f'))
        {
           for (n=0; n