/* Written by Pat McMahon 25/1/2019 
  *  Infrared Sports Action - Table Tennis
 *  Copied by Pat McMahon 21/7/2018 from Chapter 2.8 of Alan Smith's "Introduction to Arduino" textbook
 *  www.introtoarduino.com
 *  
 */

#include <IRremote.h>

const int kPinLed1 = 2;
const int kPinLed2 = 3;
const int kPinLed3 = 4;
const int kPinLed4 = 5;
const int kPinLed5 = 6;
const int kPinLed6 = 7;
const int kPinLed7 = 8;
const int kPinLed8 = 9;
const int kPinLed9 = 10;
const int kPinLed10 = 11;
const int kPinLed11 = 12;
const int kPinLed12 = 13;
const int kPinLed13 = 14;
const int kPinLed14 = 15;
const int kPinLed15 = 16;
const int kPinLed16 = 17;
const int kPinLed17 = 18;
const int kReceivePin = A5;
const int kDelayTime = 60;

IRrecv irrecv(kReceivePin);

void setup()
{
  Serial.begin(9600);
  Serial.println("Setup");

  pinMode(kPinLed1, OUTPUT);
  pinMode(kPinLed2, OUTPUT);
  pinMode(kPinLed3, OUTPUT);
  pinMode(kPinLed4, OUTPUT);
  pinMode(kPinLed5, OUTPUT);
  pinMode(kPinLed6, OUTPUT);
  pinMode(kPinLed7, OUTPUT);
  pinMode(kPinLed8, OUTPUT);
  pinMode(kPinLed9, OUTPUT);
  pinMode(kPinLed10, OUTPUT);
  pinMode(kPinLed11, OUTPUT);
  pinMode(kPinLed12, OUTPUT);
  pinMode(kPinLed13, OUTPUT);
  pinMode(kPinLed14, OUTPUT);
  pinMode(kPinLed15, OUTPUT);
  pinMode(kPinLed16, OUTPUT);
  pinMode(kPinLed17, OUTPUT);

  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
  decode_results results;

  if (irrecv.decode(&results)) // if there is an IR reading
  {
    Serial.println(results.value, HEX);

    switch (results.value)
    {
      case 0x2F0:
        Serial.println("Up!");
     //   Playx5();
      //  break;
      //case 0xAF0:
       // Serial.println("Back!");
       // BallTracking;
      case 0x2D0:
        Serial.println("Left!");
        ServeLeft;
      case 0xCD0:
        Serial.println("Right!");
        ServeRight;
      //case 0xA70:
       // Serial.println("Stop!");
       // Stop;
    }

    irrecv.resume(); // Receive the next value
  }
}

void ServeRight()
{
  // turn on each of the LEDs in order Left to Right
  for (int i = 0; i < 2; i++)
  {
    delay(kDelayTime);
    digitalWrite(kPinLed1, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed1, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed2, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed2, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed3, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed3, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed4, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed4, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed5, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed5, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed6, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed6, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed7, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed7, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed8, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed8, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed9, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed9, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed10, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed10, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed11, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed11, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed12, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed12, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed13, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed13, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed14, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed14, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed15, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed15, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed16, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed16, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed17, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed17, LOW);
    delay(kDelayTime);
    
    // turn on each of the LEDs in order Right to Left
    delay(kDelayTime);
    digitalWrite(kPinLed17, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed17, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed16, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed16, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed15, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed15, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed14, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed14, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed13, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed13, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed12, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed12, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed11, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed11, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed10, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed10, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed9, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed9, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed8, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed8, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed7, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed7, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed6, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed6, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed5, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed5, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed4, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed4, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed3, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed3, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed2, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed2, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed1, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed1, LOW);
    delay(kDelayTime); 
  }
  void ServeLeft()
  {
  // turn on each of the LEDs in order Left to Right
  for (int i = 0; i < 2; i++)
  {// turn on each of the LEDs in order Right to Left
    delay(kDelayTime);
    digitalWrite(kPinLed17, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed17, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed16, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed16, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed15, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed15, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed14, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed14, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed13, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed13, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed12, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed12, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed11, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed11, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed10, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed10, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed9, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed9, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed8, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed8, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed7, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed7, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed6, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed6, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed5, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed5, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed4, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed4, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed3, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed3, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed2, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed2, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed1, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed1, LOW);
    delay(kDelayTime); 
    delay(kDelayTime);
    digitalWrite(kPinLed1, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed1, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed2, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed2, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed3, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed3, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed4, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed4, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed5, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed5, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed6, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed6, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed7, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed7, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed8, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed8, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed9, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed9, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed10, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed10, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed11, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed11, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed12, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed12, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed13, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed13, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed14, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed14, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed15, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed15, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed16, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed16, LOW);
    delay(kDelayTime);
    digitalWrite(kPinLed17, HIGH);
    delay(kDelayTime);
    digitalWrite(kPinLed17, LOW);
    delay(kDelayTime);
  }  
    
  }
  //void Playx5()
 // void Stop()
 // void BallTracking()
}